Skip to content

Commit

Permalink
Simplify ready execution claiming, passing only job_ids
Browse files Browse the repository at this point in the history
We no longer need to pass executions, as that was a change to
prepare for an implementation of sequential jobs that I won't
be pursuing. We can simply pluck the job_ids of the selected
executions, which as a side-effect, avoids an extra query due
to counting the jobs before actually trying to claim them.

Thanks to @djmb for spotting the extra `exists?` query that
was a consequence of this change.
  • Loading branch information
rosa committed Nov 7, 2023
1 parent 2b86b44 commit c8caf88
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions app/models/solid_queue/claimed_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ def success?
end
end

CLAIM_ATTRIBUTES = %w[ job_id ]

class << self
def claiming(executions, process_id, &block)
job_data = Array(executions).collect { |execution| { job_id: execution.job_id, process_id: process_id } }
def claiming(job_ids, process_id, &block)
job_data = Array(job_ids).collect { |job_id| { job_id: job_id, process_id: process_id } }

insert_all(job_data)
where(job_id: job_data.map { |data| data[:job_id]} ).tap do |claimed|
where(job_id: job_ids).tap do |claimed|
block.call(claimed)
SolidQueue.logger.info("[SolidQueue] Claimed #{claimed.size} jobs")
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/solid_queue/ready_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def queued_as(queues)

private
def select_candidates(queues, limit)
queued_as(queues).not_paused.ordered.limit(limit).lock("FOR UPDATE SKIP LOCKED")
queued_as(queues).not_paused.ordered.limit(limit).lock("FOR UPDATE SKIP LOCKED").pluck(:job_id)
end

def lock(candidates, process_id)
Expand All @@ -32,7 +32,7 @@ def lock(candidates, process_id)

def claim(process_id)
transaction do
SolidQueue::ClaimedExecution.claiming(self, process_id) do |claimed|
SolidQueue::ClaimedExecution.claiming(job_id, process_id) do |claimed|
delete if claimed.one?
end
end
Expand Down

0 comments on commit c8caf88

Please sign in to comment.