Class: Tincan::Failure

Inherits:
Object
  • Object
show all
Defined in:
lib/tincan/failure.rb

Overview

Encapsulates a failed attempt at a message attempted from a Redis queue.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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).

Parameters:

  • message_id (Integer) (defaults to: nil)

    The identifier for the message to retry.

  • queue_name (String) (defaults to: nil)

    The name of the queue in which this failure originally occurred.



16
17
18
19
20
21
# File 'lib/tincan/failure.rb', line 16

def initialize(message_id = nil, queue_name = nil)
  self.message_id = message_id
  self.attempt_count = 0
  self.failed_at = DateTime.now
  self.queue_name = queue_name
end

Instance Attribute Details

#attempt_countObject

Returns the value of attribute attempt_count.



8
9
10
# File 'lib/tincan/failure.rb', line 8

def attempt_count
  @attempt_count
end

#failed_atObject

Returns the value of attribute failed_at.



8
9
10
# File 'lib/tincan/failure.rb', line 8

def failed_at
  @failed_at
end

#message_idObject

Returns the value of attribute message_id.



8
9
10
# File 'lib/tincan/failure.rb', line 8

def message_id
  @message_id
end

#queue_nameObject

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.

Parameters:

  • hash (Hash)

    A hash of properties and their values.

Returns:



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.

Parameters:

  • json (String)

    A JSON string to be decoded.

Returns:



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_afterDateTime

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.

Returns:

  • (DateTime)

    The date/time when this is allowed to be retried.



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.

Returns:

  • (String)

    A JSON-compliant marshalling of this instance’s data.



50
51
52
53
54
# File 'lib/tincan/failure.rb', line 50

def to_json(options = {})
  Hash[%i(failed_at attempt_count message_id queue_name).map do |name|
    [name, send(name)]
  end].to_json(options)
end