Class: Workers::Scheduler

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/workers/scheduler.rb

Instance Method Summary collapse

Methods included from Helpers

#concat_e, #log_debug, #log_error, #log_info, #log_warn

Constructor Details

#initialize(options = {}) ⇒ Scheduler

Returns a new instance of Scheduler.


5
6
7
8
9
10
11
12
13
# File 'lib/workers/scheduler.rb', line 5

def initialize(options = {})
  @logger = Workers::LogProxy.new(options[:logger])
  @pool = options[:pool] || Workers::Pool.new
  @schedule = SortedSet.new
  @mutex = Mutex.new
  @thread = Thread.new { start_loop }

  nil
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/workers/scheduler.rb', line 49

def alive?
  @thread && @thread.alive?
end

#disposeObject


39
40
41
42
43
44
45
46
47
# File 'lib/workers/scheduler.rb', line 39

def dispose
  @mutex.synchronize do
    @pool.shutdown
    @pool.join
    @thread.kill
  end

  nil
end

#schedule(timer) ⇒ Object


15
16
17
18
19
20
21
22
23
# File 'lib/workers/scheduler.rb', line 15

def schedule(timer)
  @mutex.synchronize do
    @schedule << timer
  end

  wakeup

  nil
end

#unschedule(timer) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/workers/scheduler.rb', line 25

def unschedule(timer)
  @mutex.synchronize do
    @schedule.delete(timer)
  end

  nil
end

#wakeupObject


33
34
35
36
37
# File 'lib/workers/scheduler.rb', line 33

def wakeup
  @thread.wakeup

  nil
end