Class: Disqualified::Record
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- Disqualified::Record
- Defined in:
- app/models/disqualified/record.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.claim_one!(id: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/disqualified/record.rb', line 17 def self.claim_one!(id: nil) run_id = SecureRandom.uuid association = Disqualified::Record .runnable .order(run_at: :asc) .limit(1) if id association = association.where(id:) end claimed_count = association.update_all( locked_by: run_id, locked_at: Time.now, updated_at: Time.now, attempts: Arel.sql("attempts + 1") ) raise Disqualified::Error::NoClaimableJob if claimed_count == 0 Disqualified::Record.find_by!(locked_by: run_id) rescue ActiveRecord::RecordNotFound raise Disqualified::Error::NoClaimableJob end |
Instance Method Details
#finish ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/models/disqualified/record.rb', line 56 def finish transaction do update!(locked_by: nil, locked_at: nil, finished_at: Time.now) if sequence_uuid && sequence_step Disqualified::SequenceRecord .where(uuid: sequence_uuid, current_step: sequence_step) .update_all( current_step: sequence_step + 1, updated_at: Time.now ) end end end |
#requeue ⇒ Object
70 71 72 73 74 |
# File 'app/models/disqualified/record.rb', line 70 def requeue retry_count = attempts - 1 sleep = (retry_count**4) + 15 + (rand(10) * (retry_count + 1)) unclaim(next_run_at: Time.now + sleep) end |
#run! ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/disqualified/record.rb', line 43 def run! record = self.class.claim_one!(id:) begin record.send(:instantiate_handler_and_perform_with_args) rescue => e record.unclaim raise e else record.finish end record end |
#unclaim(next_run_at: nil) ⇒ Object
76 77 78 79 80 81 82 |
# File 'app/models/disqualified/record.rb', line 76 def unclaim(next_run_at: nil) if next_run_at update!(locked_by: nil, locked_at: nil, run_at: next_run_at) else update!(locked_by: nil, locked_at: nil) end end |