Search code examples
ruby-on-railscronrecurring-billing

How to implement something like cron jobs on Rails 4


I'm developing an app in Rails 4 and not sure about how to implement a certain feature: basically I need to implement some sort of cron job. For instance, let's say I create an invoice in my app. I need the app to check the invoice due date and send a reminder to the customer that the invoice hasn't been paid. I want to achieve this without any input from the user.

How can I achieve this?

I've been thinking about this and thought about use a tool like whenever which would check the db every so often.

But is there another way to do it? Something more efficient?

Thanks

EDIT: Inefficiencies I'm trying to avoid:

The way I envisaged this could work is:

  • An invoice is created. A due date is selected. This date is automatically added to a collection (I'm using Mongoid, but could be same as table)
  • Every day whenever (the gem) or a cron job will fire up a script/method/function that will check the aforementioned collection for entries. If finds entries with due dates within a certain time-range (due within 3 days for instance) would fire up another script/method to notify the customer that invoice is almost due.

My concern is if there are thousands of invoices, it might be quite resource intensive to check all the entries and check whether they're within a specific time range. So, is there a better solution?


Solution

  • As an alternative to cron you could use this gem for the delayed job:

    https://github.com/amitree/delayed_job_recurring

    Sidekiq has also periodic jobs(although it is only present in sidekiq pro):

    https://github.com/mperham/sidekiq/wiki/Ent-Periodic-Jobs