class Resque::Plugins::Mission

Constants

Version

Attributes

progress[R]

Public Class Methods

create_from_options(args={}) click to toggle source

Public: Override this in your class to allow new tasks to be created from the queue. The given args will be what is fed to .queue

args - from self.queue!

# File lib/resque/plugins/mission.rb, line 25
def self.create_from_options(args={})
  new args
end
inherited(subklass) click to toggle source

When subclassing Task, we also want to make it available as a resque job

# File lib/resque/plugins/mission.rb, line 11
def self.inherited(subklass)
  subklass.const_set('Job', Class.new(Mission::Job))
  subklass::Job.const_set('TASK_CLASS', subklass)
end
queue(q=nil) click to toggle source

Public: Sets the queue name for resque

# File lib/resque/plugins/mission.rb, line 37
def self.queue(q=nil)
  if q then @queue = q.to_s
  else @queue ||= "statused"
  end
end
queue!(args={}) click to toggle source

Public: Create a Queue Job.

args - Hash that will be fed to self.create_from_options from the queue.

# File lib/resque/plugins/mission.rb, line 32
def self.queue!(args={})
  self::Job.create({'args' => args})
end
step(method_name, options={}) click to toggle source

Public: Declare a step to be run on call. Steps will be run in the order they are declared.

method_name - a symbol name for the method to be called. options - An optional Hash of information about the step.

current arguments:
- :message => Used for status updates, vs. method_name titlecased
# File lib/resque/plugins/mission.rb, line 50
def self.step(method_name, options={})
  steps << [method_name, options]
end
steps() click to toggle source

Internal: The list of steps to be run on call

# File lib/resque/plugins/mission.rb, line 17
def self.steps
  @steps ||= []
end

Public Instance Methods

call(status=nil) { |index, class.length, name| ... } click to toggle source

Public: Perform the Mission

status - Optional Mission::Progress object block - if given, will yield idx, total, status message to the block

before performing the step
# File lib/resque/plugins/mission.rb, line 59
def call(status=nil, &block)
  @progress = status || Progress.new
  start_timer
  self.class.steps.each_with_index do |step, index|
    method_name, options = step
    next if progress.completed?(method_name.to_s)
    progress.start method_name.to_s
    name = options[:message] || (method_name.to_s.gsub(/\w+/) {|word| word.capitalize})
    yield index, self.class.steps.length, name if block_given?
    send method_name
  end
  progress.finish
rescue Object => e
  progress.failures += 1
  raise e
end

Private Instance Methods

delta_time() click to toggle source

Private: time since call was called and now

# File lib/resque/plugins/mission.rb, line 83
def delta_time
  return 0 unless @start_time
  Time.now.to_f - @start_time
end
start_timer() click to toggle source

Private: start the timer

# File lib/resque/plugins/mission.rb, line 78
def start_timer
  @start_time = Time.now.to_f
end
stats_key(key=nil) click to toggle source

Private: Key for Statsd

# File lib/resque/plugins/mission.rb, line 89
def stats_key(key=nil)
  @key_base ||= self.class.name.downcase.gsub(/\s+/,"_").gsub('::','.')
  key ? "#{@key_base}.#{key}" : @key_base
end