lib/delayed/performable_method.rb in delayed_job-1.8.5 vs lib/delayed/performable_method.rb in delayed_job-1.9.0pre
- old
+ new
@@ -1,55 +1,62 @@
+class Class
+ def load_for_delayed_job(arg)
+ self
+ end
+
+ def dump_for_delayed_job
+ name
+ end
+end
+
module Delayed
class PerformableMethod < Struct.new(:object, :method, :args)
- CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
- AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/
+ STRING_FORMAT = /^LOAD\;([A-Z][\w\:]+)(?:\;(\w+))?$/
+
+ class LoadError < StandardError
+ end
def initialize(object, method, args)
raise NoMethodError, "undefined method `#{method}' for #{object.inspect}" unless object.respond_to?(method)
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 display_name
+ if STRING_FORMAT === object
+ "#{$1}#{$2 ? '#' : '.'}#{method}"
+ else
+ "#{object.class}##{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
+ rescue PerformableMethod::LoadError
+ # We cannot do anything about objects that can't be loaded
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
+ def load(obj)
+ if STRING_FORMAT === obj
+ $1.constantize.load_for_delayed_job($2)
+ else
+ obj
end
+ rescue => e
+ Delayed::Worker.logger.warn "Could not load object for job: #{e.message}"
+ raise PerformableMethod::LoadError
end
- def dump(arg)
- case arg
- when Class then class_to_string(arg)
- when ActiveRecord::Base then ar_to_string(arg)
- else arg
+ def dump(obj)
+ if obj.respond_to?(:dump_for_delayed_job)
+ "LOAD;#{obj.dump_for_delayed_job}"
+ else
+ obj
end
end
-
- def ar_to_string(obj)
- "AR:#{obj.class}:#{obj.id}"
- end
-
- def class_to_string(obj)
- "CLASS:#{obj.name}"
- end
end
-end
\ No newline at end of file
+end