延迟作业:handle_asynchronously,执行时间可变

Delayed Jobs: handle_asynchronously with variable execution time

我想 运行 X 秒后的延迟函数。 X 由对象的字段决定。

我试过这个:

# after_create this function is triggered
def save_distance
    update_attribute :distance, 5
end

# function gets delayed by X seconds (X = field named "timespan" of my object)
handle_asynchronously :save_distance, :run_at => Proc.new { timespan.seconds.from_now }

但是不行,returns:

undefined local variable or method `timespan' for #<Class:0x007f9b9d4f4c88>

如何做到?

你需要给proc传入一个参数

handle_asynchronously :save_distance, :run_at => Proc.new {|object| object.timespan.seconds.from_now }