lib/good_job/cli.rb in good_job-3.21.2 vs lib/good_job/cli.rb in good_job-3.21.3

- old
+ new

@@ -18,10 +18,13 @@ RAILS_ENVIRONMENT_RB = File.expand_path("config/environment.rb") # Number of seconds between checking shutdown conditions SHUTDOWN_EVENT_TIMEOUT = 10 + # Number of seconds between checking shutdown conditions when idle-timeout is enabled + SHUTDOWN_EVENT_TIMEOUT_FOR_IDLE_TIMEOUT = 1 + class << self # Whether the CLI is running from the executable # @return [Boolean, nil] attr_accessor :within_exe alias within_exe? within_exe @@ -72,10 +75,14 @@ desc: "Maximum number of scheduled jobs to cache in memory (env var: GOOD_JOB_MAX_CACHE, default: 10000)" method_option :shutdown_timeout, type: :numeric, banner: 'SECONDS', desc: "Number of seconds to wait for jobs to finish when shutting down before stopping the thread. (env var: GOOD_JOB_SHUTDOWN_TIMEOUT, default: -1 (forever))" + method_option :idle_timeout, + type: :numeric, + banner: 'SECONDS', + desc: 'Exit process when no jobs have been performed for this many seconds (env var: GOOD_JOB_IDLE_TIMEOUT, default: nil)' method_option :enable_cron, type: :boolean, desc: "Whether to run cron process (default: false)" method_option :daemonize, type: :boolean, @@ -113,12 +120,13 @@ @stop_good_job_executable = Concurrent::Event.new %w[INT TERM].each do |signal| trap(signal) { Thread.new { @stop_good_job_executable.set }.join } end + loop_wait = configuration.idle_timeout ? SHUTDOWN_EVENT_TIMEOUT_FOR_IDLE_TIMEOUT : SHUTDOWN_EVENT_TIMEOUT Kernel.loop do - @stop_good_job_executable.wait(SHUTDOWN_EVENT_TIMEOUT) - break if @stop_good_job_executable.set? || capsule.shutdown? + @stop_good_job_executable.wait(loop_wait) + break if @stop_good_job_executable.set? || capsule.shutdown? || (configuration.idle_timeout && capsule.idle?(configuration.idle_timeout)) end systemd.stop do capsule.shutdown(timeout: configuration.shutdown_timeout) probe_server&.stop