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, 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 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, queue: nil) ⇒ Job
Returns a new instance of Job.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tina4/job.rb', line 10 def initialize(topic:, payload:, id: nil, priority: 0, available_at: nil, attempts: 0, queue: nil) @id = id || SecureRandom.uuid @topic = topic @payload = payload @created_at = Time.now @attempts = attempts @priority = priority @available_at = available_at @status = :pending @queue = queue 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.
78 79 80 |
# File 'lib/tina4/job.rb', line 78 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.
62 63 64 |
# File 'lib/tina4/job.rb', line 62 def complete @status = :completed end |
#fail(reason = "") ⇒ Object
Mark this job as failed with a reason.
67 68 69 70 71 |
# File 'lib/tina4/job.rb', line 67 def fail(reason = "") @status = :failed @error = reason @attempts += 1 end |
#increment_attempts! ⇒ Object
57 58 59 |
# File 'lib/tina4/job.rb', line 57 def increment_attempts! @attempts += 1 end |
#reject(reason = "") ⇒ Object
Reject this job with a reason. Alias for fail().
74 75 76 |
# File 'lib/tina4/job.rb', line 74 def reject(reason = "") fail(reason) end |
#retry(delay_seconds: 0) ⇒ Object
Re-queue this message with incremented attempts. Uses the stored queue reference (set at construction time).
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tina4/job.rb', line 24 def retry(delay_seconds: 0) q = @queue raise ArgumentError, "No queue reference — set at construction" unless q @attempts += 1 @status = :pending @available_at = delay_seconds > 0 ? Time.now + delay_seconds : nil q.backend.enqueue(self) self end |
#to_array ⇒ Object
35 36 37 |
# File 'lib/tina4/job.rb', line 35 def to_array [@id, @topic, @payload, @priority, @attempts] end |
#to_hash ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tina4/job.rb', line 39 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
53 54 55 |
# File 'lib/tina4/job.rb', line 53 def to_json(*_args) JSON.generate(to_hash) end |