Class: Tincan::Failure
- Inherits:
-
Object
- Object
- Tincan::Failure
- Defined in:
- lib/tincan/failure.rb
Overview
Encapsulates a failed attempt at a message attempted from a Redis queue.
Instance Attribute Summary collapse
-
#attempt_count ⇒ Object
Returns the value of attribute attempt_count.
-
#failed_at ⇒ Object
Returns the value of attribute failed_at.
-
#message_id ⇒ Object
Returns the value of attribute message_id.
-
#queue_name ⇒ Object
Returns the value of attribute queue_name.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Tincan::Failure
Assigns keys and values to this object based on an already-deserialized JSON object.
-
.from_json(json) ⇒ Tincan::Failure
Deserializes an object from a JSON string.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Overrides equality operator to determine if all ivars are equal.
-
#attempt_after ⇒ DateTime
Gives a date and time when this object is allowed to be attempted again, derived from when it last failed, plus the number of attempts, in seconds.
-
#initialize(message_id = nil, queue_name = nil) ⇒ Tincan::Message
constructor
Creates a new instance of a notification with an object (usually an ActiveModel instance).
-
#to_json(options = {}) ⇒ String
Generates a version of this failure as a JSON string.
Constructor Details
#initialize(message_id = nil, queue_name = nil) ⇒ Tincan::Message
Creates a new instance of a notification with an object (usually an ActiveModel instance).
16 17 18 19 20 21 |
# File 'lib/tincan/failure.rb', line 16 def initialize( = nil, queue_name = nil) self. = self.attempt_count = 0 self.failed_at = DateTime.now self.queue_name = queue_name end |
Instance Attribute Details
#attempt_count ⇒ Object
Returns the value of attribute attempt_count.
8 9 10 |
# File 'lib/tincan/failure.rb', line 8 def attempt_count @attempt_count end |
#failed_at ⇒ Object
Returns the value of attribute failed_at.
8 9 10 |
# File 'lib/tincan/failure.rb', line 8 def failed_at @failed_at end |
#message_id ⇒ Object
Returns the value of attribute message_id.
8 9 10 |
# File 'lib/tincan/failure.rb', line 8 def @message_id end |
#queue_name ⇒ Object
Returns the value of attribute queue_name.
8 9 10 |
# File 'lib/tincan/failure.rb', line 8 def queue_name @queue_name end |
Class Method Details
.from_hash(hash) ⇒ Tincan::Failure
Assigns keys and values to this object based on an already-deserialized JSON object.
42 43 44 45 46 |
# File 'lib/tincan/failure.rb', line 42 def self.from_hash(hash) instance = new(hash['message_id'], hash['queue_name']) instance.attempt_count = hash['attempt_count'].to_i instance end |
.from_json(json) ⇒ Tincan::Failure
Deserializes an object from a JSON string.
34 35 36 |
# File 'lib/tincan/failure.rb', line 34 def self.from_json(json) from_hash(JSON.parse(json)) end |
Instance Method Details
#==(other) ⇒ Object
Overrides equality operator to determine if all ivars are equal
59 60 61 62 63 64 65 |
# File 'lib/tincan/failure.rb', line 59 def ==(other) false unless other checks = %i(failed_at attempt_count message_id queue_name).map do |p| other.send(p) == send(p) end checks.all? end |
#attempt_after ⇒ DateTime
Gives a date and time when this object is allowed to be attempted again, derived from when it last failed, plus the number of attempts, in seconds.
27 28 29 |
# File 'lib/tincan/failure.rb', line 27 def attempt_after failed_at + Rational((attempt_count * 10), 86400) end |
#to_json(options = {}) ⇒ String
Generates a version of this failure as a JSON string.
50 51 52 53 54 |
# File 'lib/tincan/failure.rb', line 50 def to_json( = {}) Hash[%i(failed_at attempt_count message_id queue_name).map do |name| [name, send(name)] end].to_json() end |