#Add sidekiq, sinatra and whenever to the Gemfile
gem 'sidekiq'
gem 'sinatra', require: false
gem "whenever", require: falseThen install the gem
$ bundle install#Generate file schedule.rb
$ wheneverize .This will create a new file in config/schedule.rb
#Write crontab
- Add these line at the top of
config/schedule.rbto run crontab in development mode.
if Rails.env == "development"
set :output, {error: "log/cron-error.log", standard: "log/cron.log"}
set :output, "#{path}/log/cron_log.log"
set :environment, :development
env :PATH, ENV['PATH']
end- Example
schedulefile
every 1.minute do
runner "User.exe"
endAnd this is the User class
class User < ApplicationRecord
class << self
def exe
User.first.increment! :point
end
end
end#Execute crontabs You will need to do this in order for your jobs to execute:
$ whenever --update-crontab####Some common used options
- You can list installed cron jobs using
crontab -l - Write down the crontab using
whenever -w - Stop/Clear crontab using
whenever -corcrontab -r