Is it possible to configure the Rails4 Capistrano3+whenever for each environment?

Asked 1 years ago, Updated 1 years ago, 53 views


in Rails 4 Configuring crontab on Capistrano3+whenever

I'd like to set up settings for each environment on schedule.rb, is it possible?

#schedule.rb
set: output, 'log/crontab.log'

every —hour do
  rake "xxxxxxxxx:delete_file",:environment=>"staging"
  rake "xxxxxxxxx:delete_file",:environment=>"production"
end

# deploy.rb
set:whenever_environment, "#{fetch(:stage)}"
set:whenever_identifier, ->{"#{fetch(:application)}_#{fetch(:stage)}"}

If you write like above,

when we did cap staging deployment and cap production deployment The following crontab will be completed.

#Begin Whenever generated tasks for:xxxxxxxx_staging
    0***/bin/bash-l-c'cd/home/xxx/www/html/xxxxxxx/staging/releases/20150509011936&RAILS_ENV=staging bundle execute xxxxx:delete_file --silent>log/crontab.log2>1
    0***/bin/bash-l-c'cd/home/xxx/www/html/xxxxxxx/staging/releases/20150509011936&RAILS_ENV=production bundle execute xxxxx:delete_file --silent>log/crontab.log2>1

# End Whenever generated tasks for:xxxxxxxx_staging

# Begin Whenever generated tasks for:xxxxxxxx_production
    0***/bin/bash-l-c'cd/home/xxx/www/html/xxxxxxx/production/releases/20150509012426&RAILS_ENV=staging bundle exec rake xxxxxxxx:delete_file --silent>>log/crontab.log2>1>
    0***/bin/bash-l-c'cd/home/xxx/www/html/xxxxxxx/production/releases/20150509012426&RAILS_ENV=production bundle exec rake xxxxxxxx:delete_file --silent>>log/crontab.log.2>gt;1>

# End Whenever generated tasks for :xxxxxxxx_production

The following results are ideal.

#Begin Whenever generated tasks for:xxxxxxxx_staging
    0***/bin/bash-l-c'cd/home/xxx/www/html/xxxxxxx/staging/releases/20150509011936&RAILS_ENV=staging bundle execute xxxxx:delete_file --silent>log/crontab.log2>1

# End Whenever generated tasks for:xxxxxxxx_staging

# Begin Whenever generated tasks for:xxxxxxxx_production
    0***/bin/bash-l-c'cd/home/xxx/www/html/xxxxxxx/production/releases/20150509012426&RAILS_ENV=production bundle exec rake xxxxxxxx:delete_file --silent>>log/crontab.log.2>gt;1>

# End Whenever generated tasks for :xxxxxxxx_production

ruby-on-rails whenever

2022-09-30 17:15

1 Answers

I think the environment variable can be used in schedule.rb, so
How about conditional branching?

every:hour do
  rake "xxxxxxxxx:delete_file",:environment=>"staging" if"#{environment}"=="staging"
  rake "xxxxxxxxx:delete_file", : environment=>"production" if"#{environment}"=="production 
end


2022-09-30 17:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.