Sha256: b90098c71466cd947e4a3f79fbe648f8d0240b85fc1186ce99b7cc941ad62aa8

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

module Backburner
  # A single backburner job which can be processed and removed by the worker
  class Job
    include Backburner::Helpers

    # Raises when a job times out
    class JobTimeout < RuntimeError; end
    class JobNotFound < RuntimeError; end

    attr_accessor :task, :body, :name, :args

    # Construct a job to be parsed and processed
    #
    # task is a reserved object containing the json body in the form of
    #   { :class => "NewsletterSender", :args => ["foo@bar.com"] }
    #
    # @example
    #  Backburner::Job.new(payload)
    #
    def initialize(task)
      @task = task
      @body = JSON.parse(task.body)
      @name, @args = body["class"], body["args"]
    end

    # Processes a job and handles any failure, deleting the job once complete
    #
    # @example
    #   @task.process
    #
    def process
      timeout_job_after(task.ttr - 1) { job_class.perform(*args) }
      task.delete
    end

    # Bury a job out of the active queue if that job fails
    def bury
      task.bury
    end

    protected

    # Returns the class for the job handler
    #
    # @example
    #   job_class # => NewsletterSender
    #
    def job_class
      handler = constantize(name) rescue nil
      raise(JobNotFound, name) unless handler
      handler
    end

    # Timeout job after given time
    #
    # @example
    #   timeout_job_after(3) { do_something! }
    #
    def timeout_job_after(secs, &block)
      begin
        Timeout::timeout(secs) { yield }
      rescue Timeout::Error
        raise JobTimeout, "#{name} hit #{secs}s timeout"
      end
    end

  end # Job
end # Backburner

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backburner-0.0.3 lib/backburner/job.rb
backburner-0.0.2 lib/backburner/job.rb