diff --git a/lib/resque/scheduler/cli.rb b/lib/resque/scheduler/cli.rb index c0d7fea9..818d3809 100644 --- a/lib/resque/scheduler/cli.rb +++ b/lib/resque/scheduler/cli.rb @@ -15,7 +15,8 @@ module Scheduler quiet: 'QUIET', pidfile: 'PIDFILE', poll_sleep_amount: 'RESQUE_SCHEDULER_INTERVAL', - verbose: 'VERBOSE' + verbose: 'VERBOSE', + timeout: 'TIMEOUT' }.freeze class Cli diff --git a/lib/resque/scheduler/configuration.rb b/lib/resque/scheduler/configuration.rb index f9ac54f6..8ab61569 100644 --- a/lib/resque/scheduler/configuration.rb +++ b/lib/resque/scheduler/configuration.rb @@ -80,6 +80,13 @@ def poll_sleep_amount Float(environment.fetch('RESQUE_SCHEDULER_INTERVAL', '5')) end + # Sets timeout for Resque::Scheduler::Lock::Base + attr_writer :lock_timeout + + def lock_timeout + @lock_timeout ||= environment.fetch('LOCK_TIMEOUT', 60 * 3).to_i + end + private # Copied from https://github.com/rails/rails/blob/main/activemodel/lib/active_model/type/boolean.rb#L17 diff --git a/lib/resque/scheduler/env.rb b/lib/resque/scheduler/env.rb index 150e22a2..cd309c11 100644 --- a/lib/resque/scheduler/env.rb +++ b/lib/resque/scheduler/env.rb @@ -56,21 +56,21 @@ def setup_pid_file def setup_scheduler_configuration Resque::Scheduler.configure do |c| - c.app_name = options[:app_name] if options.key?(:app_name) - - c.dynamic = !!options[:dynamic] if options.key?(:dynamic) - - c.env = options[:env] if options.key?(:env) - - c.logfile = options[:logfile] if options.key?(:logfile) - - c.logformat = options[:logformat] if options.key?(:logformat) + [ + :app_name, + :dynamic, + :env, + :logfile, + :logformat, + :lock_timeout, + :verbose + ].each do |option_key| + c.send("#{option_key}=", options[option_key]) if options.key?(option_key) + end if (psleep = options[:poll_sleep_amount]) && !psleep.nil? c.poll_sleep_amount = Float(psleep) end - - c.verbose = !!options[:verbose] if options.key?(:verbose) end end diff --git a/lib/resque/scheduler/lock/base.rb b/lib/resque/scheduler/lock/base.rb index 164c2f9f..fe231993 100644 --- a/lib/resque/scheduler/lock/base.rb +++ b/lib/resque/scheduler/lock/base.rb @@ -11,7 +11,7 @@ def initialize(key, options = {}) @key = key # 3 minute default timeout - @timeout = options[:timeout] || 60 * 3 + @timeout = options[:timeout] || Resque::Scheduler.lock_timeout end # Attempts to acquire the lock. Returns true if successfully acquired. diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 2abed87f..2bcbe4f0 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -19,6 +19,12 @@ end end + test 'setting lock_timeout from environment' do + configuration.environment = { 'LOCK_TIMEOUT' => '47' } + + assert_equal 47, configuration.lock_timeout + end + test 'env set from Rails.env' do Rails.expects(:env).returns('development')