Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-plugins

Running Rake task at variable intervals


I am using whenever(https://github.com/javan/whenever) ruby gem to insert a crontab entry in after_create callback of model X. This entry instructs the job to run every 45 minutes. But I want to give the control to the user where by he can specify the time interval. So the process goes like this:

  1. The user creates a Model X, I insert a crontab entry to execute a job after 45 minutes.
  2. User sees that 45 minutes is not good enough, he wants to be able to change the time interval from UI to 30 minutes (please note that this is a HTML/UI control where he should be able to do that).
  3. Later on he should be able to change the time interval any number of times.

Any gem or any idea on how we can accomplish this is appreciated. I am open to any version of rails and can start implementation in anyone since this feature forms the core of functionality.

Thanks.


Solution

  • I don't like the approach of your solution. It means, that if in the future your application will have 2k users, you'll start 2k crontabs?

    Instead of creating one crontab for every user, simply create one every minute. Store in the User model the information about the crontab frequency for that user and the last time the crontab was performed.

    Then, when the main crontab starts every minute, check which users have a last_crontab_at + frequency < Time.current and run the script for these users.

    In this way, the application will scale regardless the number of users.

    Also, remember crontabs are local to the server. What will happen if you have to change the server or create a cluster? I suggest you to think about using a background processing service or a queue.