Skip to content

Commit 477cd0e

Browse files
committed
TIL Server: Using Whenever gem on Mina Deployment
Whenever gem is a popular gem to generate cron jobs easily on the server. Using mina gem to deploy our app, we can easily add the whenever command into the deployment script of mina so that on every deployment, mina will automatically run whenever to generate the whenever cron jobs. ```ruby desc "Clear scheduler" task :clear_scheduler => :environment do queue "bundle exec whenever --clear-crontab gooroo-app-cronjobs --set 'path=#{deploy_to}/current/'" queue %[echo "-----> Clear Scheduler Completed."] end desc "Invoke scheduler" task :exec_scheduler => :environment do queue "bundle exec whenever --update-crontab gooroo-app-cronjobs --set 'path=#{deploy_to}/current/'" queue %[echo "-----> Add Scheduler Completed."] end ``` There is big caveat when we add the mina task in the deploy script. Whenever will generate many cron jobs each time we deploy. This is because whenever does not recognize the previous crob jobs due to the changing release paths in mina. Therefore, we need to specify the _identifier_ in the command line eg. `gooroo-app-cronjobs` Next, we just need to add the whenever tasks in the deploy block as below: ```ruby desc "Deploys the current version to the server." task :deploy => :environment do deploy do invoke :'git:clone' invoke :'deploy:link_shared_paths' invoke :'bundle:install' invoke :'rails:db_migrate' invoke :'rails:assets_precompile' invoke :clear_scheduler invoke :exec_scheduler to :launch do queue "mkdir -p #{deploy_to}/#{current_path}/tmp" queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt" end end end ```
1 parent 8590a8e commit 477cd0e

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)