Class: Resque::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/resque_manager/overrides/resque/job.rb

Instance Method Summary collapse

Instance Method Details

#performObject

Attempts to perform the work represented by this job instance. Calls #perform on the class given in the payload with the arguments given in the payload. A block is sent so a message can be yielded back to be set in the worker.



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
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/resque_manager/overrides/resque/job.rb', line 7

def perform
  job = payload_class
  job_args = args || []
  job_was_performed = false

  begin
    # Execute before_perform hook. Abort the job gracefully if
    # Resque::DontPerform is raised.
    begin
      before_hooks.each do |hook|
        job.send(hook, *job_args)
      end
    rescue DontPerform
      return false
    end

    # Execute the job. Do it in an around_perform hook if available.
    if around_hooks.empty?
      job.perform(*job_args) do |status|
        self.worker
      end
      job_was_performed = true
    else
      # We want to nest all around_perform plugins, with the last one
      # finally calling perform
      stack = around_hooks.reverse.inject(nil) do |last_hook, hook|
        if last_hook
          lambda do
            job.send(hook, *job_args) { last_hook.call }
          end
        else
          lambda do
            job.send(hook, *job_args) do
              result = job.perform(*job_args) do |status|
                self.worker
              end
              job_was_performed = true
              result
            end
          end
        end
      end
      stack.call
    end

    # Execute after_perform hook
    after_hooks.each do |hook|
      job.send(hook, *job_args)
    end

    # Return true if the job was performed
    return job_was_performed

      # If an exception occurs during the job execution, look for an
      # on_failure hook then re-raise.
  rescue Object => e
    failure_hooks.each { |hook| job.send(hook, e, *job_args) }
    raise e
  end
end