Class: WorkerKiller::PumaPlugin
- Inherits:
-
Object
- Object
- WorkerKiller::PumaPlugin
- Includes:
- Singleton
- Defined in:
- lib/worker_killer/puma_plugin.rb
Instance Attribute Summary collapse
-
#ipc_path ⇒ Object
Returns the value of attribute ipc_path.
-
#killer ⇒ Object
Returns the value of attribute killer.
-
#thread ⇒ Object
Returns the value of attribute thread.
Instance Method Summary collapse
- #config(puma) ⇒ Object
- #find_worker(worker_num) ⇒ Object
-
#initialize ⇒ PumaPlugin
constructor
A new instance of PumaPlugin.
- #log(msg) ⇒ Object
- #start(launcher) ⇒ Object
- #start_ipc_listener ⇒ Object
Constructor Details
#initialize ⇒ PumaPlugin
Returns a new instance of PumaPlugin.
14 15 16 17 18 |
# File 'lib/worker_killer/puma_plugin.rb', line 14 def initialize @ipc_path = File.join('tmp', "puma_worker_killer_#{Process.pid}.socket") @killer = ::WorkerKiller::Killer::Puma.new(worker_num: nil, ipc_path: ipc_path) log "Initializing IPC: #{@ipc_path}" end |
Instance Attribute Details
#ipc_path ⇒ Object
Returns the value of attribute ipc_path.
12 13 14 |
# File 'lib/worker_killer/puma_plugin.rb', line 12 def ipc_path @ipc_path end |
#killer ⇒ Object
Returns the value of attribute killer.
12 13 14 |
# File 'lib/worker_killer/puma_plugin.rb', line 12 def killer @killer end |
#thread ⇒ Object
Returns the value of attribute thread.
12 13 14 |
# File 'lib/worker_killer/puma_plugin.rb', line 12 def thread @thread end |
Instance Method Details
#config(puma) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/worker_killer/puma_plugin.rb', line 20 def config(puma) puma.on_worker_boot do |num| log "Set worker_num: #{num}" @killer.worker_num = num end end |
#find_worker(worker_num) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/worker_killer/puma_plugin.rb', line 54 def find_worker(worker_num) worker = @runner.worker_at(worker_num) unless worker log "Unknown worker index: #{worker_num.inspect}. Skipping." return nil end unless worker.booted? log "Worker #{worker_num.inspect} is not booted yet. Skipping." return nil end if worker.term? log "Worker #{worker_num.inspect} already terminating. Skipping." return nil end worker end |
#log(msg) ⇒ Object
74 75 76 |
# File 'lib/worker_killer/puma_plugin.rb', line 74 def log(msg) warn("#{self.class}[#{Process.pid}]: #{msg}") end |
#start(launcher) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/worker_killer/puma_plugin.rb', line 27 def start(launcher) @runner = launcher.instance_variable_get('@runner') launcher.events.on_booted do @thread ||= start_ipc_listener end end |
#start_ipc_listener ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/worker_killer/puma_plugin.rb', line 35 def start_ipc_listener log 'Start IPC listener' Thread.new do Socket.unix_server_loop(ipc_path) do |sock, *args| if (line = sock.gets) worker_num = Integer(line.strip) if (worker = find_worker(worker_num)) log "Killing worker #{worker_num}" worker.term! end end rescue StandardError => e log("Exception: #{e.inspect}") ensure sock.close end end end |