作为一个例子,我有一个目前看起来像这样的类(我在这里使用delayed_job,但我之前遇到过这个问题):
class Invoice < ActiveRecord::Base
def issue!
# Do something time consuming
end
handle_asynchronously :issue!, owner_type: self.to_s, owner_id: ->(i) { i.id }
def send!
# Do something time consuming
end
handle_asynchronously :send!, owner_type: self.to_s, owner_id: ->(i) { i.id }
end
我想要的是这样的课程:
class Invoice < ActiveRecord::Base
handle_asynchronously :issue!, :send!, owner_type: self.to_s, owner_id: ->(i) { i.id }
def issue!
# Do something time consuming
end
def send!
# Do something time consuming
end
end
希望显而易见的是后一个例子优于可读性/干燥性。
这里的问题是当调用异步句柄时,发送!并发出!没有定义。
有没有办法推迟handle_asynchronously
调用,直到课程完全定义完毕?