Skip to content

Commit 7b9f3cc

Browse files
committedNov 17, 2023
Add a Puma plugin for Solid Queue
This allows you to run Solid Queue as part of a puma cluster, by adding ``` plugin :solid_queue ``` to your Puma config. It will boot up the Solid Queue supervisor, leaving it to manage the scheduler and worker processes as normal. If either Puma or the Solid Cache supervisor dies, the other will notice and shut itself down. On puma shutdown, it will signal Solid Queue to also shutdown.
1 parent a471237 commit 7b9f3cc

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
 

‎lib/puma/plugin/solid_queue.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require "puma/plugin"
2+
3+
Puma::Plugin.create do
4+
attr_reader :puma_pid, :solid_queue_pid, :log_writer
5+
6+
def start(launcher)
7+
@log_writer = launcher.log_writer
8+
@puma_pid = $$
9+
@solid_queue_pid = fork do
10+
Thread.new { monitor_puma }
11+
SolidQueue::Supervisor.start(mode: :all)
12+
end
13+
14+
launcher.events.on_stopped { stop_solid_queue }
15+
16+
in_background do
17+
monitor_solid_queue
18+
end
19+
end
20+
21+
private
22+
def stop_solid_queue
23+
Process.waitpid(solid_queue_pid, Process::WNOHANG)
24+
log "Stopping Solid Queue..."
25+
Process.kill(:INT, solid_queue_pid) if solid_queue_pid
26+
Process.wait(solid_queue_pid)
27+
rescue Errno::ECHILD, Errno::ESRCH
28+
end
29+
30+
def monitor_puma
31+
monitor(:puma_dead?, "Detected Puma has gone away, stopping Solid Queue...")
32+
end
33+
34+
def monitor_solid_queue
35+
monitor(:solid_queue_dead?, "Detected Solid Queue has gone away, stopping Puma...")
36+
end
37+
38+
def monitor(process_dead, message)
39+
loop do
40+
if send(process_dead)
41+
log message
42+
Process.kill(:INT, $$)
43+
break
44+
end
45+
sleep 2
46+
end
47+
end
48+
49+
def solid_queue_dead?
50+
Process.waitpid(solid_queue_pid, Process::WNOHANG)
51+
false
52+
rescue Errno::ECHILD, Errno::ESRCH
53+
true
54+
end
55+
56+
def puma_dead?
57+
Process.ppid != puma_pid
58+
end
59+
60+
def log(...)
61+
log_writer.log(...)
62+
end
63+
end

0 commit comments

Comments
 (0)
Please sign in to comment.