Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 22, 2022 09:17 am GMT

Cron Job With Whenever Gem Setup Ruby On Rails

Introduction About the Cron Job

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.

Image description

Whats 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 the cron tool. The info of the process that will run the cron tool is stored in a file called crontab.

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 e-commerce application.

Step 1Install whenever gem ( https://github.com/javan/whenever )

# Cronjobsgem 'whenever', require: false

Step 2Go to the project and run this

cd /my-great-projectbundle exec wheneverize .

add log file path in the schedule.rb

# schedule.rbset :output, {:error => log/cron_error_log.log, :standard => log/cron_log.log}

Step 3 Create a rake task

touch lib/tasks/first_sample_task.rake

On our task, we will simply print a message.

# lib/tasks/first_sample_task.rakedesc 'First Whenever rake task'task first_sample_task: :environment do  Rails.logger.info "First Sample Task"end

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

# See all tasksbundle exec rake --tasks

Step 4 Add in the schedule file

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)  |  |  |  |  |*  *  *  *  *  command to execute

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

# schedule.rbevery '*/15 * * * *' do  rake 'first_sample_task'end

For Reference
https://crontab.guru/#/15_*_

Step 5Update crontab

whenever --update-crontab# crontab file */5 * * * * /bin/bash -l -c 'cd OUR_PROJECT_PATH && RAILS_ENV=production bundle exec rake first_sample_task - silent >> log/cron_log.log 2>> log/cron_error_log.log'

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

# log/cron_log.logI, [2022-05-04T18:05:02.890007 #98613]  INFO -- : First Sample TaskI, [2022-05-04T18:20:02.944687 #98649]  INFO -- : First Sample Task

Step 6 Crontab important commands

# get crontab listcrontab -l# delete all crontabcrontab -r# edit crontabcrontab -e

Step 7For deployment need to import whenever file in capfile and deploy.rb

# capfilerequire 'whenever/capistrano'# deploy.rbrequire 'whenever'require 'whenever/capistrano'

If this guide has been helpful to you and your team please share it with others!


Original Link: https://dev.to/kanani_nirav/cron-job-with-whenever-gem-setup-ruby-on-rails-4nli

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To