Read more

Cronjobs: "Craken" is dead, long live "Whenever"

Tobias Kraze
November 30, 2012Software engineer at makandra GmbH

Our old solution for cronjobs, the "craken" plugin, is no longer maintained and does not work on Rails 3.2+.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

We will instead use the whenever gem Show archive.org snapshot .

"Whenever" works just like "craken", by putting your rake tasks into the server's cron table. Everything seems to work just like we need it.

Installation for new projects

  1. Add "whenever" to your Gemfile:

     group :deploy do
       gem 'whenever', require: false
     end
    
  2. Add it to your config/deploy.rb:
    For Capistrano 2

     set :application, 'PROJECT_NAME' # might be already set
     set :whenever_command, "bundle exec whenever"
     require 'whenever/capistrano'
    

    Note that "whenever" will not use a :cron role, but instead default to run cronjobs on the :db server. That's just fine for us.

    For Capistrano 3

     set :application, 'PROJECT_NAME' # might be already set
     set :whenever_roles , [:cron]
     set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
    

    Note that "whenever" will now use a :cron role for all whenever tasks without a specific role provided in the attribute roles:

  3. Add to your Capfile (For Capistrano 3 only):

      require 'whenever/capistrano'       
    
  4. Run b wheneverize.

  5. Edit config/schedule.rb, for example

    every 15.minutes, roles: [:cron] do
      rake "makandra_de_website:update_tweet_cache"
    end
    
  6. You can check the crontab that would be written by running b whenever. This does not change anything.

Migration from craken

Do all of the above, and also

  1. Remove all blocks and hooks belonging to craken from your config/deploy.rb.
  2. Delete vendor/plugins/craken.
  3. Check if you have lib/tasks/craken.rb. If so, delete that, too.
  4. Make sure you have moved all cronjobs from config/craken/raketab to config/schedule.rb. Then delete the first file.
  5. After the first deploy: Update the crontab by logging in on the server and run crontab -e. Remove the lines added by "craken" (they are clearly marked by a comment).
Tobias Kraze
November 30, 2012Software engineer at makandra GmbH
Posted by Tobias Kraze to makandra dev (2012-11-30 16:51)