Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rykov committed Sep 17, 2011
0 parents commit 0e3e871
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2011 Michael Rykov

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resque-delay
---------------

Requires the resque gem.

Allows to call .send_later or .delay.method on objects ala DelayedJob


Installation
============

$ gem install resque-delay


Author
=====

Michael Rykov :: mrykov@gmail.com
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
task :default => :spec
task :test => :spec

desc "Run specs"
task :spec do
exec "spec spec/resque_spec.rb"
end
5 changes: 5 additions & 0 deletions lib/resque-delay.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'resque_delay/performable_method'
require 'resque_delay/message_sending'

Object.send(:include, ResqueDelay::MessageSending)
Module.send(:include, ResqueDelay::MessageSending::ClassMethods)
49 changes: 49 additions & 0 deletions lib/resque_delay/message_sending.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'active_support/basic_object'

module ResqueDelay
class DelayProxy < ActiveSupport::BasicObject
def initialize(target, options)
@target = target
@options = options
end

def method_missing(method, *args)
queue = @options[:to] || :default
performable_method = PerformableMethod.create(@target, method, args)
Resque::Job.create(queue, DelayProxy, performable_method)
end

# Called asynchrously by Resque
def self.perform(args)
PerformableMethod.new(*args).perform
end
end

module MessageSending
def delay(options = {})
DelayProxy.new(self, options)
end
alias __delay__ delay

#def send_later(method, *args)
# warn "[DEPRECATION] `object.send_later(:method)` is deprecated. Use `object.delay.method"
# __delay__.__send__(method, *args)
#end
#
#def send_at(time, method, *args)
# warn "[DEPRECATION] `object.send_at(time, :method)` is deprecated. Use `object.delay(:run_at => time).method"
# __delay__(:run_at => time).__send__(method, *args)
#end

module ClassMethods
def handle_asynchronously(method)
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
with_method, without_method = "#{aliased_method}_with_delay#{punctuation}", "#{aliased_method}_without_delay#{punctuation}"
define_method(with_method) do |*args|
delay.__send__(without_method, *args)
end
alias_method_chain method, :delay
end
end
end
end
58 changes: 58 additions & 0 deletions lib/resque_delay/performable_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module ResqueDelay
class PerformableMethod < Struct.new(:object, :method, :args)
CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/

def self.create(object, method, args)
raise NoMethodError, "undefined method `#{method}' for #{object.inspect}" unless object.respond_to?(method)
self.new(object, method, args)
end

def initialize(object, method, args)
self.object = dump(object)
self.args = args.map { |a| dump(a) }
self.method = method.to_sym
end

def display_name
case self.object
when CLASS_STRING_FORMAT then "#{$1}.#{method}"
when AR_STRING_FORMAT then "#{$1}##{method}"
else "Unknown##{method}"
end
end

def perform
load(object).send(method, *args.map{|a| load(a)})
rescue ActiveRecord::RecordNotFound
# We cannot do anything about objects which were deleted in the meantime
true
end

private

def load(arg)
case arg
when CLASS_STRING_FORMAT then $1.constantize
when AR_STRING_FORMAT then $1.constantize.find($2)
else arg
end
end

def dump(arg)
case arg
when Class then class_to_string(arg)
when ActiveRecord::Base then ar_to_string(arg)
else arg
end
end

def ar_to_string(obj)
"AR:#{obj.class}:#{obj.id}"
end

def class_to_string(obj)
"CLASS:#{obj.name}"
end
end
end
22 changes: 22 additions & 0 deletions resque-delay.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Gem::Specification.new do |s|
s.name = "resque-delay"
s.version = "0.5.0"
s.date = Time.now.strftime('%Y-%m-%d')
s.summary = "Enable send_later/delay for Resque"
s.homepage = "http://github.com/rykov/resque-delay"
s.email = "mrykov@gmail"
s.authors = [ "Michael Rykov" ]
s.has_rdoc = false

s.files = %w( README.md Rakefile LICENSE )
s.files += Dir.glob("lib/**/*")
s.files += Dir.glob("test/**/*")
s.files += Dir.glob("spec/**/*")

s.add_dependency "resque", ">= 1.9"
s.add_dependency "activerecord", ">= 2.3"

s.description = <<DESCRIPTION
Enable send_later support for Resque
DESCRIPTION
end
6 changes: 6 additions & 0 deletions spec/resque_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'logger'

describe "resque" do

end
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rubygems'
$TESTING=true
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'resque'

Spec::Matchers.define :have_key do |expected|
match do |redis|
redis.exists(expected)
end
end

0 comments on commit 0e3e871

Please sign in to comment.