Class: Everyx::Everyx

Inherits:
Object
  • Object
show all
Defined in:
lib/everyx.rb

Overview

The class that responsible for running the periodic task

Instance Method Summary collapse

Constructor Details

#initializeEveryx

Returns a new instance of Everyx.



6
7
8
# File 'lib/everyx.rb', line 6

def initialize
  @threads = []
end

Instance Method Details

#every(delay, runner) ⇒ Object

Call the runner every ‘delay` seconds



11
12
13
14
15
16
17
18
# File 'lib/everyx.rb', line 11

def every( delay, runner )
  @threads << Thread.new(runner, delay) do |r,delay|
    loop do
      r.call
      sleep delay
    end
  end
end

#in(delay, runner) ⇒ Object

Call the runner once, ‘delay` seconds from now.



21
22
23
24
25
26
# File 'lib/everyx.rb', line 21

def in( delay, runner )
  @threads << Thread.new( runner, delay ) do |r, delay|
    sleep delay
    r.call
  end
end

#joinObject

Join all the threads. Call this when you’re all done with the Everyx instance.



30
31
32
# File 'lib/everyx.rb', line 30

def join
  @threads.each {|t| t.join }
end