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 authored and PatrickTulskie committed Dec 29, 2024
1 parent f43e09a commit 94de443
Show file tree
Hide file tree
Showing 5 changed files with 20 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',
lock_timeout: 'LOCK_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
4 changes: 4 additions & 0 deletions lib/resque/scheduler/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def setup_pid_file
at_exit { cleanup_pid_file }
end

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

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

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

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
# rubocop:enable Metrics/AbcSize

def cleanup_pid_file
return unless pidfile_path
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 94de443

Please sign in to comment.