Class: Resque::JobWithStatus
- Inherits:
-
Object
- Object
- Resque::JobWithStatus
- Defined in:
- lib/resque_ui/overrides/resque_status/job_with_status.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#worker ⇒ Object
readonly
Returns the value of attribute worker.
Class Method Summary collapse
-
.enqueue(klass, options = {}) ⇒ Object
Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>.
-
.perform(uuid = nil, options = {}) ⇒ Object
This is the method called by Resque::Worker when processing jobs.
Instance Method Summary collapse
- #counter(counter) ⇒ Object
- #incr_counter(counter) ⇒ Object
-
#initialize(uuid, worker = nil, options = {}) ⇒ JobWithStatus
constructor
Create a new instance with
uuid
andoptions
OVERRIDE to add the worker attr. - #name ⇒ Object
-
#pause! ⇒ Object
Pause the current job, setting the status to ‘paused’.
-
#safe_perform! ⇒ Object
Run by the Resque::Worker when processing this job.
-
#tick(*messages) ⇒ Object
sets the status of the job for the current iteration.
Constructor Details
#initialize(uuid, worker = nil, options = {}) ⇒ JobWithStatus
Create a new instance with uuid
and options
OVERRIDE to add the worker attr
45 46 47 48 49 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 45 def initialize(uuid, worker = nil, = {}) @uuid = uuid @options = @worker = worker end |
Instance Attribute Details
#worker ⇒ Object (readonly)
Returns the value of attribute worker.
4 5 6 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 4 def worker @worker end |
Class Method Details
.enqueue(klass, options = {}) ⇒ Object
Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>. Returns the UUID of the job override to pass actual parameters instead of a single hash, to make backward compatible with existing resque jobs.
9 10 11 12 13 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 9 def self.enqueue(klass, = {}) uuid = Resque::Status.create :name => "#{self.name}: #{.inspect}" Resque.enqueue(klass, uuid, ) uuid end |
.perform(uuid = nil, options = {}) ⇒ Object
This is the method called by Resque::Worker when processing jobs. It creates a new instance of the job class and populates it with the uuid and options.
You should not override this method, rather the perform
instance method. OVERRIDE to pass the block in order to set the worker status, returns the worker object
57 58 59 60 61 62 63 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 57 def self.perform(uuid=nil, = {}) uuid ||= Resque::Status.generate_uuid worker = yield if block_given? instance = new(uuid, worker, ) instance.safe_perform! { |status| yield status if block_given? } instance end |
Instance Method Details
#counter(counter) ⇒ Object
99 100 101 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 99 def counter(counter) Resque::Status.counter(counter, uuid) end |
#incr_counter(counter) ⇒ Object
95 96 97 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 95 def incr_counter(counter) Resque::Status.incr_counter(counter, uuid) end |
#name ⇒ Object
91 92 93 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 91 def name "#{self.class.name}: #{.inspect}" end |
#pause! ⇒ Object
Pause the current job, setting the status to ‘paused’
36 37 38 39 40 41 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 36 def pause! set_status({ 'status' => 'paused', 'message' => "#{worker} paused at #{Time.now}" }) 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 69 def safe_perform! unless should_kill? || (status && status.killed?) set_status({'status' => 'working'}) perform { |status| yield status if block_given? } kill! if should_kill? completed unless status && status.completed? on_success if respond_to?(:on_success) end rescue Killed logger.info "Job #{self} Killed at #{Time.now}" Resque::Status.killed(uuid) on_killed if respond_to?(:on_killed) rescue => e logger.error e failed("The task failed because of an error: #{e}") if respond_to?(:on_failure) on_failure(e) else raise e end end |
#tick(*messages) ⇒ Object
sets the status of the job for the current iteration. 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::Status.kill()
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 19 def tick(*) kill! if should_kill? || status.killed? set_status({'status' => 'working'}, *) # check to see if the worker doing the job has been paused, pause the job if so if self.worker && self.worker.paused? loop do # Set the status to paused. # May need to do this repeatedly because there could be workers in a chained job still doing work. pause! unless status.paused? break unless self.worker.paused? sleep 60 end set_status({'status' => 'working'}, *) unless status && (status.completed? || status.paused? || status.killed?) end end |