forked from rykov/resque-delay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0e3e871
Showing
9 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |