Skip to content

Commit

Permalink
Expose timeout for lock via environment variable configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pmm4654 committed Dec 19, 2023
1 parent 5d7a4e1 commit 02d2120
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/resque/scheduler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/resque/scheduler/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions lib/resque/scheduler/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/resque/scheduler/lock/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 02d2120

Please sign in to comment.