Using delayed_job on a non-ActiveRecord class
I'm attempting to use handle_asynchronously
on a me开发者_C百科thod of a class, as per the following:
class MyClass
def publish_stream(opts={})
# . . .
end
handle_asynchronously :publish_stream
end
When delayed_job attempts to execute, it throws the following exception:
NoMethodError: undefined method `publish_stream_without_send_later' for #<YAML::Object:0xbb52b00>
(Note that the I replaced the actual method name with "my_method" above.)
I believe the exception is occurring because the class defining this method is not derived from ActiveRecord::Base
; instead, it's simply derived from Object
. What can I do to resolve this?
My guess is that DelayedJob tries to serialize the object you are working on, and it cannot recreate the object from that.
After some digging, it turns out that the delayed_job rake task must be loading something in the wrong order. It worked fine in the console. The solution was to do the following manually in an initializer:
require 'my_class'
where "my_class" is the filename of the class.
精彩评论