lib/good_job/cli.rb in good_job-3.20.0 vs lib/good_job/cli.rb in good_job-3.21.0
- old
+ new
@@ -15,10 +15,13 @@
class CLI < Thor
# Path to the local Rails application's environment configuration.
# Requiring this loads the application's configuration and classes.
RAILS_ENVIRONMENT_RB = File.expand_path("config/environment.rb")
+ # Number of seconds between checking shutdown conditions
+ SHUTDOWN_EVENT_TIMEOUT = 10
+
class << self
# Whether the CLI is running from the executable
# @return [Boolean, nil]
attr_accessor :within_exe
alias within_exe? within_exe
@@ -104,17 +107,18 @@
if configuration.probe_port
probe_server = GoodJob::ProbeServer.new(port: configuration.probe_port)
probe_server.start
end
- @stop_good_job_executable = false
+ require 'concurrent/atomic/event'
+ @stop_good_job_executable = Concurrent::Event.new
%w[INT TERM].each do |signal|
- trap(signal) { @stop_good_job_executable = true }
+ trap(signal) { Thread.new { @stop_good_job_executable.set }.join }
end
Kernel.loop do
- sleep 0.1
- break if @stop_good_job_executable || capsule.shutdown?
+ @stop_good_job_executable.wait(SHUTDOWN_EVENT_TIMEOUT)
+ break if @stop_good_job_executable.set? || capsule.shutdown?
end
systemd.stop do
capsule.shutdown(timeout: configuration.shutdown_timeout)
probe_server&.stop