Search code examples
ruby-on-railsrubyasynchronousdelayed-job

Delayed Jobs: handle_asynchronously with variable execution time


I want to run a delayed function after X seconds. X is determinated by an object's field.

I tried this:

# 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 }

But it doesn't work, returns:

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

How can it be done?


Solution

  • You need to pass in a parameter to the proc

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