Class: Chore::Strategy::WorkerManager

Inherits:
Object
  • Object
show all
Includes:
Ipc
Defined in:
lib/chore/strategies/worker/helpers/worker_manager.rb

Overview

:nodoc:

Constant Summary

Constants included from Ipc

Ipc::BIG_ENDIAN, Ipc::MSG_BYTES, Ipc::READY_MSG

Instance Method Summary collapse

Methods included from Ipc

#add_worker_socket, #child_connection, #clear_ready, #create_master_socket, #delete_socket_file, #ipc_help, #read_msg, #select_sockets, #send_msg, #signal_ready

Constructor Details

#initialize(master_socket) ⇒ WorkerManager

Returns a new instance of WorkerManager.



8
9
10
11
12
# File 'lib/chore/strategies/worker/helpers/worker_manager.rb', line 8

def initialize(master_socket)
  @master_socket = master_socket
  @pid_to_worker = {}
  @socket_to_worker = {}
end

Instance Method Details

#create_and_attach_workersObject

Create num of missing workers and sockets and attach them for the master



16
17
18
19
20
# File 'lib/chore/strategies/worker/helpers/worker_manager.rb', line 16

def create_and_attach_workers
  create_workers do |num_workers|
    attach_workers(num_workers)
  end
end

#ready_workers(sockets = []) {|workers| ... } ⇒ Object

Return the workers associated with a given array of sockets.

block

A block can be provided to perform tasks on the workers

associated with the sockets given

Yields:

  • (workers)


51
52
53
54
55
# File 'lib/chore/strategies/worker/helpers/worker_manager.rb', line 51

def ready_workers(sockets = [], &block)
  workers = @socket_to_worker.values_at(*sockets)
  yield workers if block_given?
  workers
end

#respawn_terminated_workers!Object

Reap dead workers and create new ones to replace them



23
24
25
26
27
# File 'lib/chore/strategies/worker/helpers/worker_manager.rb', line 23

def respawn_terminated_workers!
  Chore.logger.info 'WM: Respawning terminated workers'
  reap_workers
  create_and_attach_workers
end

#stop_workers(sig) ⇒ Object

Stop children with the given kill signal and wait for them to die



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chore/strategies/worker/helpers/worker_manager.rb', line 30

def stop_workers(sig)
  @pid_to_worker.each do |pid, worker|
    begin
      Chore.logger.info { "WM: Sending #{sig} to: #{pid}" }
      Process.kill(sig, pid)
    rescue Errno::ESRCH => e
      Chore.logger.error "WM: Signal to children error: #{e}"
    end
  end
  # TODO: Sleep for the shutdown timeout and kill any remaining workers
  reap_workers
end

#worker_socketsObject

Return all the worker sockets



44
45
46
# File 'lib/chore/strategies/worker/helpers/worker_manager.rb', line 44

def worker_sockets
  @socket_to_worker.keys
end