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 you're 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 = options['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

VERSION =
'0.4.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



39
40
41
# File 'lib/resque/plugins/status.rb', line 39

def options
  @options
end

#uuidObject (readonly)

Returns the value of attribute uuid



39
40
41
# File 'lib/resque/plugins/status.rb', line 39

def uuid
  @uuid
end

Class Method Details

.included(base) ⇒ Object



41
42
43
# File 'lib/resque/plugins/status.rb', line 41

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()



193
194
195
196
197
198
199
200
201
# File 'lib/resque/plugins/status.rb', line 193

def at(num, total, *messages)
  if total.to_f <= 0.0
    raise(NotANumber, "Called at() with total=#{total} which is not a number")
  end
  tick({
    'num' => num,
    'total' => total
  }, *messages)
end

#completed(*messages) ⇒ Object

set the status to 'completed' passing along any addional messages



218
219
220
221
222
223
# File 'lib/resque/plugins/status.rb', line 218

def completed(*messages)
  set_status({
    'status' => 'completed',
    'message' => "Completed at #{Time.now}"
  }, *messages)
end

#failed(*messages) ⇒ Object

set the status to 'failed' passing along any additional messages



213
214
215
# File 'lib/resque/plugins/status.rb', line 213

def failed(*messages)
  set_status({'status' => 'failed'}, *messages)
end

#initialize(uuid, options = {}) ⇒ Object

Create a new instance with uuid and options



137
138
139
140
# File 'lib/resque/plugins/status.rb', line 137

def initialize(uuid, options = {})
  @uuid    = uuid
  @options = options
end

#kill!Object

kill the current job, setting the status to 'killed' and raising Killed

Raises:



226
227
228
229
230
231
232
# File 'lib/resque/plugins/status.rb', line 226

def kill!
  set_status({
    'status' => 'killed',
    'message' => "Killed at #{Time.now}"
  })
  raise Killed
end

#nameObject



179
180
181
# File 'lib/resque/plugins/status.rb', line 179

def name
  "#{self.class.name}(#{options.inspect unless options.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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/resque/plugins/status.rb', line 146

def safe_perform!
  set_status({'status' => 'working'})
  perform
  if status && status.failed?
    on_failure(status.message) 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

Returns:

  • (Boolean)


185
186
187
# File 'lib/resque/plugins/status.rb', line 185

def should_kill?
  Resque::Plugins::Status::Hash.should_kill?(uuid)
end

#statusObject

get the Resque::Plugins::Status::Hash object for the current uuid



175
176
177
# File 'lib/resque/plugins/status.rb', line 175

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.



170
171
172
# File 'lib/resque/plugins/status.rb', line 170

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()



207
208
209
210
# File 'lib/resque/plugins/status.rb', line 207

def tick(*messages)
  kill! if should_kill?
  set_status({'status' => 'working'}, *messages)
end