37

この質問は、cronジョブを作成するためのwhengemについて知っている場合にのみ意味があります。私の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 に答える
12

when に複数のパラメータを渡したい場合は注意してください。
次のようにする必要があります。

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 に答える
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 に答える