class Resque::Plugins::Pertry

Constants

JOB_HASH

Attributes

exception_handled[R]

Public Class Methods

enqueue(args = {}) click to toggle source

Enqueue a job

# File lib/resque/plugins/pertry.rb, line 14
def enqueue(args = {})
  raise ArgumentError, "Invalid arguments, expecting a Hash but got: #{args.inspect}" unless Hash === args

  args.symbolize_keys!
  args = check_arguments(args)
  raise ArgumentError, "Invalid arguments, #{JOB_HASH} is a reserved argument!" if args.key?(JOB_HASH)

  Resque.enqueue(self, args)
end
in_queue(queue) click to toggle source

Specificy job queue

# File lib/resque/plugins/pertry.rb, line 45
def in_queue(queue)
  @queue = queue.to_sym
end
instance(args = {}) click to toggle source
# File lib/resque/plugins/pertry.rb, line 37
def instance(args = {})
  args.symbolize_keys!
  raise ArgumentError, "Job is not supported, missing key #{JOB_HASH} from payload #{args.inspect}" unless args.key?(JOB_HASH)

  new(check_arguments(args), args[JOB_HASH])
end
needs(*arguments) click to toggle source

Define required job attributes

# File lib/resque/plugins/pertry.rb, line 55
def needs(*arguments)
  arguments.each do |argument|
    if Hash === argument
      argument.each do |key, default|
        self.required_arguments << { :name => key, :default => default }
      end
    else
      self.required_arguments << { :name => argument }
    end
  end
end
new(arguments, job_properties) click to toggle source
# File lib/resque/plugins/pertry.rb, line 89
def initialize(arguments, job_properties)
  set_job_arguments(arguments)
  set_job_properties(job_properties)
end
perform(args = {}) click to toggle source

Perform a job

# File lib/resque/plugins/pertry.rb, line 25
def perform(args = {})
  raise ArgumentError, "Invalid arguments, expecting a Hash but got: #{args.inspect}" unless Hash === args

  begin
    job = instance(args)
    job.perform
  rescue => e
    job.handle_exception(e)
    raise unless job.exception_handled
  end
end
queue() click to toggle source

Get job queue

# File lib/resque/plugins/pertry.rb, line 50
def queue
  @queue or raise ArgumentError, "No queue defined for job #{self.name}!"
end
required_arguments() click to toggle source

List of required attributes

# File lib/resque/plugins/pertry.rb, line 68
def required_arguments
  @required_arguments ||= []
end

Private Class Methods

check_arguments(provided_arguments) click to toggle source

Check that job arguments match required arguments

# File lib/resque/plugins/pertry.rb, line 75
def check_arguments(provided_arguments)
  required_arguments.inject({}) do |checked_arguments, argument|
    raise ArgumentError, "#{self} is missing required argument #{argument[:name]} from #{provided_arguments.inspect}" unless provided_arguments.member?(argument[:name]) || argument.member?(:default)

    provided_argument = provided_arguments[argument[:name]] || argument[:default]
    # TODO check that provided_argument is serializable as json

    checked_arguments[argument[:name]] = provided_argument
    checked_arguments
  end
end

Public Instance Methods

arguments() click to toggle source
# File lib/resque/plugins/pertry.rb, line 126
def arguments
  @_arguments
end
complete!() click to toggle source

mark job as complete

# File lib/resque/plugins/pertry.rb, line 121
def complete!
  Resque::Pertry::ResquePertryPersistence.finnish_job(self.class, payload)
end
Also aliased as: complete_job!
complete_job!()
Alias for: complete!
exception_handled!() click to toggle source
# File lib/resque/plugins/pertry.rb, line 110
def exception_handled!
  @exception_handled = true
end
fail!() click to toggle source

mark job as failed, won’t be retried

# File lib/resque/plugins/pertry.rb, line 115
def fail!
  Resque::Pertry::ResquePertryPersistence.fail_job(self.class, payload)
end
Also aliased as: fail_job!
fail_job!()
Alias for: fail!
handle_exception(exception) click to toggle source
# File lib/resque/plugins/pertry.rb, line 99
def handle_exception(exception)
  # we don't handle exceptions by default, leave it for resque
  # when overridding this method, if you decide you want to completely
  # handle it, and not pass it back to resque, you'll need to call
  # exception_handled!
end
payload() click to toggle source
# File lib/resque/plugins/pertry.rb, line 130
def payload
  @_arguments.merge(JOB_HASH => @_job_properties)
end
perform() click to toggle source

Perform method needs to be overridden in job classes

# File lib/resque/plugins/pertry.rb, line 95
def perform
  raise NoMethodError, "No method #{self.class.name}#perform defined!"
end

Private Instance Methods

set_job_arguments(hash) click to toggle source
# File lib/resque/plugins/pertry.rb, line 141
def set_job_arguments(hash)
  @_arguments = hash
  arguments.each do |key, val|
    instance_variable_set("@#{key}", val)
  end
end
set_job_properties(hash) click to toggle source
# File lib/resque/plugins/pertry.rb, line 136
def set_job_properties(hash)
  @_job_properties ||= {}
  @_job_properties.merge!(hash)
end