Sha256: f924e21f1de3a6a8d141f464395a2628f203f06565d085bd74ff46535891d78f

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

class Bj
  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 Bj::Mixin::Async
    #
    #     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)
        Bj.submit "CACHE_CLASSES=true ./script/runner '#{self.class.name}.perform(#{id}, #{method.inspect})'", :stdin => args.to_yaml, :is_restartable => false
      end
 
      module ClassMethods
        def async(method, *args)
          Bj.submit "CACHE_CLASSES=true ./script/runner '#{self.name}.perform(nil, #{method.inspect})'", :stdin => args.to_yaml, :is_restartable => false
        end
 
        # Performs a class method if id is nil or
        # an instance method if id has a value.
        def perform(id, method)
          args = YAML.load(STDIN)
 
          obj = id ? find(id) : self
          obj.send(method, *args)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
commonthread-rails-0.4.2 lib/bj/mixin/async.rb
commonthread-rails-0.4.1 lib/bj/mixin/async.rb
commonthread-rails-0.4.0 lib/bj/mixin/async.rb
commonthread-rails-0.3.4 lib/bj/mixin/async.rb
commonthread-rails-0.3.3 lib/bj/mixin/async.rb
commonthread-rails-0.3.2 lib/bj/mixin/async.rb
commonthread-rails-0.3.1 lib/bj/mixin/async.rb
commonthread-rails-0.3.0 lib/bj/mixin/async.rb