Class: Kommando::ScheduledCommandWorker

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

Constant Summary collapse

WAIT_STEP_SIZE =
0.25
WAIT_LIMIT =
20.0

Instance Method Summary collapse

Constructor Details

#initialize(adapter, dependencies, number_of_runners) ⇒ ScheduledCommandWorker

Returns a new instance of ScheduledCommandWorker.



6
7
8
9
10
11
12
13
# File 'lib/kommando/scheduled_command_worker.rb', line 6

def initialize(adapter, dependencies, number_of_runners)
  @number_of_runners = number_of_runners
  @runners = []

  @runners << ScheduledCommandRunner.new(@runners.size + 1, adapter, dependencies) while @runners.size < @number_of_runners

  @self_read, @self_write = IO.pipe
end

Instance Method Details

#startObject



15
16
17
18
# File 'lib/kommando/scheduled_command_worker.rb', line 15

def start
  @runners.each(&:start)
  IO.select([@self_read])
end

#stopObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kommando/scheduled_command_worker.rb', line 20

def stop
  @runners.each(&:stop_before_next_fetch)

  deadline = now + WAIT_LIMIT

  sleep WAIT_STEP_SIZE until @runners.all?(&:stopped?) || now > deadline

  @runners.reject(&:stopped?).each(&:kill)

  @self_write.puts(:stop)
end