5

I need to schedule a job in rails, where a method will called on a model. I know there are several cron-based gems, but these are all for repeating jobs, where as I need to perform the job only once, at a set time. Is there a gem that will do this?

charleyh
  • 2,033
  • 2
  • 20
  • 32

2 Answers2

4

Rails 4.2 will have the Active Job framework for scheduling jobs. You can use the Active Job gem in Rails 4.1. It serves as a standardized wrapper for several queueing systems, including Sidekiq and Resque (both require Redis to handle delayed jobs). Using Active Job gives you the flexibility of swapping out the queueing system if necessary.

You can schedule a job with Active Job like this:

MyJob.enqueue_at(5.hours.from_now, record)

The Rails maintainers intend for this to become the standard mechanism for scheduling jobs.

Have a look at the rails-mailinglist-signup example application to see how to use Active Job.

Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82
  • Big fan of your work Daniel. Did you ever use ActiveJob as a gem with 4.1 or did you go straight to Rails 4.2 to use it? I am getting `uninitialized constant ActiveJob` when I try to use it as a gem with 4.1 and can't determine whether it is possible or not. – Marklar Oct 27 '14 at 00:53
  • 1
    After they released Rails 4.2.0.beta1, ActiveJob was no longer available as a stand-alone gem. It's now merged into Rails 4.2. I'm using Rails 4.2. I don't know whether ActiveJob can be used with 4.1. – Daniel Kehoe Oct 27 '14 at 00:58
0

If you deploy to Heroku, there is a scheduler add-on.

Otherwise, you can use Sidekiq + Sidetiq to accomplish what you are looking for.

Rafal
  • 2,576
  • 2
  • 18
  • 13