Sha256: 48723316a5fe3862513b6fba746ab4ac6166adc5394ae5818066f94fb73f6487

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

module ResqueDelayable
  require "resque"

  begin
    require "resque/scheduler"
  rescue LoadError
  end

  class DelayedMethod
    attr_accessor :klass
    attr_accessor :instance
    attr_accessor :options

    def initialize(klass, instance = nil, options = {})
      unless instance.nil? || ResqueDelayable::ACTIVE_SERIALIZERS.any? {|klass| klass.serialize_match(instance) }
        raise "Cannot serialize instances of this type (#{instance.class}). Consider creating a serializer."
      end

      self.klass = klass
      self.instance = instance
      self.options = options
    end
  
    def dequeue(method, *parameters)
      ::Resque.dequeue(self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
    end

    def method_missing(method, *parameters)
      if instance.nil?
        raise NoMethodError.new("undefined method '#{method}' for #{self.klass}", method, parameters) unless klass.respond_to?(method)
      else
        raise NoMethodError.new("undefined method '#{method}' for #{self.instance}", method, parameters) unless instance.respond_to?(method)
      end

      if self.options[:run_at]
        if ::Resque.respond_to?(:enqueue_at)
          ::Resque.enqueue_at(self.options[:run_at], self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
        else
          raise "Cannot use run_at, resque-scheduler not installed"
        end
      elsif self.options[:run_in]
        if ::Resque.respond_to?(:enqueue_in)
          ::Resque.enqueue_in(self.options[:run_in], self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
        else
          raise "Cannot use run_in, resque-scheduler not installed"
        end
      else
        ::Resque.enqueue(self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
      end

      true
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
resque-delayable-1.0.2 lib/resque-delayable/delayed_method.rb
resque-delayable-1.0.1 lib/resque-delayable/delayed_method.rb
resque-delayable-1.0.0 lib/resque-delayable/delayed_method.rb