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

#constantize(camel_cased_word) ⇒ Object



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
# File 'lib/boss_queue/job.rb', line 72

def constantize(camel_cased_word) # :nodoc:
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check it it's owned
      # directly before we reach Object or the end of ancestors.
      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # owner is in Object, so raise
      constant.const_get(name, false)
    end
  end
end

#enqueueObject



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

def enqueue
  queue = AWS::SQS.new.queues[queue_name]
  queue.send_message(id.to_s)
end

#enqueue_with_delay(delay) ⇒ Object



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

def enqueue_with_delay(delay)
  queue = AWS::SQS.new.queues[queue_name]
  queue.send_message(id.to_s, :delay_seconds => [900, [0, delay].max].min)
end

#fail(err) ⇒ Object



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

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



66
67
68
69
# File 'lib/boss_queue/job.rb', line 66

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

#workObject



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

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