Cron Job with Gem whenever combines Active Job in Rails

Tram Ho

Recently, I have been working on a project and I have encountered a task “Automatically create annual leave days for employees after finishing the allowable registration schedule”. That is, the task to set the run time according to the installation schedule is after the end of the vacation registration period. I went online and learned about setting up the runtime automatically in linux with the cron job and all tasks will be written in crontab, and in rails that support gem whenever. I have learned gem whenever and used in combination with Rails Active Job to serve the task’s solution.


Gem whenever

Gem whenever is a rails gem that helps to implement cronjobs in linux, performing a periodic automation job that we can configure. When using gem whenever, just like on the terminal in linux, we use crontab -e to add tasks and schedule the running, the key problem is there. Let’s understand whenever is an alarm clock and we can set the time in schedule.rb

I manually added the work manually to crontab with the command 0 1 * * * ~/Hoc/Rails/Project/SUN*/wsm_02/config/schedule.rb ie at 1:00 am the day will run the schedule file .rb

It is done manually, now we deploy gem whenever to perform this task automatically from the beginning.

We proceed to add Gemfile, then run bundle install


to install gem. Then we run the command

A config / schedule.rb file will be created. This file is where I will install the job configuration to run periodically.

Of course, linux will not automatically understand each schedule.rb file change, we must update the schedule to crontab with the command

Configuration in the schedule.rb file

A few ways to set up job runtime

Meaning of the numbers 15 0 1 12 * and the * signs see the following explanation:

So 15 0 1 12 * that an appointment for 0h 15 minutes on December 1 every year will run the task.

You can see above there are different types of Job runs: rake or runner, command and script

  • rake: Run Rails app rake tasks
  • runner: Run class methods of the Rails app
  • command: Run the linux commands
  • script: Run the script file

The crontab log can be set by setting up the schedule.rb file:

Configure a rake file

Every day at 1am has a schedule to run rake “auto_create_dayoff_plan: enforcement”

end_register_date: Installation date of the system to allow employees to register their own holidays

Job configuration

The above three code means that at the end of the monthly registration permission (end_register_date), ActiveJob (CreateDayoffPlanJob) will be activated. Everything then is just the task to run the Job that ActiveJob takes care of and the job in Service AutoCreateDayoffPlanService will be performed. There is already an article on viblo writing about ActiveJob, I will put the link here for everyone to read.


Above is my understanding and application of gem whenever. When tasks require an autorun, scheduled to run at a fixed time in the future, let’s use this gem. Posts with the understanding during the task should have possible misunderstandings, hope readers can comment on the comments to build together more perfect for readers later.

Share the news now

Source : Viblo