Module: Resque::Plugins::Status
- Included in:
- JobWithStatus
- Defined in:
- lib/resque/plugins/status.rb,
lib/resque/plugins/status/hash.rb
Overview
Resque::Plugins::Status is a module your jobs will include. It provides helper methods for updating the status/etc from within an instance as well as class methods for creating and queuing the jobs.
All you have to do to get this functionality is include Resque::Plugins::Status and then implement a <tt>perform<tt> method.
For example
class ExampleJob
include Resque::Plugins::Status
def perform
num = ['num']
i = 0
while i < num
i += 1
at(i, num)
end
completed("Finished!")
end
end
This job would iterate num times updating the status as it goes. At the end we update the status telling anyone listening to this job that its complete.
Defined Under Namespace
Modules: ClassMethods Classes: Hash, Killed, NotANumber
Constant Summary collapse
- VERSION =
'0.5.0'
- STATUS_QUEUED =
'queued'
- STATUS_WORKING =
'working'
- STATUS_COMPLETED =
'completed'
- STATUS_FAILED =
'failed'
- STATUS_KILLED =
'killed'
- STATUSES =
[ STATUS_QUEUED, STATUS_WORKING, STATUS_COMPLETED, STATUS_FAILED, STATUS_KILLED ].freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Class Method Summary collapse
Instance Method Summary collapse
-
#at(num, total, *messages) ⇒ Object
set the status of the job for the current itteration.
-
#completed(*messages) ⇒ Object
set the status to ‘completed’ passing along any addional messages.
-
#failed(*messages) ⇒ Object
set the status to ‘failed’ passing along any additional messages.
-
#initialize(uuid, options = {}) ⇒ Object
Create a new instance with
uuid
andoptions
. -
#kill! ⇒ Object
kill the current job, setting the status to ‘killed’ and raising
Killed
. - #name ⇒ Object
-
#safe_perform! ⇒ Object
Run by the Resque::Worker when processing this job.
-
#should_kill? ⇒ Boolean
Checks against the kill list if this specific job instance should be killed on the next iteration.
-
#status ⇒ Object
get the Resque::Plugins::Status::Hash object for the current uuid.
-
#status=(new_status) ⇒ Object
Set the jobs status.
-
#tick(*messages) ⇒ Object
sets the status of the job for the current itteration.
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
52 53 54 |
# File 'lib/resque/plugins/status.rb', line 52 def @options end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
52 53 54 |
# File 'lib/resque/plugins/status.rb', line 52 def uuid @uuid end |
Class Method Details
.included(base) ⇒ Object
54 55 56 |
# File 'lib/resque/plugins/status.rb', line 54 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#at(num, total, *messages) ⇒ Object
set the status of the job for the current itteration. num
and total
are passed to the status as well as any messages. This will kill the job if it has been added to the kill list with Resque::Plugins::Status::Hash.kill()
206 207 208 209 210 211 212 213 214 |
# File 'lib/resque/plugins/status.rb', line 206 def at(num, total, *) if total.to_f <= 0.0 raise(NotANumber, "Called at() with total=#{total} which is not a number") end tick({ 'num' => num, 'total' => total }, *) end |
#completed(*messages) ⇒ Object
set the status to ‘completed’ passing along any addional messages
231 232 233 234 235 236 |
# File 'lib/resque/plugins/status.rb', line 231 def completed(*) set_status({ 'status' => STATUS_COMPLETED, 'message' => "Completed at #{Time.now}" }, *) end |
#failed(*messages) ⇒ Object
set the status to ‘failed’ passing along any additional messages
226 227 228 |
# File 'lib/resque/plugins/status.rb', line 226 def failed(*) set_status({'status' => STATUS_FAILED}, *) end |
#initialize(uuid, options = {}) ⇒ Object
Create a new instance with uuid
and options
150 151 152 153 |
# File 'lib/resque/plugins/status.rb', line 150 def initialize(uuid, = {}) @uuid = uuid @options = end |
#kill! ⇒ Object
kill the current job, setting the status to ‘killed’ and raising Killed
239 240 241 242 243 244 245 |
# File 'lib/resque/plugins/status.rb', line 239 def kill! set_status({ 'status' => STATUS_KILLED, 'message' => "Killed at #{Time.now}" }) raise Killed end |
#name ⇒ Object
192 193 194 |
# File 'lib/resque/plugins/status.rb', line 192 def name "#{self.class.name}(#{.inspect unless .empty?})" end |
#safe_perform! ⇒ Object
Run by the Resque::Worker when processing this job. It wraps the perform
method ensuring that the final status of the job is set regardless of error. If an error occurs within the job’s work, it will set the status as failed and re-raise the error.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/resque/plugins/status.rb', line 159 def safe_perform! set_status({'status' => STATUS_WORKING}) perform if status && status.failed? on_failure(status.) if respond_to?(:on_failure) return elsif status && !status.completed? completed end on_success if respond_to?(:on_success) rescue Killed Resque::Plugins::Status::Hash.killed(uuid) on_killed if respond_to?(:on_killed) rescue => e failed("The task failed because of an error: #{e}") if respond_to?(:on_failure) on_failure(e) else raise e end end |
#should_kill? ⇒ Boolean
Checks against the kill list if this specific job instance should be killed on the next iteration
198 199 200 |
# File 'lib/resque/plugins/status.rb', line 198 def should_kill? Resque::Plugins::Status::Hash.should_kill?(uuid) end |
#status ⇒ Object
get the Resque::Plugins::Status::Hash object for the current uuid
188 189 190 |
# File 'lib/resque/plugins/status.rb', line 188 def status Resque::Plugins::Status::Hash.get(uuid) end |
#status=(new_status) ⇒ Object
Set the jobs status. Can take an array of strings or hashes that are merged (in order) into a final status hash.
183 184 185 |
# File 'lib/resque/plugins/status.rb', line 183 def status=(new_status) Resque::Plugins::Status::Hash.set(uuid, *new_status) end |
#tick(*messages) ⇒ Object
sets the status of the job for the current itteration. You should use the at
method if you have actual numbers to track the iteration count. This will kill the job if it has been added to the kill list with Resque::Plugins::Status::Hash.kill()
220 221 222 223 |
# File 'lib/resque/plugins/status.rb', line 220 def tick(*) kill! if should_kill? set_status({'status' => STATUS_WORKING}, *) end |