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'])
Constant Summary collapse
- DontPerform =
Raise Resque::Job::DontPerform from a before_perform hook to abort the job.
Class.new(StandardError)
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).
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#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.
- .from_hash(hash) ⇒ Object
-
.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, start = Time.now) ⇒ 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
classify, constantize, decode, encode, mongo, mongo_queues, mongo_stats, mongo_workers
Constructor Details
#initialize(queue, payload, start = Time.now) ⇒ Job
Returns a new instance of Job.
34 35 36 37 38 |
# File 'lib/resque/job.rb', line 34 def initialize(queue, payload, start = Time.now) @queue = queue @payload = payload @start = start end |
Instance Attribute Details
#payload ⇒ Object (readonly)
This job’s associated payload object.
30 31 32 |
# File 'lib/resque/job.rb', line 30 def payload @payload end |
#queue ⇒ Object (readonly)
The name of the queue from which this job was pulled (or is to be placed)
27 28 29 |
# File 'lib/resque/job.rb', line 27 def queue @queue end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
32 33 34 |
# File 'lib/resque/job.rb', line 32 def start @start end |
#worker ⇒ Object
The worker object which is currently processing this job.
23 24 25 |
# File 'lib/resque/job.rb', line 23 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.
45 46 47 48 49 50 51 52 53 |
# File 'lib/resque/job.rb', line 45 def self.create(queue, klass, *args) Resque.validate(klass, queue) if Resque.inline? constantize(klass).perform(*decode(encode(args))) else Resque.push(queue, :class => klass.to_s, :args => args) end 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/resque/job.rb', line 83 def self.destroy(queue, klass, *args) klass = klass.to_s destroyed = 0 ### find the item using a mongo query, that's essentially why we removed decode / encode ### for the job payload. This should be much faster than loading every job and matching ### them against our conditions ! mongo_query = { :queue => queue.to_s, 'item.class' => klass } unless args.empty? mongo_query.merge!({ 'item.args' => args }) end job_count = mongo.find(mongo_query).count mongo.remove(mongo_query, :safe => true) QueueStats.remove_job(queue,job_count) job_count # mongo.find(mongo_query).each do |rec| # json = decode(rec['item']) # # match = json['class'] == klass # match &= json['args'] == args unless args.empty? # # if match # destroyed += 1 # mongo.remove(:_id => rec['_id']) # end # end # # destroyed end |
.from_hash(hash) ⇒ Object
55 56 57 |
# File 'lib/resque/job.rb', line 55 def self.from_hash(hash) new(hash['queue'],hash['payload'],Time.parse(hash['run_at'])) end |
Instance Method Details
#==(other) ⇒ Object
Equality
221 222 223 224 225 |
# File 'lib/resque/job.rb', line 221 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.
194 195 196 |
# File 'lib/resque/job.rb', line 194 def args @payload['args'] end |
#fail(exception) ⇒ Object
Given an exception object, hands off the needed parameters to the Failure module.
200 201 202 203 204 205 206 |
# File 'lib/resque/job.rb', line 200 def fail(exception) Failure.create \ :payload => payload, :exception => exception, :worker => worker, :queue => queue end |
#inspect ⇒ Object
String representation
215 216 217 218 |
# File 'lib/resque/job.rb', line 215 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.
189 190 191 |
# File 'lib/resque/job.rb', line 189 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/resque/job.rb', line 126 def perform job = payload_class job_args = args || [] job_was_performed = false before_hooks = Plugin.before_hooks(job) around_hooks = Plugin.around_hooks(job) after_hooks = Plugin.after_hooks(job) failure_hooks = Plugin.failure_hooks(job) 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) 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) 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 |
#recreate ⇒ Object
Creates an identical job, essentially placing this job back on the queue.
210 211 212 |
# File 'lib/resque/job.rb', line 210 def recreate self.class.create(queue, payload_class, *args) end |