Class: BossQueue::Job

Inherits:
AWS::Record::HashModel
  • Object
show all
Defined in:
lib/boss_queue/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queue_nameObject

Returns the value of attribute queue_name.



6
7
8
# File 'lib/boss_queue/job.rb', line 6

def queue_name
  @queue_name
end

Class Method Details

.find_by_id(id, options = {}) ⇒ Record::HashModel Also known as: []

We need consistent reads, so override @find_by_id

Parameters:

  • id (String)

    The id of the record to load.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :shard (String)

    Specifies what shard (i.e. table) should be searched.

Returns:

  • (Record::HashModel)

    Returns the record with the given id.

Raises:

  • (RecordNotFound)

    Raises a record not found exception if there was no data found for the given id.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/boss_queue/job.rb', line 36

def find_by_id id, options = {}

  table = dynamo_db_table(options[:shard])

  data = table.items[id].attributes.to_h(:consistent_read => options[:consistent_read])

  raise AWS::Record::RecordNotFound, "no data found for id: #{id}" if data.empty?

  obj = self.new(:shard => table)
  obj.send(:hydrate, id, data)
  obj

end

Instance Method Details

#enqueueObject



54
55
56
# File 'lib/boss_queue/job.rb', line 54

def enqueue
  sqs_queue.send_message(id.to_s)
end

#enqueue_with_delay(delay) ⇒ Object



58
59
60
# File 'lib/boss_queue/job.rb', line 58

def enqueue_with_delay(delay)
  sqs_queue.send_message(id.to_s, :delay_seconds => [900, [0, delay].max].min)
end

#fail(err) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/boss_queue/job.rb', line 71

def fail(err)
  self.failed_attempts ||= 0
  self.failed_attempts += 1
  self.exception_name = err.class.to_s
  self.exception_message = err.message
  self.stacktrace = err.backtrace[0, 15].join("\n")

  failed_target = target rescue nil

  if failed_target.nil?
    if delete_if_target_missing?
      destroy
    else
      self.failed = true
      self.target_missing = true
      self.save!
    end

  elsif failure_action == 'retry' && retry_delay
    enqueue_with_delay(retry_delay)
    self.save!

  elsif failure_action == 'callback' &&
        failure_callback


    delete_me = failed_target.send(failure_callback, err, *arguments)
    if delete_me
      destroy
    else
      self.failed = true
      self.save!
    end

  else
    self.failed = true
    self.save!
  end
end

#retry_delayObject



111
112
113
114
# File 'lib/boss_queue/job.rb', line 111

def retry_delay
  return nil if failed_attempts.nil? || failed_attempts > 4
  60 * 2**(failed_attempts - 1)
end

#sqs_queue=(queue_obj) ⇒ Object

:nodoc:



116
117
118
# File 'lib/boss_queue/job.rb', line 116

def sqs_queue=(queue_obj) # :nodoc:
  @sqs_queue = queue_obj
end

#workObject



62
63
64
65
66
67
68
69
# File 'lib/boss_queue/job.rb', line 62

def work
  begin
    target.send(callback, *arguments)
    destroy
  rescue StandardError => err
    fail(err)
  end
end