From 97e288ccf99c05c049ba4d407e51c97d7784c0b0 Mon Sep 17 00:00:00 2001 From: Joshua Pinter Date: Thu, 9 May 2019 10:00:31 -0600 Subject: [PATCH] Add option to exclude locked jobs (i.e. currently being processed) in jobs:check. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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`. --- lib/delayed/tasks.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/delayed/tasks.rb b/lib/delayed/tasks.rb index 409ba48f8..dd8f14899 100644 --- a/lib/delayed/tasks.rb +++ b/lib/delayed/tasks.rb @@ -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"