Search code examples
ruby-on-railstimedelayed-job

Run Delayed Job at a specific time


I have a email delivery job which I would like to run at the user specified time. I have a datetime column deliver_at at which I should deliver the emails. I am using delayed-job for performing background jobs.

delayed job alows me to specify the time difference and not the exact time for scheduling the job.

  GiftCardMailerJob.set(wait: delay_by.minutes.from_now).perform_later(go)

Is there a way I give the deliver_at time directly and make delayed job run at that time?

If not how can I arrive at the time difference?

((deliver_at - Time.now) / 60).minutes.from_now

Should be the correct approach?


Solution

  • You can use the enqueue method in delayed_jobs

    Delayed::Job.enqueue(GiftCardMailerJob.new, :run_at => deliver_at)
    

    If you see the delayed_jobs table in the database, the run_at field is of type datetime and you can have any datetime value for it.