Sha256: da9e0a1d9934ec3855d07d1e520447ba2c1d5b1fb50b9b667cccb913f03c7871

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require 'resque'

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

      def request(object, method, *args)
        options = object.send("#{method.to_s.sub(/([?!=])$/, '')}_backgrounded_options")
        @@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
        clazz.respond_to?(method) ? clazz : clazz.find(id)
      end
      def instance_identifiers(object)
        instance, id = if object.is_a?(Class) 
          [object.name, -1]
        else
          [object.class.name, object.id]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backgrounded-0.7.3 lib/backgrounded/handler/resque_handler.rb
backgrounded-0.7.2 lib/backgrounded/handler/resque_handler.rb