class Cuetip::Models::QueuedJob

Constants

PROCESS_IDENTIFIER

Public Class Methods

find_and_lock(queued_job_id = nil) click to toggle source

Simultaneously find an outstanding job and lock it

# File lib/cuetip/models/queued_job.rb, line 33
def self.find_and_lock(queued_job_id = nil)
  lock_id = generate_lock_id
  scope = if queued_job_id
            where(id: queued_job_id)
          else
            self
          end
  count = scope.pending.limit(1).update_all(locked_by: lock_id, locked_at: Time.now)
  QueuedJob.find_by_locked_by(lock_id) if count > 0
end
generate_lock_id() click to toggle source

Generate a random lock ID to use in the locking process

# File lib/cuetip/models/queued_job.rb, line 28
def self.generate_lock_id
  PROCESS_IDENTIFIER + ':' + rand(1_000_000_000).to_s.rjust(9, '0')
end

Public Instance Methods

requeue(attributes = {}) click to toggle source

Unlock the job and allow it to be re-run elsewhere.

# File lib/cuetip/models/queued_job.rb, line 19
def requeue(attributes = {})
  self.attributes = attributes
  self.locked_by = nil
  self.locked_at = nil
  save!
  self
end