Class: Tina4::Job
- Inherits:
-
Object
- Object
- Tina4::Job
- Defined in:
- lib/tina4/job.rb
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
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
readonly
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.
-
#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
Mark this job as failed with a reason.
- #increment_attempts! ⇒ Object
-
#initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0) ⇒ Job
constructor
A new instance of Job.
-
#reject(reason = "") ⇒ Object
Reject this job with a reason.
-
#retry(queue:, delay_seconds: 0) ⇒ Object
Re-queue this message with incremented attempts.
- #to_array ⇒ Object
- #to_hash ⇒ Object
- #to_json(*_args) ⇒ Object
Constructor Details
#initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0) ⇒ Job
Returns a new instance of Job.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tina4/job.rb', line 10 def initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0) @id = id || SecureRandom.uuid @topic = topic @payload = payload @created_at = Time.now @attempts = attempts @priority = priority @available_at = available_at @status = :pending end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
7 8 9 |
# File 'lib/tina4/job.rb', line 7 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 (readonly)
Returns the value of attribute error.
74 75 76 |
# File 'lib/tina4/job.rb', line 74 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 |
#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.
58 59 60 |
# File 'lib/tina4/job.rb', line 58 def complete @status = :completed end |
#fail(reason = "") ⇒ Object
Mark this job as failed with a reason.
63 64 65 66 67 |
# File 'lib/tina4/job.rb', line 63 def fail(reason = "") @status = :failed @error = reason @attempts += 1 end |
#increment_attempts! ⇒ Object
53 54 55 |
# File 'lib/tina4/job.rb', line 53 def increment_attempts! @attempts += 1 end |
#reject(reason = "") ⇒ Object
Reject this job with a reason. Alias for fail().
70 71 72 |
# File 'lib/tina4/job.rb', line 70 def reject(reason = "") fail(reason) end |
#retry(queue:, delay_seconds: 0) ⇒ Object
Re-queue this message with incremented attempts. Delegates to the queue’s backend via the queue reference.
23 24 25 26 27 28 29 |
# File 'lib/tina4/job.rb', line 23 def retry(queue:, delay_seconds: 0) @attempts += 1 @status = :pending @available_at = delay_seconds > 0 ? Time.now + delay_seconds : nil queue.backend.enqueue(self) self end |
#to_array ⇒ Object
31 32 33 |
# File 'lib/tina4/job.rb', line 31 def to_array [@id, @topic, @payload, @priority, @attempts] end |
#to_hash ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tina4/job.rb', line 35 def to_hash h = { id: @id, topic: @topic, payload: @payload, created_at: @created_at.iso8601, attempts: @attempts, status: @status, priority: @priority } h[:available_at] = @available_at.iso8601 if @available_at h end |
#to_json(*_args) ⇒ Object
49 50 51 |
# File 'lib/tina4/job.rb', line 49 def to_json(*_args) JSON.generate(to_hash) end |