Fork me on GitHub

Keep Learning Conhecimento nunca é o bastante

Postado em
10 April 2012 @ 14:15

Tag(s)
Deploy, Rails, Ruby

Capistrano and whenever: updating the crontab of the runner user

When deploying Rails applications with Capistrano it’s a common practice to use two separated users: a deployer (the user who actually does the deploy process, copying files and all that) and a runner (the user that runs the application on the server).

If you need cronjobs you probably use the whenever gem. It’s an awesome tool to configure your scheduled jobs with a nice and easy DSL. It also has built-in integration with Capistrano, but there’s a caveat: it will update the crontab of the deployer, not the runner‘s one.

To solve that issue, I’ve dropped the “automatic” integration (just a simple require “whenever/capistrano”) and used a hook to invoke whenever passing a user option to specify which user’s crontab to update. Here’s the relevant code:

after "deploy:update_code" do
  run "cd #{release_path} && #{try_sudo} GEM_HOME=/opt/local/ruby/gems RAILS_ENV=production bundle exec whenever --clear-crontab #{application} --user #{runner}"
  run "cd #{release_path} && #{try_sudo} GEM_HOME=/opt/local/ruby/gems RAILS_ENV=production bundle exec whenever --update-crontab #{application} --user #{runner}"
end

The code above sets some env vars for sanity sake and runs whenever specifying the runner user as the crontab owner.

There’s another small tweak you’ll need. On your schedule.rb file, add the following code on the very first line:

env "PATH", ENV["PATH"]
every 10.minutes do
  # ...
end

This will avoid nasty path-related errors (the cronjobs won’t find the gem’s bins and all that). And… that’s it! Have fun! 🙂


Nenhum comentário até agora


Nenhum comentário ainda. Você pode ser o primeiro!

Deixe um comentário