GhostRider
GhostRider

Reputation: 2170

Rail app on Heroku: How to implement a "scheduled job" at regular intervals

I am not sure if this question is of the correct format for SO.

I have a rails app with deployment on Heroku. In development I am using the "crono" gem to send a simple email out every week to remind users of various things. I can see how to use this in production with Heroku. There is almost nothing on this in a google search. Therefore my question is: "what is the best way to implement a simple weekly job in a rails app that is deployed on Heroku. Heroku has "scheduler" but this is a "best effort" add on that can claim to be reliable (according to their documentation)

Thanks

Upvotes: 1

Views: 464

Answers (1)

andy
andy

Reputation: 8875

There's two ways to achieve what you want:

1. Use the Heroku scheduler (I would)

Honestly it's just so simple to set up, I would use this. It's also extremely cheap because you only pay for the dyno while the job is running. After it's run Heroku destroys the dyno (it's a one off dyno)

The best way to implement it is to have you background jobs callable by a rake task, and simply have the scheduler call that.

If you have a time interval Heroku doesn't support, simply handle that in your code. For example if you want to send e-mails once a week, have a timestamp to record when the last email was sent. Run the scheduler once a day and just check to see if it's ok to send another email, if not do nothing.

2. Use some kind of scheduler gem

There's a bunch of them out there. For example, rufus.

In this case, you'd write your rufus jobs. You would then need to have a worker dyno always running for rufus, this is much more expensive.

You'd tell the dyno to run rufus and keep running rufus by specifying the command rufus needs in your procfile. Something like:

scheduler: rake rufus:scheduler # Add rake task for rufus scheduler process

(credit for the above snippet How to avoid Rufus scheduler being called as many times as there are no.of dynos?)

Upvotes: 1

Related Questions