Rails - Delayed Job of specific method with run_at in params / specifying when to post

Laurent picture Laurent · May 17, 2011 · Viewed 7k times · Source

I'm a bit at a loss here.
I'm simply trying to delay a checkin, for instance, at a user defined time. I'd like to make it as simple as possible.
EDIT: solved, see below.

class MyController < ApplicationController

  def new_checkin
    # default venue is "SubMate"
    @venue_id = params[:venue_id] || "4c2c3acd8abca59355760120"
    @time = params[:time]
    @checkin = Delayed::Job.enqueue(self.perform_checkin, :run_at => @time.minutes.from_now)
  end

  def perform_checkin
    foursquare.checkins.add(:venueId => @venue_id, :broadcast => "private")
  end

The result is strange : the delayed_job is save, the action is performed (without the requested @time delay), and i still get an error:

ArgumentError in MyController#new_checkin
  Cannot enqueue items which do not respond to perform

What am I missing? How can I perform this delayed job, with the user requested delay?

Thank you!

UPDATE
Thanks to @Roman below, didn't know you could pass arguments to delay:

def new_checkin
  @venue_id = params[:venue_id]
  @time = params[:time]
  @checkin = self.perform_checkin
end

def perform_checkin
  foursquare.checkins.delay({:run_at => @time.minutes.from_now}).add(:venueId => @venue_id, :broadcast => "private")
end

Answer

Roman picture Roman · May 17, 2011

If you want to enqueue it this way, then as the error suggests, the enqueued object should respond to :perform. Check this http://asciicasts.com/episodes/171-delayed-job You can also use :handle_asynchronously or .delay.method. Check this: https://github.com/collectiveidea/delayed_job