Skip to content

Commit

Permalink
Fix reading configuration from environment for boolean values (#735)
Browse files Browse the repository at this point in the history
* Fix reading configuration from environment for boolean values

* Improve description of change

Co-authored-by: Michael Bianco <[email protected]>
  • Loading branch information
carsonreinke and iloveitaly authored Jan 31, 2022
1 parent 5ddc78b commit bfbc753
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## [Unreleased]
### Changed
- Remove support for Ruby < 2.3
- Configuration to use a set of truthy values to enable boolean settings instead of simply existence
- Add `delay_or_enqueue_at` for delaying existing jobs or creating a new job

## [4.5.0] - 2021-09-25
Expand Down
39 changes: 31 additions & 8 deletions lib/resque/scheduler/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,61 @@ def configure
yield self
end

attr_writer :environment
def environment
@environment ||= ENV
end

# Used in `#load_schedule_job`
attr_writer :env

def env
return @env if @env
@env ||= Rails.env if defined?(Rails) && Rails.respond_to?(:env)
@env ||= ENV['RAILS_ENV']
@env ||= environment['RAILS_ENV']
@env
end

# If true, logs more stuff...
attr_writer :verbose

def verbose
@verbose ||= !!ENV['VERBOSE']
@verbose ||= to_bool(environment['VERBOSE'])
end

# If set, produces no output
attr_writer :quiet

def quiet
@quiet ||= !!ENV['QUIET']
@quiet ||= to_bool(environment['QUIET'])
end

# If set, will write messages to the file
attr_writer :logfile

def logfile
@logfile ||= ENV['LOGFILE']
@logfile ||= environment['LOGFILE']
end

# Sets whether to log in 'text' or 'json'
attr_writer :logformat

def logformat
@logformat ||= ENV['LOGFORMAT']
@logformat ||= environment['LOGFORMAT']
end

# If set, will try to update the schedule in the loop
attr_writer :dynamic

def dynamic
@dynamic ||= !!ENV['DYNAMIC_SCHEDULE']
@dynamic ||= to_bool(environment['DYNAMIC_SCHEDULE'])
end

# If set, will append the app name to procline
attr_writer :app_name

def app_name
@app_name ||= ENV['APP_NAME']
@app_name ||= environment['APP_NAME']
end

# Amount of time in seconds to sleep between polls of the delayed
Expand All @@ -66,7 +71,25 @@ def app_name

def poll_sleep_amount
@poll_sleep_amount ||=
Float(ENV.fetch('RESQUE_SCHEDULER_INTERVAL', '5'))
Float(environment.fetch('RESQUE_SCHEDULER_INTERVAL', '5'))
end

private

# Copied from https://github.com/rails/rails/blob/main/activemodel/lib/active_model/type/boolean.rb#L17
TRUE_VALUES = [
true, 1,
'1', :'1',
't', :t,
'T', :T,
'true', :true,
'TRUE', :TRUE,
'on', :on,
'ON', :ON
].to_set.freeze

def to_bool(value)
TRUE_VALUES.include?(value)
end
end
end
Expand Down
41 changes: 41 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# vim:fileencoding=utf-8
require_relative 'test_helper'

context 'Configuration' do
%w(VERBOSE QUIET DYNAMIC_SCHEDULE).each do |setting|
method = setting.downcase.to_sym
method = :dynamic if setting == 'DYNAMIC_SCHEDULE'

test "enabling #{method} from environment" do
configuration.environment = { setting => 'true' }

assert configuration.send(method)
end

test "disabling #{method} from environment" do
configuration.environment = { setting => 'false' }

assert !configuration.send(method)
end
end

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

assert_equal 'development', configuration.env
end

test 'env set from environment' do
configuration.environment = { 'RAILS_ENV' => 'development' }

assert_equal 'development', configuration.env
end

private

def configuration
@configuration ||= Module.new do
extend Resque::Scheduler::Configuration
end
end
end

0 comments on commit bfbc753

Please sign in to comment.