Class: Resque::Job
- Inherits:
-
Object
- Object
- Resque::Job
- Extended by:
- Helpers
- Includes:
- Helpers
- Defined in:
- lib/resque/job.rb
Overview
A Resque::Job represents a unit of work. Each job lives on a single queue and has an associated payload object. The payload is a hash with two attributes: ‘class` and `args`. The `class` is the name of the Ruby class which should be used to run the job. The `args` are an array of arguments which should be passed to the Ruby class’s ‘perform` class-level method.
You can manually run a job using this code:
job = Resque::Job.reserve(:high)
klass = Resque::Job.constantize(job.payload['class'])
klass.perform(*job.payload['args'])
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
This job’s associated payload object.
-
#queue ⇒ Object
readonly
The name of the queue from which this job was pulled (or is to be placed).
-
#worker ⇒ Object
The worker object which is currently processing this job.
Class Method Summary collapse
-
.create(queue, klass, *args) ⇒ Object
Creates a job by placing it on a queue.
-
.destroy(queue, klass, *args) ⇒ Object
Removes a job from a queue.
-
.reserve(queue) ⇒ Object
Given a string queue name, returns an instance of Resque::Job if any jobs are available.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Equality.
-
#args ⇒ Object
Returns an array of args represented in this job’s payload.
-
#fail(exception) ⇒ Object
Given an exception object, hands off the needed parameters to the Failure module.
-
#initialize(queue, payload) ⇒ Job
constructor
A new instance of Job.
-
#inspect ⇒ Object
String representation.
-
#payload_class ⇒ Object
Returns the actual class constant represented in this job’s payload.
-
#perform ⇒ Object
Attempts to perform the work represented by this job instance.
-
#recreate ⇒ Object
Creates an identical job, essentially placing this job back on the queue.
Methods included from Helpers
after_fork, before_fork, classify, constantize, decode, encode, redis
Constructor Details
#initialize(queue, payload) ⇒ Job
Returns a new instance of Job.
28 29 30 31 |
# File 'lib/resque/job.rb', line 28 def initialize(queue, payload) @queue = queue @payload = payload end |
Instance Attribute Details
#payload ⇒ Object (readonly)
This job’s associated payload object.
26 27 28 |
# File 'lib/resque/job.rb', line 26 def payload @payload end |
#queue ⇒ Object (readonly)
The name of the queue from which this job was pulled (or is to be placed)
23 24 25 |
# File 'lib/resque/job.rb', line 23 def queue @queue end |
#worker ⇒ Object
The worker object which is currently processing this job.
19 20 21 |
# File 'lib/resque/job.rb', line 19 def worker @worker end |
Class Method Details
.create(queue, klass, *args) ⇒ Object
Creates a job by placing it on a queue. Expects a string queue name, a string class name, and an optional array of arguments to pass to the class’ ‘perform` method.
Raises an exception if no queue or class is given.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/resque/job.rb', line 38 def self.create(queue, klass, *args) if !queue raise NoQueueError.new("Jobs must be placed onto a queue.") end if klass.to_s.empty? raise NoClassError.new("Jobs must be given a class.") end Resque.push(queue, :class => klass.to_s, :args => args) end |
.destroy(queue, klass, *args) ⇒ Object
Removes a job from a queue. Expects a string queue name, a string class name, and, optionally, args.
Returns the number of jobs destroyed.
If no args are provided, it will remove all jobs of the class provided.
That is, for these two jobs:
{ ‘class’ => ‘UpdateGraph’, ‘args’ => [‘defunkt’] } { ‘class’ => ‘UpdateGraph’, ‘args’ => [‘mojombo’] }
The following call will remove both:
Resque::Job.destroy(queue, 'UpdateGraph')
Whereas specifying args will only remove the 2nd job:
Resque::Job.destroy(queue, 'UpdateGraph', 'mojombo')
This method can be potentially very slow and memory intensive, depending on the size of your queue, as it loads all jobs into a Ruby array before processing.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/resque/job.rb', line 74 def self.destroy(queue, klass, *args) klass = klass.to_s queue = "queue:#{queue}" destroyed = 0 redis.lrange(queue, 0, -1).each do |string| json = decode(string) match = json['class'] == klass match &= json['args'] == args unless args.empty? if match destroyed += redis.lrem(queue, 0, string).to_i end end destroyed end |
Instance Method Details
#==(other) ⇒ Object
Equality
140 141 142 143 144 |
# File 'lib/resque/job.rb', line 140 def ==(other) queue == other.queue && payload_class == other.payload_class && args == other.args end |
#args ⇒ Object
Returns an array of args represented in this job’s payload.
113 114 115 |
# File 'lib/resque/job.rb', line 113 def args @payload['args'] end |
#fail(exception) ⇒ Object
Given an exception object, hands off the needed parameters to the Failure module.
119 120 121 122 123 124 125 |
# File 'lib/resque/job.rb', line 119 def fail(exception) Failure.create \ :payload => payload, :exception => exception, :worker => worker, :queue => queue end |
#inspect ⇒ Object
String representation
134 135 136 137 |
# File 'lib/resque/job.rb', line 134 def inspect obj = @payload "(Job{%s} | %s | %s)" % [ @queue, obj['class'], obj['args'].inspect ] end |
#payload_class ⇒ Object
Returns the actual class constant represented in this job’s payload.
108 109 110 |
# File 'lib/resque/job.rb', line 108 def payload_class @payload_class ||= constantize(@payload['class']) end |
#perform ⇒ Object
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.
103 104 105 |
# File 'lib/resque/job.rb', line 103 def perform args ? payload_class.perform(*args) : payload_class.perform end |
#recreate ⇒ Object
Creates an identical job, essentially placing this job back on the queue.
129 130 131 |
# File 'lib/resque/job.rb', line 129 def recreate self.class.create(queue, payload_class, *args) end |