Skip to content

Commit 5d200d0

Browse files
kolbasovrosa
authored andcommitted
Fix Supervisor monitoring in Puma's plugin
Puma fires up the background hooks before the ```on_booted``` event. Since the plugin registers the ```monitor_solid_queue``` after Puma is fully booted, it doesn't get executed. The fix is to move the ```monitor_solid_queue``` out of the ```on_booted``` and check whether the SolidQueue has started in the ```solid_queue_dead?```.
1 parent 90f4e13 commit 5d200d0

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/puma/plugin/solid_queue.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ def start(launcher)
77
@log_writer = launcher.log_writer
88
@puma_pid = $$
99

10+
in_background do
11+
monitor_solid_queue
12+
end
13+
1014
launcher.events.on_booted do
1115
@solid_queue_pid = fork do
1216
Thread.new { monitor_puma }
1317
SolidQueue::Supervisor.start(mode: :all)
1418
end
15-
16-
in_background do
17-
monitor_solid_queue
18-
end
1919
end
2020

2121
launcher.events.on_stopped { stop_solid_queue }
@@ -51,12 +51,18 @@ def monitor(process_dead, message)
5151
end
5252

5353
def solid_queue_dead?
54-
Process.waitpid(solid_queue_pid, Process::WNOHANG)
54+
if solid_queue_started?
55+
Process.waitpid(solid_queue_pid, Process::WNOHANG)
56+
end
5557
false
5658
rescue Errno::ECHILD, Errno::ESRCH
5759
true
5860
end
5961

62+
def solid_queue_started?
63+
solid_queue_pid.present?
64+
end
65+
6066
def puma_dead?
6167
Process.ppid != puma_pid
6268
end

test/integration/puma/plugin_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class PumaPluginTest < ActiveSupport::TestCase
2626
end
2727

2828
teardown do
29-
terminate_process(@pid, signal: :INT)
29+
terminate_process(@pid, signal: :INT) if process_exists?(@pid)
30+
3031
wait_for_registered_processes 0, timeout: 1.second
3132

3233
JobResult.delete_all
@@ -49,4 +50,13 @@ class PumaPluginTest < ActiveSupport::TestCase
4950
wait_for_jobs_to_finish_for(2.seconds)
5051
assert_equal 1, JobResult.where(queue_name: :background, status: "completed", value: :puma_plugin).count
5152
end
53+
54+
test "stop puma when solid queue's supervisor dies" do
55+
supervisor = find_processes_registered_as("Supervisor").first
56+
57+
signal_process(supervisor.pid, :KILL)
58+
wait_for_process_termination_with_timeout(@pid)
59+
60+
assert_not process_exists?(@pid)
61+
end
5262
end

0 commit comments

Comments
 (0)