Class: BossQueue::Job
- Inherits:
-
AWS::Record::HashModel
- Object
- AWS::Record::HashModel
- BossQueue::Job
- Defined in:
- lib/boss_queue/job.rb
Instance Attribute Summary collapse
-
#queue_name ⇒ Object
Returns the value of attribute queue_name.
Class Method Summary collapse
-
.find_by_id(id, options = {}) ⇒ Record::HashModel
(also: [])
We need consistent reads, so override @find_by_id.
Instance Method Summary collapse
- #enqueue ⇒ Object
- #enqueue_with_delay(delay) ⇒ Object
- #fail(err) ⇒ Object
- #retry_delay ⇒ Object
-
#sqs_queue=(queue_obj) ⇒ Object
:nodoc:.
- #work ⇒ Object
Instance Attribute Details
#queue_name ⇒ Object
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
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, = {} table = dynamo_db_table([:shard]) data = table.items[id].attributes.to_h(:consistent_read => [: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
#enqueue ⇒ Object
54 55 56 |
# File 'lib/boss_queue/job.rb', line 54 def enqueue sqs_queue.(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.(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. = err. 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_delay ⇒ Object
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 |
#work ⇒ Object
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 |