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
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

#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

#stats_releasesObject

Returns the value of attribute stats_releases.



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

def stats_releases
  @stats_releases
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



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

def __getobj__
  __setobj__(@task)
  super
end

#buryObject



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_classObject

Returns the class for the job handler

Examples:

job_class # => NewsletterSender

Raises:



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

#processObject

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

Examples:

@task.process


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