Class: Workers::Scheduler

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scheduler

Returns a new instance of Scheduler.



3
4
5
6
7
8
# File 'lib/workers/scheduler.rb', line 3

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

Instance Method Details

#disposeObject



34
35
36
37
38
39
40
41
42
# File 'lib/workers/scheduler.rb', line 34

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

  return nil
end

#schedule(timer) ⇒ Object



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

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

  wakeup

  return nil
end

#unschedule(timer) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/workers/scheduler.rb', line 20

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

  return true
end

#wakeupObject



28
29
30
31
32
# File 'lib/workers/scheduler.rb', line 28

def wakeup
  @thread.wakeup

  return nil
end