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 df7ef73
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 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 :timeout

def timeout
@timeout ||= environment.fetch('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
2 changes: 2 additions & 0 deletions lib/resque/scheduler/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def setup_scheduler_configuration

c.logformat = options[:logformat] if options.key?(:logformat)

c.timeout = options[:timeout].to_i if options.key?(:timeout)

if (psleep = options[:poll_sleep_amount]) && !psleep.nil?
c.poll_sleep_amount = Float(psleep)
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.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 timeout from environment' do
configuration.environment = { 'TIMEOUT' => '47' }

assert_equal 47, configuration.timeout
end

test 'env set from Rails.env' do
Rails.expects(:env).returns('development')

Expand Down

0 comments on commit df7ef73

Please sign in to comment.