Skip to content

Commit

Permalink
A touch of cleanup to the default sql method
Browse files Browse the repository at this point in the history
  • Loading branch information
albus522 committed Nov 8, 2019
1 parent 8891789 commit ceb5b07
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/delayed/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,13 @@ def self.reserve_with_scope_using_optimized_sql(ready_scope, worker, now)

def self.reserve_with_scope_using_default_sql(ready_scope, worker, now)
# This is our old fashion, tried and true, but possibly slower lookup
# Instead of reading the entire job record for our detect loop, just pluck the ID,
# and only read the job record after we've successfully locked the job.
# This can be particularly helpful when operating a large job cluster that uses
# a large read_ahead value to increase the odds of successfully locking a job with
# one method call to reserve_with_scope.
locked_job = ready_scope.limit(worker.read_ahead).select(:id).detect do |job|
# Instead of reading the entire job record for our detect loop, we select only the id,
# and only read the full job record after we've successfully locked the job.
# This can have a noticable impact on large read_ahead configurations and large payload jobs.
ready_scope.limit(worker.read_ahead).select(:id).detect do |job|
count = ready_scope.where(id: job.id).update_all(locked_at: now, locked_by: worker.name)
count == 1
count == 1 && job.reload
end

locked_job.reload if locked_job
end

def self.reserve_with_scope_using_optimized_postgres(ready_scope, worker, now)
Expand Down

0 comments on commit ceb5b07

Please sign in to comment.