Sha256: 7bd734f09044209525f3e19320ea6e470035b988da57329dce2bd82aad207c88

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'resque'

module Backgrounded
  module Handler
    #enque requests in resque
    class ResqueHandler
      DEFAULT_QUEUE = 'backgrounded'
      INVALID_ID = -1
      @@queue = DEFAULT_QUEUE

      def request(object, method, *args)
        options = object.send Backgrounded.method_name_for_backgrounded_options(method)
        @@queue = options[:queue] || DEFAULT_QUEUE
        instance, id = instance_identifiers(object)
        Resque.enqueue(ResqueHandler, instance, id, method, *args)
      end
      def self.queue
        @@queue
      end
      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.is_a?(Class) 
          [object.name, INVALID_ID]
        else
          [object.class.name, object.id]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
backgrounded-0.7.4 lib/backgrounded/handler/resque_handler.rb