Class: Backburner::Job

Inherits:
SimpleDelegator
  • Object
show all
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

Instance Method Summary collapse

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]"] }

Examples:

Backburner::Job.new(payload)


22
23
24
25
26
27
28
29
30
31
# 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]
rescue => ex # Job was not valid format
  self.bury
  raise JobFormatInvalid, "Job body could not be parsed: #{ex.inspect}"
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



12
13
14
# File 'lib/backburner/job.rb', line 12

def args
  @args
end

#bodyObject

Returns the value of attribute body.



12
13
14
# File 'lib/backburner/job.rb', line 12

def body
  @body
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/backburner/job.rb', line 12

def name
  @name
end

#taskObject

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



35
36
37
38
# File 'lib/backburner/job.rb', line 35

def __getobj__
  __setobj__(@task)
  super
end

#buryObject



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

def bury
  @hooks.invoke_hook_events(job_name, :on_bury, *args)
  task.bury
end

#job_classObject

Returns the class for the job handler

Examples:

job_class # => NewsletterSender

Raises:



81
82
83
84
85
# File 'lib/backburner/job.rb', line 81

def job_class
  handler = try_job_class
  raise(JobNotFound, self.name) unless handler
  handler
end

#processObject

Processes a job and handles any failure, deleting the job once complete

Examples:

@task.process


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/backburner/job.rb', line 45

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
    # We subtract one to ensure we timeout before beanstalkd does, except if:
    #  a) ttr == 0, to support never timing out
    #  b) ttr == 1, so that we don't accidentally set it to never time out
    #  NB: A ttr of 1 will likely result in race conditions between
    #  Backburner and beanstalkd and should probably be avoided
    timeout_job_after(task.ttr > 1 ? task.ttr - 1 : task.ttr) { job_class.perform(*args) }
  end
  task.delete
  # 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



71
72
73
74
# File 'lib/backburner/job.rb', line 71

def retry(count, delay)
  @hooks.invoke_hook_events(job_name, :on_retry, count, delay, *args)
  task.release(delay: delay)
end