Class: Tina4::Job
- Inherits:
-
Object
- Object
- Tina4::Job
- Defined in:
- lib/tina4/job.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#available_at ⇒ Object
readonly
Returns the value of attribute available_at.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#error ⇒ Object
Returns the value of attribute error.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#status ⇒ Object
Returns the value of attribute status.
-
#topic ⇒ Object
readonly
Returns the value of attribute topic.
Instance Method Summary collapse
-
#complete ⇒ Object
Mark this job as completed.
-
#fail(reason = "") ⇒ Object
Record a failed attempt.
- #increment_attempts! ⇒ Object
-
#initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0, created_at: nil, error: nil, queue: nil) ⇒ Job
constructor
A new instance of Job.
-
#reject(reason = "") ⇒ Object
Reject this job with a reason.
-
#retry(delay_seconds: 0) ⇒ Object
Re-queue this message with optional delay.
- #to_array ⇒ Object
- #to_hash ⇒ Object
- #to_json(*_args) ⇒ Object
Constructor Details
#initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0, created_at: nil, error: nil, queue: nil) ⇒ Job
Returns a new instance of Job.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tina4/job.rb', line 10 def initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0, created_at: nil, error: nil, queue: nil) @id = id || SecureRandom.uuid @topic = topic @payload = payload @created_at = created_at || Time.now @attempts = attempts @priority = priority @available_at = available_at # Populated by fail()/reject() — why the job last died. Surfaces in # dead_letters() so consumers can see the failure reason without # trawling logs. @error = error @status = :pending @queue = queue end |
Instance Attribute Details
#attempts ⇒ Object
Returns the value of attribute attempts.
8 9 10 |
# File 'lib/tina4/job.rb', line 8 def attempts @attempts end |
#available_at ⇒ Object (readonly)
Returns the value of attribute available_at.
7 8 9 |
# File 'lib/tina4/job.rb', line 7 def available_at @available_at end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
7 8 9 |
# File 'lib/tina4/job.rb', line 7 def created_at @created_at end |
#error ⇒ Object
Returns the value of attribute error.
8 9 10 |
# File 'lib/tina4/job.rb', line 8 def error @error end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/tina4/job.rb', line 7 def id @id end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
7 8 9 |
# File 'lib/tina4/job.rb', line 7 def payload @payload end |
#priority ⇒ Object (readonly)
Returns the value of attribute priority.
7 8 9 |
# File 'lib/tina4/job.rb', line 7 def priority @priority end |
#queue ⇒ Object
Returns the value of attribute queue.
8 9 10 |
# File 'lib/tina4/job.rb', line 8 def queue @queue end |
#status ⇒ Object
Returns the value of attribute status.
8 9 10 |
# File 'lib/tina4/job.rb', line 8 def status @status end |
#topic ⇒ Object (readonly)
Returns the value of attribute topic.
7 8 9 |
# File 'lib/tina4/job.rb', line 7 def topic @topic end |
Instance Method Details
#complete ⇒ Object
Mark this job as completed. Terminal — the job is done and removed.
73 74 75 76 77 |
# File 'lib/tina4/job.rb', line 73 def complete @status = :completed @queue.backend.complete(self) if @queue && @queue.backend.respond_to?(:complete) self end |
#fail(reason = "") ⇒ Object
Record a failed attempt.
Increments attempts and stores reason. If the job still has retries left (+attempts < max_retries+) it is automatically re-enqueued to the pending queue, so the next pop/consume picks it up again (after the queue’s retry_backoff delay, if any). Once it has been attempted max_retries times it is moved to the dead-letter store, where queue.dead_letters returns it. No manual retry_failed is required.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tina4/job.rb', line 87 def fail(reason = "") @error = reason if @queue && @queue.backend.respond_to?(:fail) @queue.backend.fail(self, reason) @status = @attempts >= @queue.max_retries ? :dead : :pending else # No backend reference — degrade to in-memory bookkeeping only. @attempts += 1 @status = :failed end self end |
#increment_attempts! ⇒ Object
68 69 70 |
# File 'lib/tina4/job.rb', line 68 def increment_attempts! @attempts += 1 end |
#reject(reason = "") ⇒ Object
Reject this job with a reason. Alias for fail().
101 102 103 |
# File 'lib/tina4/job.rb', line 101 def reject(reason = "") fail(reason) end |
#retry(delay_seconds: 0) ⇒ Object
Re-queue this message with optional delay. Always re-enqueues regardless of the retry limit — this is a manual override, distinct from the automatic fail() path. Increments attempts.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tina4/job.rb', line 30 def retry(delay_seconds: 0) q = @queue raise ArgumentError, "No queue reference — set at construction" unless q if q.backend.respond_to?(:retry) q.backend.retry(self, delay_seconds: delay_seconds) else @attempts += 1 @status = :pending @available_at = delay_seconds > 0 ? Time.now + delay_seconds : nil q.backend.enqueue(self) end self end |
#to_array ⇒ Object
45 46 47 |
# File 'lib/tina4/job.rb', line 45 def to_array [@id, @topic, @payload, @priority, @attempts] end |
#to_hash ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/tina4/job.rb', line 49 def to_hash h = { id: @id, topic: @topic, payload: @payload, created_at: @created_at.iso8601(6), attempts: @attempts, status: @status, priority: @priority, error: @error } h[:available_at] = @available_at.iso8601 if @available_at h end |
#to_json(*_args) ⇒ Object
64 65 66 |
# File 'lib/tina4/job.rb', line 64 def to_json(*_args) JSON.generate(to_hash) end |