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

Exit after a fixed number of jobs executed. #1164

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ You can then do the following:
# or to run in the foreground
RAILS_ENV=production script/delayed_job run --exit-on-complete

# Exit after a specific number of jobs, irrespective of success or failure.
script/delayed_job start --exit-after=1
env EXIT_AFTER=4 rake jobs:work

**Rails 4:** *replace script/delayed_job with bin/delayed_job*

Workers can be running on any computer, as long as they have access to the
Expand Down
3 changes: 3 additions & 0 deletions lib/delayed/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def initialize(args) # rubocop:disable MethodLength
opt.on('--daemon-options a, b, c', Array, 'options to be passed through to daemons gem') do |daemon_options|
@daemon_options = daemon_options
end
opt.on('--exit-after N', 'Exit after executing N jobs.') do |n|
@options[:exit_after] = n.to_i
end
end
@args = opts.parse!(args) + (@daemon_options || [])
end
Expand Down
1 change: 1 addition & 0 deletions lib/delayed/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

@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[:exit_after] = ENV['EXIT_AFTER'].to_i if ENV['EXIT_AFTER']
end

desc "Exit with error status if any jobs older than max_age seconds haven't been attempted yet."
Expand Down
8 changes: 6 additions & 2 deletions lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Worker # rubocop:disable ClassLength
cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time,
:default_priority, :sleep_delay, :logger, :delay_jobs, :queues,
:read_ahead, :plugins, :destroy_failed_jobs, :exit_on_complete,
:default_log_level
:default_log_level, :exit_after

# Named queue into which jobs are enqueued by default
cattr_accessor :default_queue_name
Expand Down Expand Up @@ -132,7 +132,7 @@ def initialize(options = {})
@quiet = options.key?(:quiet) ? options[:quiet] : true
@failed_reserve_count = 0

[:min_priority, :max_priority, :sleep_delay, :read_ahead, :queues, :exit_on_complete].each do |option|
[:min_priority, :max_priority, :sleep_delay, :read_ahead, :queues, :exit_on_complete, :exit_after].each do |option|
self.class.send("#{option}=", options[option]) if options.key?(option)
end

Expand Down Expand Up @@ -191,6 +191,7 @@ def start # rubocop:disable CyclomaticComplexity, PerceivedComplexity
say format("#{count} jobs processed at %.4f j/s, %d failed", count / @realtime, @result.last)
end

break if self.class.exit_after and @total_jobs_run >= self.class.exit_after
break if stop?
end
end
Expand All @@ -209,6 +210,7 @@ def stop?
def work_off(num = 100)
success = 0
failure = 0
@total_jobs_run ||= 0

num.times do
case reserve_and_run_one_job
Expand All @@ -219,6 +221,8 @@ def work_off(num = 100)
else
break # leave if no work could be done
end
@total_jobs_run += 1
break if self.class.exit_after and @total_jobs_run >= self.class.exit_after
break if stop? # leave if we're exiting
end

Expand Down