Class: Droonga::Supervisor

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/droonga/supervisor.rb

Defined Under Namespace

Classes: WorkerConfiguration, WorkerRunner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loop, n_workers, config) ⇒ Supervisor

Returns a new instance of Supervisor.



24
25
26
27
28
29
# File 'lib/droonga/supervisor.rb', line 24

def initialize(loop, n_workers, config)
  @loop = loop
  @n_workers = n_workers
  @config = config
  @on_ready = nil
end

Instance Attribute Details

#on_ready=(value) ⇒ Object (writeonly)

Sets the attribute on_ready

Parameters:

  • value

    the value to set the attribute on_ready to.



23
24
25
# File 'lib/droonga/supervisor.rb', line 23

def on_ready=(value)
  @on_ready = value
end

Instance Method Details

#startObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/droonga/supervisor.rb', line 31

def start
  n_ready_workers = 0
  @worker_runners = @n_workers.times.collect do |i|
    worker_runner = WorkerRunner.new(@loop, i, @config)
    worker_runner.on_ready = lambda do
      n_ready_workers += 1
      if n_ready_workers == @n_workers
        @on_ready.call if @on_ready
      end
    end
    worker_runner.start
    # TODO: support auto re-run
    worker_runner
  end
end

#stop_gracefullyObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/droonga/supervisor.rb', line 47

def stop_gracefully
  logger.trace("stop_gracefully: start")
  n_worker_runners = @worker_runners.size
  n_done_worker_runners = 0
  @worker_runners.each do |worker_runner|
    worker_runner.stop_gracefully do
      n_done_worker_runners += 1
      if n_done_worker_runners == n_worker_runners
        yield if block_given?
      end
    end
  end
  logger.trace("stop_gracefully: done")
end

#stop_immediatelyObject



62
63
64
65
66
# File 'lib/droonga/supervisor.rb', line 62

def stop_immediately
  @worker_runners.each do |worker_runner|
    worker_runner.stop_immediately
  end
end