Class: Backburner::Job
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Backburner::Job
- Includes:
- Helpers
- Defined in:
- lib/backburner/job.rb
Overview
A single backburner job which can be processed and removed by the worker
Defined Under Namespace
Classes: JobFormatInvalid, JobNotFound, JobTimeout, RetryJob
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#body ⇒ Object
Returns the value of attribute body.
-
#name ⇒ Object
Returns the value of attribute name.
-
#stats_releases ⇒ Object
Returns the value of attribute stats_releases.
-
#task ⇒ Object
Returns the value of attribute task.
Instance Method Summary collapse
-
#__getobj__ ⇒ Object
Sets the delegator object to the underlying beaneater job self.bury.
- #bury ⇒ Object
-
#initialize(task) ⇒ Job
constructor
Construct a job to be parsed and processed.
-
#job_class ⇒ Object
Returns the class for the job handler.
-
#process ⇒ Object
Processes a job and handles any failure, deleting the job once complete.
- #retry(count, delay) ⇒ Object
Methods included from Helpers
#classify, #constantize, #dasherize, #exception_message, #expand_tube_name, included, #queue_config, #resolve_max_job_retries, #resolve_priority, #resolve_respond_timeout, #resolve_retry_delay, #resolve_retry_delay_proc
Constructor Details
#initialize(task) ⇒ Job
Construct a job to be parsed and processed
task is a reserved object containing the json body in the form of
{ :class => "NewsletterSender", :args => ["[email protected]"] }
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/backburner/job.rb', line 22 def initialize(task) @hooks = Backburner::Hooks @task = task @body = task.body.is_a?(Hash) ? task.body : Backburner.configuration.job_parser_proc.call(task.body) @name = body["class"] || body[:class] @args = body["args"] || body[:args] @ttr = body["ttr"] || body[:ttr] rescue => ex # Job was not valid format # self.bury # raise JobFormatInvalid, "Job body could not be parsed: #{ex.inspect}" end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
12 13 14 |
# File 'lib/backburner/job.rb', line 12 def args @args end |
#body ⇒ Object
Returns the value of attribute body.
12 13 14 |
# File 'lib/backburner/job.rb', line 12 def body @body end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/backburner/job.rb', line 12 def name @name end |
#stats_releases ⇒ Object
Returns the value of attribute stats_releases.
12 13 14 |
# File 'lib/backburner/job.rb', line 12 def stats_releases @stats_releases end |
#task ⇒ Object
Returns the value of attribute task.
12 13 14 |
# File 'lib/backburner/job.rb', line 12 def task @task end |
Instance Method Details
#__getobj__ ⇒ Object
Sets the delegator object to the underlying beaneater job self.bury
36 37 38 39 |
# File 'lib/backburner/job.rb', line 36 def __getobj__ __setobj__(@task) super end |
#bury ⇒ Object
85 86 87 88 |
# File 'lib/backburner/job.rb', line 85 def bury @hooks.invoke_hook_events(job_name, :on_bury, *args) @task.bury end |
#job_class ⇒ Object
Returns the class for the job handler
100 101 102 103 104 |
# File 'lib/backburner/job.rb', line 100 def job_class handler = try_job_class raise(JobNotFound, self.name) unless handler handler end |
#process ⇒ Object
Processes a job and handles any failure, deleting the job once complete
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/backburner/job.rb', line 46 def process # Invoke before hook and stop if false res = @hooks.invoke_hook_events(job_name, :before_perform, *args) return false unless res # Execute the job @hooks.around_hook_events(job_name, :around_perform, *args) do timeout_job_after(@ttr > 1 ? @ttr - 1 : @ttr) do # Decide if we want access to the root task if job_class.methods.include?(:perform_with_task) job_class.perform_with_task(@task, *args) else job_class.perform(*args) end end end retry_count = 0 begin task.delete rescue StandardError => allqex retry_count += 1 puts "Failed to delete task, waiting #{retry_count * 10}secs" sleep(retry_count * 10) retry if retry_count < 3 end # Invoke after perform hook @hooks.invoke_hook_events(job_name, :after_perform, *args) rescue => e @hooks.invoke_hook_events(job_name, :on_failure, e, *args) raise e end |
#retry(count, delay) ⇒ Object
90 91 92 93 |
# File 'lib/backburner/job.rb', line 90 def retry(count, delay) @hooks.invoke_hook_events(job_name, :on_retry, count, delay, *args) @task.release(delay) end |