A scheduled task is very important in Ruby on Rails application. Ruby on Rails does not have this type of “out-of-the-box” feature, but we can use the Cron job to achieve the purpose. Cron can be scheduled to send a reminder email, or scheduling to crawl something from another website on daily basis. In this blog, let’s quickly go through how to use Cron job.
What is CRON?
Cron task is a time-based job scheduler in Unix-like computer operating systems. Cron can be used to schedule jobs to run periodically at fixed times, dates, or intervals. Cron is most suitable for scheduling repetitive tasks in Ruby on Rails applications.
Let’s take an example of sending a daily digest email to persons or users. In this case, we might have a mailer like this using crontab:
The user can run any method from the terminal. Here I’m running an action inside a model i.e schedule_rb_method.
Open “app/models/reminder.rb” and then add the method for running an action as shown below.
Set the default rvm to the project.
run rvm --default use 2.5.0
Creating a Crontab:
Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule.
Users can have their own individual Crontab files in the system.
It’s pretty easy to start working with cron jobs in rails. You can start editing your cron tasks using the crontab command on your terminal:
Open your terminal: run crontab -e
This command will open a text file in your terminal’s default editor.
We are going to enter a line into this crontab file. It will have the following structure:
[timings] [switch to your app directory] && [path to ruby] script/runner -e [environment] “[model class name] . [ (run method name) path to cron log]”
By looking at the above line to understand: Run a task every day at 10 am.
If you try and save your crontab and you receive errors, make sure the whole command is in one line on your terminal.
If your model method runs OK, you should either get some mail as mentioned in the cron. However, if there is an error, you must check your cron method to check the log you created in rails to understand the error.
Once you’re in the editor, you can start creating and editing cron tasks in rails. To schedule a cron task, you need to define when the task will run, then define the command to trigger that task.
There are 5 variables that can be changed inside a cron task in Ruby on Rails application:
1st variable: Minute, which can be specified from 0 to 59 min
2nd variable: Hour, which can be specified from 0 to 23 hr
3rd variable: Day of Month, which can be specified from 1 to 23 day
4th variable: Month, which can be specified from 1 to 12 month
5th variable: Day of Week, which can be specified from 0 - 6 (Sunday = 0) week days
If the variable is a “*” it means for all.
If the variable is a series of number split by a comma “,”, it means OR condition on each of the specified number in Cronjobs.
minute hour day month weekday
0 30 * * * * example command to be executed
While cron jobs are useful and can get the job done in many instances, you should be careful in your decision of how to implement scheduled and delayed tasks in Ruby on Rails application.
In rails, Cronjobs are best for tasks that are not highly critical or essential to your application’s functionality or performance. They should also execute in a reasonably short amount of time so that they don’t bog down your rails server(s).