我在Sinatra应用程序中使用when gem创建一个cron作业。我正在使用/ capistrano,以便在部署时创建/更新作业。这是我的部署脚本的随时设置:
require 'whenever/capistrano'
set :environment_variable, 'RACK_ENV'
set :whenever_roles, ->{ :app }
set :whenever_command, ->{ [:bundle, :exec, :whenever] }
set :whenever_command_environment_variables, ->{ { rack_env: fetch(:whenever_environment), rails_env: nil } }
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
set :whenever_environment, ->{ fetch :rack_env, fetch(:stage, "production") }
set :whenever_variables, ->{ "environment=#{fetch :whenever_environment}" }
set :whenever_update_flags, ->{ "--update-crontab #{fetch :whenever_identifier} --set #{fetch :whenever_variables}" }
set :whenever_clear_flags, ->{ "--clear-crontab #{fetch :whenever_identifier}" }
当我部署时,我可以看到capistrano运行crontab更新,似乎将使用正确的环境变量:
INFO [8b4f49a6] Running ~/.rvm/bin/rvm default do bundle exec whenever --update-crontab sidekiq-monitor-ua_staging --set environment=staging --roles=app as deploy@10.0.254.37
DEBUG [8b4f49a6] Command: cd /var/www/sidekiq-monitor-ua/releases/20160725170122 && ( RACK_ENV=staging RAILS_ENV= ~/.rvm/bin/rvm default do bundle exec whenever --update-crontab sidekiq-monitor-ua_staging --set environment=staging --roles=app )
DEBUG [8b4f49a6] [write] crontab file updated
但是,这是我在crontab中获得的(这是生成部署到我的暂存环境):
# Begin Whenever generated tasks for: sidekiq-monitor-ua_staging
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /var/www/sidekiq-monitor-ua/releases/20160725170122 && RAILS_ENV=staging bundle exec rake log_sidekiq_status --silent >> /dev/null 2>&1'
# End Whenever generated tasks for: sidekiq-monitor-ua_staging
因此,当crontab运行rake任务时,RACK_ENV未设置且代码无法正常运行,因为它默认为开发环境。
我要么缺少某些东西,要么有错误的配置。我希望有人可以提供帮助。
答案 0 :(得分:0)
每当github README关于工作类型时提到的environment_variable:https://github.com/javan/whenever#define-your-own-job-types
此处默认的佣金工作
job_type:rake," cd:path&& :environment_variable =:环境包exec rake:task --silent:output" job_type:runner," cd:path&& bin / rails runner -e:environment':task' :输出"
:environment_variable将默认为' RAILS_ENV'
你可以在下面试试。
require 'whenever/capistrano'
set :environment_variable, 'RACK_ENV'
set :whenever_roles, ->{ :app }
set :whenever_command, ->{ [:bundle, :exec, :whenever] }
set :whenever_command_environment_variables, ->{ {} }
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
set :whenever_environment, ->{ fetch :rack_env, fetch(:stage, "production") }
set :whenever_variables, ->{ "'environment=#{fetch :whenever_environment}&environment_variable=RACK_ENV'" }
set :whenever_update_flags, ->{ "--update-crontab #{fetch :whenever_identifier} --set #{fetch :whenever_variables}" }
set :whenever_clear_flags, ->{ "--clear-crontab #{fetch :whenever_identifier}" }