Class: ConeyIsland::Job
- Inherits:
-
Object
- Object
- ConeyIsland::Job
- Defined in:
- lib/coney_island/job.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#delay ⇒ Object
Returns the value of attribute delay.
-
#dont_log ⇒ Object
Returns the value of attribute dont_log.
-
#id ⇒ Object
Returns the value of attribute id.
-
#initialization_errors ⇒ Object
Returns the value of attribute initialization_errors.
-
#instance_id ⇒ Object
Returns the value of attribute instance_id.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#method_args ⇒ Object
Returns the value of attribute method_args.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
-
#object ⇒ Object
Returns the value of attribute object.
-
#retry_limit ⇒ Object
Returns the value of attribute retry_limit.
-
#retry_on_exception ⇒ Object
Returns the value of attribute retry_on_exception.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #execute_job_method ⇒ Object
- #finalize_job ⇒ Object
- #handle_job ⇒ Object
-
#initialize(metadata, args) ⇒ Job
constructor
A new instance of Job.
- #log ⇒ Object
- #next_attempt_delay ⇒ Object
- #resubmit_args ⇒ Object
- #ticket ⇒ Object
Constructor Details
#initialize(metadata, args) ⇒ Job
Returns a new instance of Job.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/coney_island/job.rb', line 7 def initialize(, args) @args = args @id = args['job_id'] || SecureRandom.uuid @dont_log = args['dont_log'] self.log.info ("Starting job #{@id}: #{@args}") unless self.dont_log @delay = args['delay'].to_i if args['delay'] @timeout = args['timeout'] @method_name = args['method_name'] @instance_id = args['instance_id'] @singleton = args['singleton'] @class_name = args['klass'] @klass = @class_name.constantize @method_args = args['args'] if @method_args.is_a? Hash # The whole args is keyword args @method_args.symbolize_keys! end # Symbolize hash keys for consistency and keyword arguments @method_args.each { |v| v.symbolize_keys! if v.is_a?(Hash) } if !!@method_args @attempts = args['attempt_count'] || 1 @retry_limit = args['retry_limit'] || 3 @retry_on_exception = args['retry_on_exception'] @metadata = if @klass.included_modules.include?(Performer) @delay ||= @klass.get_coney_settings[:delay] @timeout ||= @klass.get_coney_settings[:timeout] end @timeout ||= ConeyIsland.default_settings[:timeout] if @instance_id.present? @object = @klass.find(@instance_id) elsif @singleton @object = @klass.new else @object = @klass end rescue StandardError => e .ack if !ConeyIsland.running_inline? self.initialization_errors = true log.error("Error initializing with args #{args}:") log.error(e.) log.error(e.backtrace.join("\n")) ConeyIsland.poke_the_badger(e, {message: "Error during job initialization, bailing out", work_queue: self.ticket, job_payload: args}) log.info("finished job #{id}") end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def args @args end |
#attempts ⇒ Object
Returns the value of attribute attempts.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def attempts @attempts end |
#class_name ⇒ Object
Returns the value of attribute class_name.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def class_name @class_name end |
#delay ⇒ Object
Returns the value of attribute delay.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def delay @delay end |
#dont_log ⇒ Object
Returns the value of attribute dont_log.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def dont_log @dont_log end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def id @id end |
#initialization_errors ⇒ Object
Returns the value of attribute initialization_errors.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def initialization_errors @initialization_errors end |
#instance_id ⇒ Object
Returns the value of attribute instance_id.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def instance_id @instance_id end |
#klass ⇒ Object
Returns the value of attribute klass.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def klass @klass end |
#metadata ⇒ Object
Returns the value of attribute metadata.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def @metadata end |
#method_args ⇒ Object
Returns the value of attribute method_args.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def method_args @method_args end |
#method_name ⇒ Object
Returns the value of attribute method_name.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def method_name @method_name end |
#object ⇒ Object
Returns the value of attribute object.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def object @object end |
#retry_limit ⇒ Object
Returns the value of attribute retry_limit.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def retry_limit @retry_limit end |
#retry_on_exception ⇒ Object
Returns the value of attribute retry_on_exception.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def retry_on_exception @retry_on_exception end |
#timeout ⇒ Object
Returns the value of attribute timeout.
3 4 5 |
# File 'lib/coney_island/job.rb', line 3 def timeout @timeout end |
Instance Method Details
#execute_job_method ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/coney_island/job.rb', line 63 def execute_job_method if method_args.present? && method_args.length > 0 # The whole args is keyword args, just pass it as is if method_args.is_a? Hash object.send method_name, method_args else # Splat the array into args object.send method_name, *method_args end else object.send method_name end end |
#finalize_job ⇒ Object
109 110 111 112 113 |
# File 'lib/coney_island/job.rb', line 109 def finalize_job .ack if !ConeyIsland.running_inline? log.info("finished job #{id}") unless self.dont_log ConeyIsland::Worker.running_jobs.delete self end |
#handle_job ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/coney_island/job.rb', line 77 def handle_job ConeyIsland::Worker.running_jobs << self execute_job_method rescue StandardError => e log.error("Error executing #{self.class_name}##{self.method_name} #{self.id} for id #{self.instance_id} with args #{self.args}:") log.error(e.) log.error(e.backtrace.join("\n")) if retry_on_exception && (self.attempts < self.retry_limit) ConeyIsland.poke_the_badger(e, {work_queue: self.ticket, job_payload: self.args, attempt_count: self.attempts}) log.error("Resubmitting #{self.id} after error on attempt ##{self.attempts}") self.attempts += 1 ConeyIsland.submit(self.klass, self.method_name, self.resubmit_args) else ConeyIsland.poke_the_badger(e, {work_queue: self.ticket, job_payload: self.args}) log.error("Bailing out on #{self.id} after error on final attempt ##{self.attempts}:") if ConeyIsland.running_inline? raise end end ensure finalize_job end |
#log ⇒ Object
59 60 61 |
# File 'lib/coney_island/job.rb', line 59 def log ConeyIsland::Worker.log end |
#next_attempt_delay ⇒ Object
100 101 102 |
# File 'lib/coney_island/job.rb', line 100 def next_attempt_delay ConeyIsland.delay_seed**(self.attempts - 1) end |
#resubmit_args ⇒ Object
104 105 106 107 |
# File 'lib/coney_island/job.rb', line 104 def resubmit_args args.select{|key,val| ['timeout','retry_on_exception','retry_limit','args','instance_id'].include? key}.merge( 'attempt_count' => self.attempts, 'work_queue' => self.ticket, 'delay' => self.next_attempt_delay) end |
#ticket ⇒ Object
55 56 57 |
# File 'lib/coney_island/job.rb', line 55 def ticket ConeyIsland::Worker.ticket end |