Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose timeout for lock via environment variable configuration #786

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just leave this and add the new timeout option?

[
: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