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 = 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 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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



52
53
54
# File 'lib/resque/plugins/status.rb', line 52

def options
  @options
end

#uuidObject (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, *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



231
232
233
234
235
236
# File 'lib/resque/plugins/status.rb', line 231

def completed(*messages)
  set_status({
    'status' => STATUS_COMPLETED,
    'message' => "Completed at #{Time.now}"
  }, *messages)
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(*messages)
  set_status({'status' => STATUS_FAILED}, *messages)
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, options = {})
  @uuid    = uuid
  @options = options
end

#kill!Object

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

Raises:



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

#nameObject



192
193
194
# File 'lib/resque/plugins/status.rb', line 192

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.



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


198
199
200
# File 'lib/resque/plugins/status.rb', line 198

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

#statusObject

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(*messages)
  kill! if should_kill?
  set_status({'status' => STATUS_WORKING}, *messages)
end