Skip to content

Commit 21eb6b4

Browse files
CamJNFooBarWidget
andauthored
Reduce redundancy in #abort_start code (#19)
Co-authored-by: Hongli Lai <hongli@hongli.nl>
1 parent 6c1ea1a commit 21eb6b4

File tree

1 file changed

+25
-41
lines changed

1 file changed

+25
-41
lines changed

lib/daemon_controller.rb

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -464,27 +464,7 @@ def abort_start(pid:, is_direct_child:)
464464
begin
465465
timeoutable(@start_abort_timeout) do
466466
allow_timeout do
467-
if is_direct_child
468-
begin
469-
debug "Waiting directly for process #{pid}"
470-
Process.waitpid(pid)
471-
rescue SystemCallError
472-
end
473-
474-
# The daemon may have:
475-
# 1. Written a PID file before forking. We delete this PID file.
476-
# -OR-
477-
# 2. It might have forked (and written a PID file) right before
478-
# we terminated it. We'll want the fork to stay alive rather
479-
# than going through the (complicated) trouble of killing it.
480-
# Don't touch the PID file.
481-
pid2 = read_pid_file
482-
debug "PID file contains #{pid2.inspect}"
483-
delete_pid_file if pid == pid2
484-
else
485-
debug "Waiting until daemon is no longer running"
486-
wait_until { !daemon_is_running? }
487-
end
467+
wait_for_aborted_process(pid:, is_direct_child:)
488468
end
489469
end
490470
rescue Timeout::Error
@@ -494,28 +474,32 @@ def abort_start(pid:, is_direct_child:)
494474
end
495475

496476
allow_timeout do
497-
if is_direct_child
498-
begin
499-
debug "Waiting directly for process #{pid}"
500-
Process.waitpid(pid)
501-
rescue SystemCallError
502-
end
477+
wait_for_aborted_process(pid:, is_direct_child:)
478+
end
479+
end
480+
end
503481

504-
# The daemon may have:
505-
# 1. Written a PID file before forking. We delete this PID file.
506-
# -OR-
507-
# 2. It might have forked (and written a PID file) right before
508-
# we terminated it. We'll want the fork to stay alive rather
509-
# than going through the (complicated) trouble of killing it.
510-
# Don't touch the PID file.
511-
pid2 = read_pid_file
512-
debug "PID file contains #{pid2.inspect}"
513-
delete_pid_file if pid == pid2
514-
else
515-
debug "Waiting until daemon is no longer running"
516-
wait_until { !daemon_is_running? }
517-
end
482+
def wait_for_aborted_process(pid:, is_direct_child:)
483+
if is_direct_child
484+
begin
485+
debug "Waiting directly for process #{pid}"
486+
Process.waitpid(pid)
487+
rescue SystemCallError
518488
end
489+
490+
# The daemon may have:
491+
# 1. Written a PID file before forking. We delete this PID file.
492+
# -OR-
493+
# 2. It might have forked (and written a PID file) right before
494+
# we terminated it. We'll want the fork to stay alive rather
495+
# than going through the (complicated) trouble of killing it.
496+
# Don't touch the PID file.
497+
pid2 = read_pid_file
498+
debug "PID file contains #{pid2.inspect}"
499+
delete_pid_file if pid == pid2
500+
else
501+
debug "Waiting until daemon is no longer running"
502+
wait_until { !daemon_is_running? }
519503
end
520504
end
521505

0 commit comments

Comments
 (0)