Class: BossQueue::Job

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

Instance Attribute 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

Instance Method Details

#enqueueObject



24
25
26
# File 'lib/boss_queue/job.rb', line 24

def enqueue
  sqs_queue.send_message(id.to_s)
end

#enqueue_with_delay(delay) ⇒ Object



28
29
30
# File 'lib/boss_queue/job.rb', line 28

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

#fail(err) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/boss_queue/job.rb', line 48

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, 7].join("\n")

  if failure_action == 'retry' && retry_delay
    enqueue_with_delay(retry_delay)
  else
    self.failed = true
  end

  self.save!
end

#retry_delayObject



64
65
66
67
# File 'lib/boss_queue/job.rb', line 64

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

#sqs_queue=(queue_obj) ⇒ Object

:nodoc:



69
70
71
# File 'lib/boss_queue/job.rb', line 69

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

#workObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/boss_queue/job.rb', line 32

def work
  begin
    klass = constantize(model_class_name)
    if model_id
      target = klass.find(model_id)
    else
      target = klass
    end
    args = JSON.parse(job_arguments)
    target.send(job_method, *args)
    destroy
  rescue StandardError => err
    fail(err)
  end
end