

Rails4にて  
Capistrano3 + whenever で crontabの設定を行っています。

schedule.rbなどにて、環境ごとの設定を行いたいのですが、可能でしょうか？

[code]

    # schedule.rb
    set :output, 'log/crontab.log'
    
    every :hour do
      rake "xxxxxxxx:delete_file", :environment => "staging"
      rake "xxxxxxxx:delete_file", :environment => "production"
    end
    
    # deploy.rb
    set :whenever_environment, "#{fetch(:stage)}"
    set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
    
[/code]

上記の様な書き方だと  
cap staging deploy と cap production deployをした時に  
以下の様なcrontabが出来上がってしまいます。

[code]

    # Begin Whenever generated tasks for: xxxxxxxx_staging
        0 * * * * /bin/bash -l -c 'cd /home/xxx/www/html/xxxxxxxx/staging/releases/20150509011936 && RAILS_ENV=staging bundle exec rake xxxxx:delete_file --silent >> log/crontab.log 2>&1'
        0 * * * * /bin/bash -l -c 'cd /home/xxx/www/html/xxxxxxxx/staging/releases/20150509011936 && RAILS_ENV=production bundle exec rake xxxxx:delete_file --silent >> log/crontab.log 2>&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/xxxxxxxx/production/releases/20150509012426 && RAILS_ENV=staging bundle exec rake xxxxxxxx:delete_file --silent >> log/crontab.log 2>&1'
        0 * * * * /bin/bash -l -c 'cd /home/xxx/www/html/xxxxxxxx/production/releases/20150509012426 && RAILS_ENV=production bundle exec rake xxxxxxxx:delete_file --silent >> log/crontab.log 2>&1'
    
    # End Whenever generated tasks for: xxxxxxxx_production
    
[/code]

下記の様な結果が理想です。

[code]

    # Begin Whenever generated tasks for: xxxxxxxx_staging
        0 * * * * /bin/bash -l -c 'cd /home/xxx/www/html/xxxxxxxx/staging/releases/20150509011936 && RAILS_ENV=staging bundle exec rake xxxxx:delete_file --silent >> log/crontab.log 2>&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/xxxxxxxx/production/releases/20150509012426 && RAILS_ENV=production bundle exec rake xxxxxxxx:delete_file --silent >> log/crontab.log 2>&1'
    
    # End Whenever generated tasks for: xxxxxxxx_production
    
[/code]


CC BY-SA 3.0
2015-05-09T01:59:26.370
9975
2018-05-18T02:15:27.880
2018-05-18T02:15:27.880
754
9652
question
ruby-on-rails whenever
Rails4 Capistrano3 + whenever で環境ごとの設定を行うことは可能でしょうか？


schedule.rb の中で、environment 変数が使える状態と思いますので、  
条件分岐するのはいかがですか？

[code]

    every :hour do
      rake "xxxxxxxx:delete_file", :environment => "staging" if "#{environment}" == "staging"
      rake "xxxxxxxx:delete_file", :environment => "production" if "#{environment}" == "production 
    end
    
[/code]


CC BY-SA 3.0
2015-05-14T14:14:10.940
10186
2015-05-14T14:14:10.940
7471
9975
answer