module SuckerPunch::Backgroundable::BackgroundableClassMethods

Public Instance Methods

always_background(*methods) click to toggle source

Marks methods to always be backgrounded. Takes one or more method symbols, and an optional options hash as the final argument.

# File lib/sucker_punch/backgroundable/backgroundable.rb, line 33
def always_background(*methods)
  options = methods.last.is_a?(Hash) ? methods.pop : {}
  @__backgroundable_methods ||= {}

  methods.each do |method|
    method = method.to_s
    if !@__backgroundable_methods[method]
      @__backgroundable_methods[method] ||= { }
      @__backgroundable_methods[method][:options] = options
      if Util.singleton_methods_include?(self, method) ||
          Util.instance_methods_include?(self, method)
        __enable_backgrounding(method)
      end
    end
  end
end
background(options = { }) click to toggle source

Allows you to background any method that has not been marked as a backgrounded method via {BackgroundableClassMethods#always_background}.

# File lib/sucker_punch/backgroundable/backgroundable.rb, line 52
def background(options = { })
  BackgroundProxy.new(self, options)
end
later(seconds, options = { }) click to toggle source

Allows you to background any method that has not been marked as a backgrounded method via {BackgroundableClassMethods#always_background}. The method will not be executed immediately, but only after 'seconds' seconds.

# File lib/sucker_punch/backgroundable/backgroundable.rb, line 59
def later(seconds, options = { })
  BackgroundProxy.new(self, options, seconds)
end
method_added(method) click to toggle source

@api private

Calls superclass method
# File lib/sucker_punch/backgroundable/backgroundable.rb, line 64
def method_added(method)
  super
  __method_added(method)
end
singleton_method_added(method) click to toggle source

@api private

Calls superclass method
# File lib/sucker_punch/backgroundable/backgroundable.rb, line 70
def singleton_method_added(method)
  super
  __method_added(method)
end

Private Instance Methods

__enable_backgrounding(method) click to toggle source
# File lib/sucker_punch/backgroundable/backgroundable.rb, line 86
      def __enable_backgrounding(method)
        singleton_method = Util.singleton_methods_include?(self, method)
        singleton = (class << self; self; end)

        if singleton_method

          SuckerPunch.logger.
            warn("always_background called for :#{method}, but :#{method} " +
                 "exists as both a class and instance method. Only the " +
                 "class method will be backgrounded.") if Util.instance_methods_include?(self, method)

          privatize = Util.private_singleton_methods_include?(self, method)
          protect = Util.protected_singleton_methods_include?(self, method) unless privatize
        else
          privatize = Util.private_instance_methods_include?(self, method)
          protect = Util.protected_instance_methods_include?(self, method) unless privatize
        end

        async_method = "__async_#{method}"
        sync_method = "__sync_#{method}"

        @__backgroundable_methods[method][:backgrounding] = true
        options = @__backgroundable_methods[method][:options]

        (singleton_method ? singleton : self).class_eval do
          define_method async_method do |*args|
            # run sucker punch job asynchronously
#            Job.new.async.perform(self, sync_method, args, options)
            JobRunner.new(self, sync_method, args, options).run
          end
        end
        
        code = singleton_method ? "class << self" : ""
        code << %Q{
          alias_method :#{sync_method}, :#{method}
          alias_method :#{method}, :#{async_method}
        }
        code << %Q{
          #{privatize ? "private" : "protected"} :#{method}, :#{sync_method}, :#{async_method}
        } if privatize || protect
        code << "end" if singleton_method

        class_eval code
      ensure
        @__backgroundable_methods[method][:backgrounding] = nil
      end
__method_added(method) click to toggle source
# File lib/sucker_punch/backgroundable/backgroundable.rb, line 77
def __method_added(method)
  method = method.to_s
  if @__backgroundable_methods &&
      @__backgroundable_methods[method] &&
      !@__backgroundable_methods[method][:backgrounding]
    __enable_backgrounding(method)
  end
end