Sha256: 2808bc79b9fbfb96b2b9c1d048b573669867cf0d5d97f61832f834ceaae83d5e

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'resque'
require 'backgrounded/handler/abstract_handler'
require 'active_record/base'

# enqueue requests in resque
module Backgrounded
  module Resque
    class ResqueHandler < Backgrounded::Handler::AbstractHandler
      DEFAULT_QUEUE = 'backgrounded'
      INVALID_ID = -1

      # resque uses this attribute to determine what queue the job belongs in
      # @see Resque.queue_from_class
      class_attribute :queue
      self.queue = DEFAULT_QUEUE

      # enqueue the requested operation into resque
      # the resque worker will invoke .perform with the class/method/args
      # @see .perform
      def request(object, method, args)
        ResqueHandler.queue = options[:queue] || DEFAULT_QUEUE
        instance, id = instance_identifiers(object)
        ::Resque.enqueue(ResqueHandler, instance, id, method, *args)
      end

      # invoke the requested method
      # fired from the resque workers
      def self.perform(clazz, id, method, *args)
        find_instance(clazz, id, method).send(method, *args)
      end

      private
      def self.find_instance(clazz, id, method)
        clazz = clazz.constantize
        id.to_i == INVALID_ID ? clazz : clazz.find(id)
      end
      def instance_identifiers(object)
        instance, id = if object.kind_of?(ActiveRecord::Base)
          [object.class.name, object.id]
        else
          [object.name, INVALID_ID]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
backgrounded-resque-2.0.0.rc4 lib/backgrounded-resque/resque_handler.rb