module Backburner::Performable

Public Class Methods

handle_asynchronously(klass, method, opts={}) click to toggle source

Make all calls to an instance method asynchronous. The given opts will be passed to the async method. @example

Backburner::Performable.handle_asynchronously(MyObject, :long_task, queue: 'long-tasks')

NB: The method called on the async proxy will be “”#{method}_without_async“. This will also be what's given to the Worker.enqueue method so your workers need to know about that. It shouldn't be a problem unless the producer and consumer are from different codebases (or anywhere they don't both call the handle_asynchronously method when booting up)

# File lib/backburner/performable.rb, line 67
def self.handle_asynchronously(klass, method, opts={})
  _handle_asynchronously(klass, klass, method, opts)
end
handle_static_asynchronously(klass, method, opts={}) click to toggle source

Make all calls to a class method asynchronous. The given opts will be passed to the async method. Please see the NB on handle_asynchronously

# File lib/backburner/performable.rb, line 73
def self.handle_static_asynchronously(klass, method, opts={})
  _handle_asynchronously(klass, klass.singleton_class, method, opts)
end
included(base) click to toggle source
# File lib/backburner/performable.rb, line 5
def self.included(base)
  base.send(:include, InstanceMethods)
  base.send(:include, Backburner::Queue)
  base.extend ClassMethods
end

Private Class Methods

_handle_asynchronously(klass, klass_eval_scope, method, opts={}) click to toggle source
# File lib/backburner/performable.rb, line 77
def self._handle_asynchronously(klass, klass_eval_scope, method, opts={})
  aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
  with_async_name    = :"#{aliased_method}_with_async#{punctuation}"
  without_async_name = :"#{aliased_method}_without_async#{punctuation}"

  klass.send(:include, Performable) unless included_modules.include?(Performable)
  klass_eval_scope.class_eval do
    define_method with_async_name do |*args|
      async(opts).__send__ without_async_name, *args
    end
    alias_method without_async_name, method.to_sym
    alias_method method.to_sym, with_async_name
  end
end