Skip to content

Instantly share code, notes, and snippets.

@trandaison
Last active January 15, 2020 17:08
Show Gist options
  • Save trandaison/ade9f47fb4778c16041b373521907031 to your computer and use it in GitHub Desktop.
Save trandaison/ade9f47fb4778c16041b373521907031 to your computer and use it in GitHub Desktop.
Simple example about Whenever gem

#Add sidekiq, sinatra and whenever to the Gemfile

gem 'sidekiq'
gem 'sinatra', require: false
gem "whenever", require: false

Then 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.rb to 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 schedule file
every 1.minute do
  runner "User.exe"
end

And 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 -c or crontab -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment