Sha256: 462c4fad4653cde66465865d0a36e261cce336409d20577f4a18911a4695d601

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Resque
  module Mixin
    # Module that provides easy queueing of jobs.
    # Transparently handles instance and class methods.
    #
    # If you consider this class:
    #
    #   class Repository < ActiveRecord::Base
    #     include Resque::Mixin::Async
    #     @queue = :high_priority
    #
    #     def self.create_index
    #       # code
    #     end
    #
    #     def notify_watchers
    #       # code
    #     end
    #   end
    #
    # you can queue new jobs on both the class and an instance like this:
    #
    #   @repository.async(:notify_watchers)
    #   Repository.async(:create_index)
    #
    module Async
      def self.included(base)
        base.extend(ClassMethods)
      end
 
      def async(method, *args)
        Resque.enqueue(self.class, id, method, *args)
      end
 
      module ClassMethods
        def queue
          @queue || 'default'
        end

        def async(method, *args)
          Resque.enqueue(self, nil, method, *args)
        end
 
        # Performs a class method if id is nil or
        # an instance method if id has a value.
        def perform(*args)
          id = args.shift
          method = args.shift
 
          obj = id ? find(id) : self
          obj.send(method, *args)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
commonthread-rails-0.3.0 lib/resque/mixin/async.rb