I have an observer that calls observer class methods in a callback. These observer class methods are meant to be handled asynchronously:

class FooObserver << ActiveRecord::Observer
  def after_create(foo)
    self.class.do_something(foo)
  end

  class << self
    def do_something(foo)
      ...
    end
    handle_asynchronously :do_something
  end
end

I would like do_something() to be run asynchronously. And it appears I'm using handle_asynchronously correctly here. Unfortunately my logs show that when the callback is triggered, the observer actually runs do_something_without_delay() instead of #do_something, indicating that the task is not run asynchronously.)

Can someone explain why this task is run without delay, and how I can get my observer to run do_something asynchronously?

Thanks!

P.S. do_something() is defined as a class method because delayed_job has trouble serializing instances of the singleton observer when enqueueing the task as a Delayed::Job object. See here.

有帮助吗?

解决方案

I figured it out: the observer actually is using delayed_job; method_without_delay() is being called only in the worker thread once that job is ready for processing (to prevent an infinite sequence of delays).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top