Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use without activerecord #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lib/resque_delay/message_sending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DelayProxy < ActiveSupport::BasicObject
def initialize(target, options)
@target = target
@options = options
if @options[:in].not_nil? && !@options[:in].kind_of?(::Fixnum)
if @options[:in].present? && !@options[:in].kind_of?(::Fixnum)
raise ::ArgumentError.new("Delayed settings must be a Fixnum! not a #{@options[:in].class.name}")
end
end
Expand All @@ -19,24 +19,29 @@ def method_missing(method, *args)
end
end

# Called asynchrously by Resque
# Called asynchronously by Resque
def self.perform(args)
PerformableMethod.new(*args).perform
if args.respond_to?(:[])
PerformableMethod.new(args["object"], args["method"], args["args"]).perform
else
PerformableMethod.new(*args).perform
end
end

private
def delay?
delay.to_i > 0
end

def delay
@delay ||= @options[:in]
end
def delay?
delay.to_i > 0
end

def delay
@delay ||= @options[:in]
end
end

module MessageSending
def delay(options = {})
DelayProxy.new(self, options)
end
end
end
end
34 changes: 24 additions & 10 deletions lib/resque_delay/performable_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module ResqueDelay
class PerformableMethod < Struct.new(:object, :method, :args)
CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/
DM_STRING_FORMAT = /^DM\:((?:[A-Z][a-zA-z]+)(?:\:\:[A-Z][a-zA-z]+)*)\:([\d\:]+)$/

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

Expand All @@ -18,15 +19,19 @@ def display_name
case self.object
when CLASS_STRING_FORMAT then "#{$1}.#{method}"
when AR_STRING_FORMAT then "#{$1}##{method}"
when DM_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
load(object).send(method, *args.map { |a| load(a) })
rescue => e
if defined?(ActiveRecord) && e.kind_of?(ActiveRecord::RecordNotFound)
true
else
raise
end
end

private
Expand All @@ -35,24 +40,33 @@ def load(arg)
case arg
when CLASS_STRING_FORMAT then $1.constantize
when AR_STRING_FORMAT then $1.constantize.find($2)
when DM_STRING_FORMAT then $1.constantize.get!(*$2.split(':'))
else arg
end
end

def dump(arg)
case arg
when Class, Module then class_to_string(arg)
when ActiveRecord::Base then ar_to_string(arg)
else arg
if arg.kind_of?(Class) || arg.kind_of?(Module)
class_to_string(arg)
elsif defined?(ActiveRecord) && arg.kind_of?(ActiveRecord::Base)
ar_to_string(arg)
elsif defined?(DataMapper) && arg.kind_of?(DataMapper::Resource)
dm_to_string(arg)
else
arg
end
end

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

def dm_to_string(obj)
"DM:#{obj.class}:#{obj.key.join(':')}"
end

def class_to_string(obj)
"CLASS:#{obj.name}"
end
end
end
end
3 changes: 1 addition & 2 deletions resque-delay.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ Gem::Specification.new do |s|
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
end