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

Updated . Posted . Visible to the public.

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

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
Last edit
Thomas Tasler
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2012-11-30 15:51)