Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Brayan Vasquez
Brayan Vasquez

Posted on

     

Ruby on Rails Cron Jobs Using Whenever

Hi Everyone!. In this post, I want to share with you a little guide that will show you how to use cron jobs in a Ruby on Rails application.

Requirements

  • Ruby (I use ruby 2.7.1p83)
  • Ruby on Rails (I use Rails 6.0.3.4)

Note: I advise you to install ruby using a version manager likervm orrbenv. Also, I recommend theGoRails Tutorial that will guide you throughout the process.

What's a Cron Job?

A cron job is a process that is scheduled to run at a certain time (every minute, day, week, or month). On Unix systems, we can run and manage these types of processes using thecron tool. The info of the process that will run thecron tool is stored in a file calledcrontab.

Examples of the use of cron jobs are the following:

  • Generate reports. e.g, a report of daily payments from an e-commerce application.
  • Send reminders to users. e.g, send offer reminders from an ecommerce application.
  • Modify the status of the records. e.g, canceling payments that exceed a time limit to pay.

We can see our cron jobs using the following command

crontab-l
Enter fullscreen modeExit fullscreen mode

To delete user jobs we can use the following command

crontab-r
Enter fullscreen modeExit fullscreen mode

And to edit our crontab file, we can use the following command

crontab-e
Enter fullscreen modeExit fullscreen mode

The structure of a cron job is the following

.--------------- minute(0-59) |  .------------ hour(0-23)|  |  .--------- day of month(1-31)|  |  |  .------ month(1-12)|  |  |  |  .--- day of week(0-6)(sunday=0 or 7)  |  |  |  |  |*****commandto execute
Enter fullscreen modeExit fullscreen mode

An example of how to define a cron job to execute every 5 minutes

*/5****  /home/user/test.rb
Enter fullscreen modeExit fullscreen mode

Setup the project

Now that we know what's a cron job, we can start to write code. First, we are going to create our rails project.

rails new whenever_example
Enter fullscreen modeExit fullscreen mode

Add whenever gem

Whenever is a gem that helps us to define cron jobs in a readable way. So, we will add this gem to ourGemfile.

# Gemfile# Cron jobsgem'whenever',require:false
Enter fullscreen modeExit fullscreen mode

Install our dependencies

bundleinstall
Enter fullscreen modeExit fullscreen mode

Create a rake task

We will use a rake task to run in our cron job. First, create a.rake file.

# Create the rake tasktouchlib/tasks/whenever_test.rake
Enter fullscreen modeExit fullscreen mode

On our task, we will simply print a message.

# lib/tasks/whenever_test.rakedesc'Whenever rake task test'taskwhenever_call: :environmentdoRails.logger.info"Whenever task"end
Enter fullscreen modeExit fullscreen mode

To see the rake tasks of our Rails application, we can use the following command.

# See all tasksbundleexecrake--tasks# See our taskbundleexecrake--tasks |grepwhenever_call# rake whenever_call                      # Whenever rake task test
Enter fullscreen modeExit fullscreen mode

We can execute our task to check that it works.

bundleexecrake whenever_call
Enter fullscreen modeExit fullscreen mode

In our logs file we should see the message we define.

taillog/development.log# Whenever task
Enter fullscreen modeExit fullscreen mode

Define our cron job

First, we have to setup the whenever gem.

bundleexecwheneverize.# [add] writing `./config/schedule.rb'# [done] wheneverized!
Enter fullscreen modeExit fullscreen mode

This will create theconfig/schedule.rb where we will define our cron jobs. We will create a simple task that will be executed every 2 minutes.

# config/schedule.rb# ....every2.minutedorake'whenever_call'end
Enter fullscreen modeExit fullscreen mode

Now, we have to update our crontab file.

whenever--update-crontab
Enter fullscreen modeExit fullscreen mode

If we list our cron jobs in the crontab file, we will see something like the following.

# crontab file0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58**** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'
Enter fullscreen modeExit fullscreen mode

If we see in our production logs, we can see the following.

# log/production.logI,[2020-11-15T20:50:02.590009#53971]  INFO -- : Whenever taskI,[2020-11-15T20:52:02.914487#54387]  INFO -- : Whenever task
Enter fullscreen modeExit fullscreen mode

As, you can see the logs have 2 minutes of difference.

However, you can see that the task is logging into ourproduction.log. If you want to run the cron jobs in development environment, we can use the following.

whenever--update-crontab--setenvironment=development
Enter fullscreen modeExit fullscreen mode

Of course, we could specify more when the task runs. Here are some examples:

Scheduling a task every day at 3:00am

# config/schedule.rb# ....every1.day,at:'3:00 am'dorake'whenever_call'end
Enter fullscreen modeExit fullscreen mode
# crontab file0 3*** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'
Enter fullscreen modeExit fullscreen mode

Scheduling a task every 2 days at 3:00am and 5:30pm

# config/schedule.rb# ....every2.day,at:['3:00 am','5:30 pm']dorake'whenever_call'end
Enter fullscreen modeExit fullscreen mode
# crontab file0 3 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'30 17 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31** /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake whenever_call --silent'
Enter fullscreen modeExit fullscreen mode

Scheduling a task on monday and sunday at 6:00pm

# config/schedule.rb# ....every[:monday,:sunday],at:'6:00 PM'dorake'whenever_call'end
Enter fullscreen modeExit fullscreen mode
# crontab file0 18** 1,0 /bin/bash-l-c'cd OUR_PROJECT_PATH && RAILS_ENV=development bundle exec rake whenever_call --silent'
Enter fullscreen modeExit fullscreen mode

If you want to clear the contents of the crontab file, you can also use the following command.

# Clear crontabwhenever-c
Enter fullscreen modeExit fullscreen mode

Final Words

Thanks for reading this post and you can find more info about whenever in itsofficial github repo.

If you are also interested in know how to create scheduled jobs in Elixir, I recommend you this amazing post3 ways to schedule tasks in Elixir I have learned in 3 years working with it.

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
danielltw profile image
Daniel Leong 🇸🇬
Just another Singaporean working overseas.
  • Location
    Wellington, New Zealand
  • Education
    Bachelor of Engineering (Hons), major in Network Engineering
  • Work
    Developer @ Discolabs
  • Joined

In a distributed world, whenever is not going to work very well.

You might want to explore further with your background job queue system and ways to schedule jobs with it. Like for Sidekiq, there is Sidekiq Scheduler.

Have fun exploring the work of scheduled tasks/jobs.

CollapseExpand
 
brayvasq profile image
Brayan Vasquez
I'm from Colombia and I'm a Software Engineer at Kommit.co. I'm currently working with Ruby on Rails.
  • Email
  • Location
    Caldas, Colombia
  • Work
    Software Engineer at Kommit
  • Joined
• Edited on• Edited

Hi Daniel!.

For a monolithic Rails application, whenever seems to work fine.

I didn't know about Sidekiq-Scheduler, it looks cool. Definitely, I will give it a look.

Thank you so much for your recommendations!

PS: It would be very helpful if you could create a post talking about distributed systems and scheduled tasks/jobs.

CollapseExpand
 
danielltw profile image
Daniel Leong 🇸🇬
Just another Singaporean working overseas.
  • Location
    Wellington, New Zealand
  • Education
    Bachelor of Engineering (Hons), major in Network Engineering
  • Work
    Developer @ Discolabs
  • Joined

Even with a monolithic application, it will be worth while to package your whole stack into docker containers. Service Oriented Architecture.

App as a single container, database as another container and redis/memcache as another container. You could package this whole stack with a single docker-compose file and your app on a docker image repository. This allows you to move and deploy your full stack with ease, on any machine you are working with.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm from Colombia and I'm a Software Engineer at Kommit.co. I'm currently working with Ruby on Rails.
  • Location
    Caldas, Colombia
  • Work
    Software Engineer at Kommit
  • Joined

More fromBrayan Vasquez

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp