|
| 1 | +# Using Whenever gem on Mina Deployment |
| 2 | + |
| 3 | +Whenever gem is a popular gem to generate cron jobs easily on the server. |
| 4 | + |
| 5 | +Using mina gem to deploy our app, we can easily add the whenever command into the deployment script of mina |
| 6 | +so that on every deployment, mina will automatically run whenever to generate the whenever cron jobs. |
| 7 | + |
| 8 | +```ruby |
| 9 | +# Whenever tasks in deploy.rb |
| 10 | +desc "Clear scheduler" |
| 11 | +task :clear_scheduler => :environment do |
| 12 | + queue "bundle exec whenever --clear-crontab gooroo-app-cronjobs --set 'path=#{deploy_to}/current/'" |
| 13 | + queue %[echo "-----> Clear Scheduler Completed."] |
| 14 | +end |
| 15 | + |
| 16 | +desc "Invoke scheduler" |
| 17 | +task :exec_scheduler => :environment do |
| 18 | + queue "bundle exec whenever --update-crontab gooroo-app-cronjobs --set 'path=#{deploy_to}/current/'" |
| 19 | + queue %[echo "-----> Add Scheduler Completed."] |
| 20 | +end |
| 21 | +``` |
| 22 | + |
| 23 | +There is big caveat when we add the mina task in the deploy script. |
| 24 | +Whenever will generate many cron jobs each time we deploy. |
| 25 | +This is because whenever does not recognize the previous crob jobs due to the changing release paths in mina. |
| 26 | +Therefore, we need to specify the _identifier_ in the command line eg. `gooroo-app-cronjobs` |
| 27 | + |
| 28 | +Next, we just need to add the whenever tasks in the deploy block as below: |
| 29 | + |
| 30 | +```ruby |
| 31 | +desc "Deploys the current version to the server." |
| 32 | +task :deploy => :environment do |
| 33 | + deploy do |
| 34 | + invoke :'git:clone' |
| 35 | + invoke :'deploy:link_shared_paths' |
| 36 | + invoke :'bundle:install' |
| 37 | + invoke :'rails:db_migrate' |
| 38 | + invoke :'rails:assets_precompile' |
| 39 | + |
| 40 | + invoke :clear_scheduler |
| 41 | + invoke :exec_scheduler |
| 42 | + |
| 43 | + to :launch do |
| 44 | + queue "mkdir -p #{deploy_to}/#{current_path}/tmp" |
| 45 | + queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt" |
| 46 | + end |
| 47 | + end |
| 48 | +end |
| 49 | +``` |
0 commit comments