Permalink
Browse files

Have the basis of a class and ActiveRecord async sender working

  • Loading branch information...
Brad Gessler
Brad Gessler committed Oct 2, 2010
1 parent fd7c223 commit 46827a9b2855bf5dea9f990188c0beac6ec3fb75
View
@@ -17,5 +17,6 @@ tmtags
coverage
rdoc
pkg
+Gemfile.lock
-## PROJECT::SPECIFIC
+## PROJECT::SPECIFIC
View
14 Gemfile
@@ -0,0 +1,14 @@
+source :rubygems
+
+gem 'json'
+gem 'daemons'
+gem 'optitron'
+gem 'beanstalk-client'
+
+group :test do
+ gem 'rspec'
+ gem 'ZenTest'
+ gem 'ruby-debug'
+ gem 'activerecord'
+ gem 'sqlite3-ruby'
+end
View
@@ -1,12 +1,11 @@
-require 'rubygems'
require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "quebert"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{A worker queue framework built around beanstalkd}
+ gem.description = %Q{A worker queue framework built around beanstalkd}
gem.email = "brad@bradgessler.com"
gem.homepage = "http://github.com/bradgessler/quebert"
gem.authors = ["Brad Gessler"]
View
@@ -7,4 +7,9 @@ module Quebert
autoload :Support, 'quebert/support'
autoload :Worker, 'quebert/worker'
autoload :Daemonizable, 'quebert/daemonizing'
+ autoload :AsyncSender, 'quebert/async_sender'
+
+ def self.configuration
+ @configuration ||= Struct.new(:backend).new
+ end
end
View
@@ -1,40 +0,0 @@
-module Quebert
- module AsyncSend
-
- class InstanceJob < Job
- def call(klass, initialize_args, meth, args)
- Support.constantize(klass).new(initialize_args).send(meth, *args)
- end
- end
-
- class ClassJob < Job
- def call(klass, meth, args)
- Support.constantize(klass).send(meth, *args)
- end
- end
-
- def self.included(base)
- base.send :extend, ClassMethods
- base.send :include, InstanceMethods
- end
-
- Configuration = Struct.new(:producer)
-
- module ClassMethods
- def async_sender
- @async_sender ||= Configuration.new(Producer.new)
- end
-
- def async_send(meth, *args)
- async_sender.producer.put ClassJob.new(self, meth, *args)
- end
- end
-
- module InstanceMethods
- def async_send(meth, *args)
- self.class.async_sender.producer.put InstanceJob.new(self.class, id, meth, *args)
- end
- end
-
- end
-end
@@ -0,0 +1,71 @@
+module Quebert
+ module AsyncSender
+
+ module ActiveRecord
+ class RecordJob < Job
+ def perform(klass, id, meth, args)
+ Support.constantize(klass).find(id).send(meth, *args)
+ end
+ end
+
+ def self.included(base)
+ base.send :extend, ClassMethods
+ base.send :include, InstanceMethods
+ end
+
+ module InstanceMethods
+ def async_send(meth, *args)
+ RecordJob.enqueue(self.class.name, id, meth, args)
+ end
+ end
+
+ module ClassMethods
+ end
+ end
+
+ # Augment a class to asycnronously send messages for both instances
+ # and classes.
+ module Klass
+ class KlassJob < Job
+ def perform(klass, meth, args)
+ Support.constantize(klass).send(meth, args)
+ end
+ end
+
+ class InstanceJob < Job
+ def perform(klass, init_args, meth, args)
+ Support.constantize(klass).new(init_args).send(meth, args)
+ end
+ end
+
+ def self.included(base)
+ base.send :extend, ClassMethods
+ base.send :include, InstanceMethods
+
+ # Intercept the arguments that we initialize the class with
+ base.class_eval do
+ alias :initialize_without_async_send :initialize
+ alias :initialize :initialize_with_async_send
+ end
+ end
+
+ module ClassMethods
+ def async_send(meth, *args)
+ KlassJob.enqueue(self.name, meth, args)
+ end
+ end
+
+ module InstanceMethods
+ def async_send(meth, *args)
+ InstanceJob.enqueue(self.class.name, @_init_args, meth, args)
+ end
+
+ def initialize_with_async_send(*args)
+ @_init_args = args
+ initialize_without_async_send(*args)
+ end
+ end
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit 46827a9

Please sign in to comment.