Skip to content

Commit 94de443

Browse files
pmm4654PatrickTulskie
authored andcommitted
Expose timeout for lock via environment variable configuration
1 parent f43e09a commit 94de443

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

lib/resque/scheduler/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module Scheduler
1515
quiet: 'QUIET',
1616
pidfile: 'PIDFILE',
1717
poll_sleep_amount: 'RESQUE_SCHEDULER_INTERVAL',
18-
verbose: 'VERBOSE'
18+
verbose: 'VERBOSE',
19+
lock_timeout: 'LOCK_TIMEOUT'
1920
}.freeze
2021

2122
class Cli

lib/resque/scheduler/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ def poll_sleep_amount
8080
Float(environment.fetch('RESQUE_SCHEDULER_INTERVAL', '5'))
8181
end
8282

83+
# Sets timeout for Resque::Scheduler::Lock::Base
84+
attr_writer :lock_timeout
85+
86+
def lock_timeout
87+
@lock_timeout ||= environment.fetch('LOCK_TIMEOUT', 60 * 3).to_i
88+
end
89+
8390
private
8491

8592
# Copied from https://github.com/rails/rails/blob/main/activemodel/lib/active_model/type/boolean.rb#L17

lib/resque/scheduler/env.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def setup_pid_file
5454
at_exit { cleanup_pid_file }
5555
end
5656

57+
# rubocop:disable Metrics/AbcSize
5758
def setup_scheduler_configuration
5859
Resque::Scheduler.configure do |c|
5960
c.app_name = options[:app_name] if options.key?(:app_name)
@@ -66,13 +67,16 @@ def setup_scheduler_configuration
6667

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

70+
c.lock_timeout = options[:lock_timeout] if options.key?(:lock_timeout)
71+
6972
if (psleep = options[:poll_sleep_amount]) && !psleep.nil?
7073
c.poll_sleep_amount = Float(psleep)
7174
end
7275

7376
c.verbose = !!options[:verbose] if options.key?(:verbose)
7477
end
7578
end
79+
# rubocop:enable Metrics/AbcSize
7680

7781
def cleanup_pid_file
7882
return unless pidfile_path

lib/resque/scheduler/lock/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(key, options = {})
1111
@key = key
1212

1313
# 3 minute default timeout
14-
@timeout = options[:timeout] || 60 * 3
14+
@timeout = options[:timeout] || Resque::Scheduler.lock_timeout
1515
end
1616

1717
# Attempts to acquire the lock. Returns true if successfully acquired.

test/configuration_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
end
2020
end
2121

22+
test 'setting lock_timeout from environment' do
23+
configuration.environment = { 'LOCK_TIMEOUT' => '47' }
24+
25+
assert_equal 47, configuration.lock_timeout
26+
end
27+
2228
test 'env set from Rails.env' do
2329
Rails.expects(:env).returns('development')
2430

0 commit comments

Comments
 (0)