via Deploy Hook
UPDATE 4.10.2013: Updating these instructions to use bundle exec, per chintan/Nic Pillinger
UPDATE 11.4.2013: Updating these instructions to use helper methods to determine which instances to run the command on
If you want your crontab updated by whenever on deploy, make a file named deploy/before_restart.rb and stick this in it:
on_app_master do # the following updates the crontab upon deployment run "cd #{config.release_path}; bundle exec whenever --set environment=#{config.framework_env} --update-crontab '#{config.app}_#{config.framework_env}'" end
This is called a ‘deploy hook’ and here’s more info.
via Deploy Hook using Binstubs
Since Engine Yard automatically installs your binstubs, you could also replace “bundle exec whenever” with “ey_bundler_binstubs/whenever”. If you want to be able to run “bin/whenever” you’ll have to symlink to ey_bundler_binstubs – but remember it’s in the current deploy directory, so you’ll have to do this per-deploy – which means you could stick this in the same before_restart.rb file (obviously it would need to be above the whenever block):
run "ln -nfs #{config.release_path}/ey_bundler_binstubs #{config.release_path}/bin" on_app_master do # the following updates the crontab upon deployment run "cd #{config.release_path}; bin/whenever --set environment=#{config.framework_env} --update-crontab '#{config.app}_#{config.framework_env}'" end
Note for Utility Instances
UPDATE 11.4.2013: If you also want your cron jobs to work on all app servers (not just your app master), change “on_app_master” above to “on_app_servers”. For app servers and utilities, use “on_app_servers_and_utilities”. For utilities only, use “on_utilities”. As far as I can tell, there does not appear to be an option for only app master and utilities; if you need this, write two identical blocks with “on_app_master” and “on_utilities”.
via Custom Chef Recipes
Why would you also need to do a custom chef recipe? Because if you rebuild your cluster and don’t redeploy, your cron jobs won’t be created.
UPDATE 4.17.2012: If you’re on EY and not using custom chef recipes, you should start. A whenever recipe has been written and is detailed here. Info on how to start using custom chef recipes is here. It’s not so bad, really. One gotcha: your “appname” in the recipe is not your rails application name, but your EY application name – these can be different things!
July 19, 2012 at 9:28 am
cant you use bundle exec such as :
run “cd #{release_path}; bundle exec whenever –set environment=#{@rails_env}; bundle exec whenever –update-crontab ‘yourAppNameHere'”
with bundle exec there is no need for step 1) install gem whenever
LikeLike
July 19, 2012 at 10:06 am
Sure you can… but since using custom chef is a better option here (for engine yard) I updated the post to advise using that instead.
Always more than one way to do it.
LikeLike
November 6, 2012 at 5:42 pm
Hi Sara, Thanks for the post. Secondary note for your readers just in case they need a reminder. If you only want this to run on a util server be sure to wrap the above deploy hook stuff with current_role == ‘util’.
Cheers and Thanks again.
LikeLike
April 10, 2013 at 10:16 am
first of all, thanks for this – helped sort my crontab/whenever problems out. secondly – chintan is correct above – you shouldn’t be installing any gems on your EY instances even in a chef recipe.
Using bundle exec means you’ll get the version of whenever you have bundled with your app and is the correct way to solve this problem.
cheers
Nic
LikeLike
April 10, 2013 at 10:47 am
Thanks, Nic. I’ve updated the post entirely to account for all the new info I’ve learned since Sep 2011.
LikeLike
April 23, 2013 at 11:00 am
Thanks for the update….Here is my deploy hook (that works) on ey solo instance for reference:
In before_restart.rb:
@rails_env = node[:environment][:framework_env]
run “cd #{release_path}; bundle exec whenever –set environment=#{@rails_env} –update-crontab fleetapp”
Also, as pointed out be sure to wrap this in current_role == ‘util’ stuff for a cluster setup.
Thanks
LikeLike