如果您了解创建cron作业的gem,那么这个问题可能才有意义。我的schedule.rb中有一项任务,如
every 1.day, :at => '4am' do
command "cd #{RAILS_ROOT} && rake thinking_sphinx:stop RAILS_ENV=#{RAILS_ENV}"
command "cd #{RAILS_ROOT} && rake thinking_sphinx:index RAILS_ENV=#{RAILS_ENV}"
command "cd #{RAILS_ROOT} && rake thinking_sphinx:start RAILS_ENV=#{RAILS_ENV}"
end
但是当我使用
更新我的crontab时whenever --update-crontab appname --set environment=production
cron作业仍然有RAILS_ENV =开发。我的生产和开发任务现在是一样的,我只需要改变环境变量,因为thinking_sphinx需要知道当前的环境。关于如何做到这一点的任何想法?
谢谢!
答案 0 :(得分:86)
每当没有检测到您的环境时,它只是默认使用生产。 您可以使用set:
为所有作业设置环境set :environment, 'staging'
或按工作:
every 2.hours do
runner 'My.runner', :environment => 'staging'
end
答案 1 :(得分:24)
不要写RAILS_ENV变量。它应该自动设置。
every 1.day, :at => '4am' do
command "cd #{RAILS_ROOT} && rake thinking_sphinx:stop"
command "cd #{RAILS_ROOT} && rake thinking_sphinx:index"
command "cd #{RAILS_ROOT} && rake thinking_sphinx:start"
end
它适用于我的应用:
every 4.days do
runner "AnotherModel.prune_old_records"
end
$ whenever --set environment=production
0 0 1,5,9,13,17,21,25,29 * * /Users/weppos/Sites/git/app/script/runner -e production "AnotherModel.prune_old_records"
$ whenever --set environment=development
0 0 1,5,9,13,17,21,25,29 * * /Users/weppos/Sites/git/app/script/runner -e development "AnotherModel.prune_old_records"
答案 2 :(得分:17)
Whenever (0.9.2)
使用@environment
变量进行环境检查:
case @environment
when 'production'
every 1.minutes do
rake "user:take_sample"
end
when 'development'
every 1.minutes do
rake "user:dev_sample"
end
end
答案 3 :(得分:16)
如果你正在使用bundler和capistrano,你可能想要尝试的其他东西。
在deploy.rb文件中,当您设置:when_command,不要时,只需执行以下操作:
set :whenever_command, "bundle exec whenever"
相反,这样做:
set(:whenever_command) { "RAILS_ENV=#{rails_env} bundle exec whenever" }
现在,在加载schedule.rb文件时,RAILS_ENV环境变量将可用,因此在schedule.rb中,您现在可以执行此操作:
set :environment, ENV['RAILS_ENV']
瞧!你准备好了。
答案 4 :(得分:10)
注意你是否想要传递多个参数。
你必须这样做:
whenever --update-crontab appname --set 'environment=production&cron_log=/path/to/log'
答案 5 :(得分:8)
这个问题已经开放了很长时间,所以我想我会分享0.9.7,Ruby 2.4.0和RAILS 5.0.1时的工作原理。在前面提到的答案中有很多接近尝试,但语法错误困扰他们。以下是有效的方法,也是非常简单的方法。
require File.expand_path(File.dirname(__FILE__) + '/environment')
set :output, {:standard => 'log/cron_log.log', :error => 'log/cron_error_log.log'}
env :PATH, ENV['PATH']
every :day, :at => '10am' do
rake "somejob:run", :environment => @environment
end
whenever --set 'environment=development' --update-crontab
0 10 * * * / bin / bash -l -c'cd / my / rails / app&& RAILS_ENV =开发包exec rake somejob:run --silent>> log / cron_log.log 2>>登录/ cron_error_log.log'
whenever --set 'environment=production' --update-crontab
0 10 * * * / bin / bash -l -c'cd / my / rails / app&& RAILS_ENV =生产包exec rake somejob:run --silent>> log / cron_log.log 2>>登录/ cron_error_log.log'
希望这可以帮助别人。 快乐编码!
答案 6 :(得分:6)
最新的任何时候都可以轻松集成Capistrano。您可以向deploy.rb添加以下内容:
set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}-#{stage}" }
require "whenever/capistrano"
答案 7 :(得分:0)
我遇到一个问题,即每次cron作业都没有设置环境-选择了/ usr / bin / bundle而不是/ usr / local / bin / bundle。
解决方案是在schedule.rb的顶部添加以下内容
env 'PATH', ENV['PATH']
答案 8 :(得分:-1)
在config / schedule.rb顶部添加以下代码行。
ENV['RAILS_ENV'] = "#{@pre_set_variables[:environment]}"
并使用以下命令更新crontab。
whenever --update-crontab pvcnxt --set 'environment=production'
然后最后使用命令
重启crontabservice crond restart
多数民众赞成!
最终 config / schedule.rb 看起来就是这样
ENV['RAILS_ENV'] = "#{@pre_set_variables[:environment]}"
env :PATH, ENV['PATH']
require File.expand_path(File.dirname(__FILE__) + "/environment")
set :output, "#{Rails.root}/logs/cron_log_#{ENV['RAILS_ENV']}.log"
every 1.day, :at => '00:00 am' do
command "cd #{Rails.root}/lib/tasks && rake clean__posts_table_rake"
end
答案 9 :(得分:-6)
我会考虑使用“rake”快捷方式使其更清晰:
every 1.day, :at => '4am' do
rake "thinking_sphinx:stop"
rake "thinking_sphinx:index"
rake "thinking_sphinx:start"
end