Skip to content

Commit

Permalink
Add option to exclude locked jobs (i.e. currently being processed) in…
Browse files Browse the repository at this point in the history
… jobs:check.

If a job is currently being processed, we want an option to exclude them from being reported in `jobs:check`.

`jobs:check`’s primary purpose should be to see the outstanding queue, not determining long-running jobs.

Made this option default to false so no breaking changes but simply pass true for `not_locked` to exclude them from this check.

Remove extra `AND`.
  • Loading branch information
joshuapinter committed Dec 1, 2020
1 parent 5ac5ade commit 97e288c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/delayed/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
end

desc "Exit with error status if any jobs older than max_age seconds haven't been attempted yet."
task :check, [:max_age] => :environment do |_, args|
args.with_defaults(:max_age => 300)
task :check, [:max_age, :not_locked] => :environment do |_, args|
args.with_defaults(:max_age => 300, :not_locked => false)

unprocessed_jobs = Delayed::Job.where('attempts = 0 AND created_at < ?', Time.now - args[:max_age].to_i).count
unprocessed_jobs = Delayed::Job.where('attempts = 0 AND created_at < ?', Time.now - args[:max_age].to_i)
unprocessed_jobs = unprocessed_jobs.where('locked_at IS NULL') if args[:not_locked]
unprocessed_jobs = unprocessed_jobs.count

if unprocessed_jobs > 0
raise "#{unprocessed_jobs} jobs older than #{args[:max_age]} seconds have not been processed yet"
Expand Down

0 comments on commit 97e288c

Please sign in to comment.