37

如果您了解用于创建 cron 作业的when 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=development。我现在的生产和开发任务是一样的,我只需要改变环境变量,因为thinking_sphinx需要知道当前的环境。关于如何做到这一点的任何想法?

谢谢!

4

10 回答 10

97

每当没有检测到您的环境时,它只是默认使用生产。您可以使用 set 为所有作业设置环境:

set :environment, 'staging' 

或每个工作:

every 2.hours do 
  runner 'My.runner', :environment => 'staging' 
end 
于 2010-09-03T14:22:34.820 回答
26

不要写 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"
于 2009-07-01T17:47:31.653 回答
23

为了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
于 2014-04-30T14:50:42.867 回答
16

如果您使用的是 bundler 和 capistrano,您可能还想尝试其他方法。

在你的 deploy.rb 文件中,当你设置 :whenever_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']

瞧!你准备好了。

于 2011-02-07T23:48:45.273 回答
14

这个问题已经开放了很长时间,所以我想我会在 0.9.7、Ruby 2.4.0 和 RAILS 5.0.1 时分享适用的内容。在前面提到的答案中,有很多密切尝试,但语法错误困扰着他们。以下是有效的方法,并且是非常简单的方法。

日程安排.rb

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

更新 crontab(dev)

whenever --set 'environment=development' --update-crontab

结果(开发)

0 10 * * * /bin/bash -l -c 'cd /my/rails/app && RAILS_ENV=development bundle exec rake somejob:run --silent >> log/cron_log.log 2>> log/cron_error_log.log'

更新 crontab(prod)

whenever --set 'environment=production' --update-crontab

结果(产品)

0 10 * * * /bin/bash -l -c 'cd /my/rails/app && RAILS_ENV=production bundle exec rake somejob:run --silent >> log/cron_log.log 2>> log/cron_error_log.log'

希望这可以帮助某人。快乐编码!

于 2017-03-02T16:16:54.433 回答
12

如果您想将多个参数传递给任何时候,请注意。
你必须这样做:

whenever --update-crontab appname --set 'environment=production&cron_log=/path/to/log'
于 2011-02-02T12:18:11.750 回答
6

最新的任何时候都可以轻松集成 Capistrano。您可以将以下内容添加到 deploy.rb:

set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}-#{stage}" }

require "whenever/capistrano"
于 2011-05-29T18:50:16.727 回答
1

我遇到了一个问题,即每当 cron 作业时都没有设置环境 - 拾取 /usr/bin/bundle 而不是 /usr/local/bin/bundle。

解决方案是将以下内容添加到 schedule.rb 的顶部

env 'PATH', ENV['PATH']
于 2019-05-31T13:45:34.433 回答
0

在 config/schedule.rb 顶部添加以下代码行。

 ENV['RAILS_ENV'] = "#{@pre_set_variables[:environment]}"

并使用以下命令更新 crontab。

whenever --update-crontab pvcnxt --set 'environment=production'

然后最后使用命令重新启动 crontab

service 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
于 2015-12-02T12:45:54.417 回答
-9

我会考虑使用“rake”快捷方式使其更干净:

every 1.day, :at => '4am' do
  rake "thinking_sphinx:stop"
  rake "thinking_sphinx:index"
  rake "thinking_sphinx:start"
end
于 2009-07-29T19:19:05.253 回答