I'm new to rails and want to run a batch file/schedule task daily once at midnight that checks which entries have expired. Every record in the table has a closing_date and after that time, such records must be inactive.(status active=false on DB). so basically it will run 2 SQL queries to fetch all records and then flag another field to inactive for records that are outdated.I'm working with Rails 5.
How should I go about this-gem(rufus,whatever,clockwork or any other gem) or simply some system tool for cronjob?I'm going to change my DB to PostgreSQL so will that impact? Any suggestions or sample code anyone can share to get an idea.
In brief: You can use whenever Gem for this purpose.
You need to create a rake task for where you will write your SQL. Then schedule it as cron job using whenever
Detailed explanation
First step will be creating a rake task. You can do this using the console
This will create a file named
my_namespace.rake
in thelib/tasks
folder with initial content likeYou can check whether your task is running properly by running
in the console.
Now you need to schedule the job using the
whenever
gem.gem 'whenever', :require => false
inGemfile
.bundle install
wheneverize
command in terminal. This will create aschedule.rb
file in config folder .Add the following code to schedule your rake task:
Run this command:
whenever --update-crontab
. In case your environment is development, use command:whenever --update-crontab --set environment='development'
(see this question.crontab -l
to check whether cron job has been added.