Skip to content

Commit fcb5f7a

Browse files
rosaclaude
andcommitted
Name the supervisor's stop checkpoint and simplify its lifecycle
Extract the repeated drain-signals-then-decide idiom into time_to_stop? and use it from both loops that poll it, turn start's early exit into a single shutdown-or-supervise branch, and move the child's signal handler re-arming into create_fork, dropping the duplicate registration in boot: forked processes only ever boot through create_fork, so the traps are always already armed by then. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 124176a commit fcb5f7a

4 files changed

Lines changed: 37 additions & 33 deletions

File tree

‎lib/solid_queue/processes/runnable.rb‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ def run_in_mode(&block)
5050
case
5151
when running_as_fork?
5252
@boot_guard = BootGuards::ForkGuard.new
53-
# Re-arm signal handlers as the first thing in the child so a TERM
54-
# forwarded by the supervisor right after fork is not handled by the
55-
# inherited enqueue-only supervisor trap (see #755).
56-
fork do
57-
register_signal_handlers
58-
block.call
59-
end.tap { @boot_guard.start }
53+
create_fork(&block).tap { @boot_guard.start }
6054
when running_async?
6155
@boot_guard = BootGuards::NullGuard.new
6256
@thread = create_thread(&block)
@@ -70,10 +64,7 @@ def run_in_mode(&block)
7064
def boot
7165
SolidQueue.instrument(:start_process, process: self) do
7266
run_callbacks(:boot) do
73-
if running_as_fork?
74-
register_signal_handlers
75-
set_procline
76-
end
67+
set_procline if running_as_fork?
7768
end
7869
end
7970

‎lib/solid_queue/processes/supervised.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def supervised?
2525
supervisor.present?
2626
end
2727

28+
def create_fork(&block)
29+
fork do
30+
register_signal_handlers
31+
block.call
32+
end
33+
end
34+
2835
def register_signal_handlers
2936
%w[ INT TERM ].each do |signal|
3037
trap(signal) do

‎lib/solid_queue/supervisor.rb‎

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ def start
3939
run_start_hooks
4040

4141
start_processes
42-
return shutdown if stopped?
4342

44-
launch_maintenance_task
45-
46-
supervise
43+
if stopped?
44+
shutdown
45+
else
46+
launch_maintenance_task
47+
supervise
48+
end
4749
end
4850

4951
def stop
@@ -68,34 +70,32 @@ def boot
6870

6971
def start_processes
7072
configuration.configured_processes.each do |configured_process|
71-
# Honour signals that arrived during boot / start hooks before forking
72-
# any children. Otherwise a queued TERM is only handled in #supervise,
73-
# after workers have already been started (see #755).
74-
process_signal_queue if standalone?
75-
break if stopped?
73+
# Honour signals that arrive during boot or start hooks: a queued TERM
74+
# stops us here, before starting children, instead of in #supervise,
75+
# after all of them have been started
76+
break if time_to_stop?
7677

7778
start_process(configured_process)
7879
end
7980
end
8081

8182
def supervise
82-
loop do
83-
break if stopped?
84-
85-
if standalone?
86-
set_procline
87-
process_signal_queue
88-
end
89-
90-
unless stopped?
91-
check_and_replace_terminated_processes
92-
interruptible_sleep(1.second)
93-
end
83+
until time_to_stop?
84+
set_procline
85+
check_and_replace_terminated_processes
86+
interruptible_sleep(1.second)
9487
end
9588
ensure
9689
shutdown
9790
end
9891

92+
# Process any signals queued while we were busy and report whether
93+
# we've been asked to stop
94+
def time_to_stop?
95+
process_signal_queue
96+
stopped?
97+
end
98+
9999
def start_process(configured_process)
100100
process_instance = configured_process.instantiate.tap do |instance|
101101
instance.supervised_by process
@@ -149,7 +149,10 @@ def shutdown
149149
end
150150

151151
def set_procline
152-
procline "supervising #{configured_processes.keys.join(", ")}"
152+
# Embedded supervisors don't own their process's title
153+
if standalone?
154+
procline "supervising #{configured_processes.keys.join(", ")}"
155+
end
153156
end
154157

155158
def sync_std_streams

‎lib/solid_queue/supervisor/signals.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def restore_default_signal_handlers
2929
end
3030

3131
def process_signal_queue
32+
# Embedded supervisors don't own their process's signals
33+
return unless standalone?
34+
3235
while signal = signal_queue.shift
3336
handle_signal(signal)
3437
end

0 commit comments

Comments
 (0)