Skip to content

Commit

Permalink
Namespace configuration env vars with backwards compatibility
Browse files Browse the repository at this point in the history
Namespace all configuration variables used by Delayed::Job under the
`DELAYED_JOB_`-prefix. This will allow developers to easily figure out
what the env var is used for at a glance and prevent collisions with
other libraries using similarly named variable names.

Allow non-namespaced configuration variables as a fallback, but with a
deprecation warning.

See #1028
  • Loading branch information
tonyta committed Aug 22, 2018
1 parent 73bd1b5 commit c8dd3ea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
master (unreleased)
===================
* Allow namespaced configuration env vars with `DELAYED_JOB_`-prefix
* Deprecate non-namespaced configuration env vars

4.1.5 - 2018-04-13
=================
* Allow Rails 5.2
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ cancel the rake task with `CTRL-C`.

If you want to just run all available jobs and exit you can use `rake jobs:workoff`

Work off queues by setting the `QUEUE` or `QUEUES` environment variable.
Work off queues by setting the `DELAYED_JOB_QUEUE` or `DELAYED_JOB_QUEUES`
environment variable.

QUEUE=tracking rake jobs:work
QUEUES=mailers,tasks rake jobs:work
DELAYED_JOB_QUEUE=tracking rake jobs:work
DELAYED_JOB_QUEUES=mailers,tasks rake jobs:work

Restarting delayed_job
======================
Expand Down
22 changes: 16 additions & 6 deletions lib/delayed/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@
end

task :environment_options => :environment do
def read_env(name)
return ::ENV[name] if ::ENV.key?(name)

deprecated_name = name[/DELAYED_JOB_(.+)/, 1]
if ::ENV.key?(deprecated_name)
warn "[DEPRECATION] '#{deprecated_name}' for configuration is deprecated. Use '#{name}'"
::ENV[deprecated_name]
end
end

@worker_options = {
:min_priority => ENV['MIN_PRIORITY'],
:max_priority => ENV['MAX_PRIORITY'],
:queues => (ENV['QUEUES'] || ENV['QUEUE'] || '').split(','),
:quiet => ENV['QUIET']
:min_priority => read_env('DELAYED_JOB_MIN_PRIORITY'),
:max_priority => read_env('DELAYED_JOB_MAX_PRIORITY'),
:queues => (read_env('DELAYED_JOB_QUEUES') || read_env('DELAYED_JOB_QUEUE') || '').split(','),
:quiet => read_env('DELAYED_JOB_QUIET')
}

@worker_options[:sleep_delay] = ENV['SLEEP_DELAY'].to_i if ENV['SLEEP_DELAY']
@worker_options[:read_ahead] = ENV['READ_AHEAD'].to_i if ENV['READ_AHEAD']
@worker_options[:sleep_delay] = read_env('DELAYED_JOB_SLEEP_DELAY').to_i if read_env('DELAYED_JOB_SLEEP_DELAY')
@worker_options[:read_ahead] = read_env('DELAYED_JOB_READ_AHEAD').to_i if read_env('DELAYED_JOB_READ_AHEAD')
end

desc "Exit with error status if any jobs older than max_age seconds haven't been attempted yet."
Expand Down

0 comments on commit c8dd3ea

Please sign in to comment.