Class: Cuetip::Models::QueuedJob
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cuetip::Models::QueuedJob
- Defined in:
- lib/cuetip/models/queued_job.rb
Constant Summary collapse
- PROCESS_IDENTIFIER =
Socket.gethostname + ":#{Process.pid}"
Class Method Summary collapse
-
.find_and_lock(queued_job_id = nil) ⇒ Object
Simultaneously find an outstanding job and lock it.
-
.generate_lock_id ⇒ Object
Generate a random lock ID to use in the locking process.
Instance Method Summary collapse
-
#requeue(attributes = {}) ⇒ Object
Unlock the job and allow it to be re-run elsewhere.
Class Method Details
.find_and_lock(queued_job_id = nil) ⇒ Object
Simultaneously find an outstanding job and lock it
33 34 35 36 37 38 39 40 41 42 |
# 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 ⇒ Object
Generate a random lock ID to use in the locking process
28 29 30 |
# 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 |
Instance Method Details
#requeue(attributes = {}) ⇒ Object
Unlock the job and allow it to be re-run elsewhere.
19 20 21 22 23 24 25 |
# 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 |