Ruby-on-rails 随时使用 Rails cron,设置环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1070231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails cron with whenever, setting the environment
提问by Tony
This question will probably only make sense if you know about the whenever gem for creating cron jobs. I have a task in my schedule.rb like
这个问题可能只有在您了解用于创建 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
However when I update my crontab using
但是,当我使用更新我的 crontab 时
whenever --update-crontab appname --set environment=production
the cron jobs still have RAILS_ENV=development. My tasks on production and development are the same right now, I just need to change the environment variable because thinking_sphinx needs to know the current environment. Any ideas on how to do this?
cron 作业仍然有 RAILS_ENV=development。我现在的生产和开发任务是一样的,我只需要更改环境变量,因为 thinking_sphinx 需要知道当前环境。关于如何做到这一点的任何想法?
Thanks!
谢谢!
采纳答案by Snips
I would consider using the "rake" shortcut to make it even cleaner:
我会考虑使用“耙子”快捷方式使其更干净:
every 1.day, :at => '4am' do
rake "thinking_sphinx:stop"
rake "thinking_sphinx:index"
rake "thinking_sphinx:start"
end
回答by Trung LE
Whenever doesn't detect your environment, it just defaults to using production. You can set the environment for all jobs using set:
只要没有检测到您的环境,它就会默认使用生产。您可以使用 set 为所有作业设置环境:
set :environment, 'staging'
Or per job:
或每个工作:
every 2.hours do
runner 'My.runner', :environment => 'staging'
end
回答by Simone Carletti
Don't write the RAILS_ENV variable. It should set it automatically.
不要写 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
It works in my app:
它适用于我的应用程序:
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"
回答by yogendra689
For Whenever (0.9.2)
为了 Whenever (0.9.2)
Use the @environmentvariable for environment check:
使用@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
回答by Tim Lowrimore
Something else you may want to try if you're using bundler and capistrano.
如果您使用的是 bundler 和 capistrano,您可能还想尝试其他一些东西。
In your deploy.rb file, when you set the :whenever_command, DO NOTsimply do this:
在您的 deploy.rb 文件中,当您设置 :whenever_command 时,不要简单地执行以下操作:
set :whenever_command, "bundle exec whenever"
Instead, do this:
相反,请执行以下操作:
set(:whenever_command) { "RAILS_ENV=#{rails_env} bundle exec whenever" }
Now, the RAILS_ENV environment variable will be available when the schedule.rb file is loaded, so in schedule.rb you can now do this:
现在,在加载 schedule.rb 文件时,RAILS_ENV 环境变量将可用,因此您现在可以在 schedule.rb 中执行以下操作:
set :environment, ENV['RAILS_ENV']
Voila! You're ready to go.
瞧!你准备好了。
回答by kaczor1984
Watch out if you want to pass more than one param to whenever.
You have to do it like that:
如果您想在任何时候传递多个参数,请注意。
你必须这样做:
whenever --update-crontab appname --set 'environment=production&cron_log=/path/to/log'
回答by Jon R.
This questions has been open a long time so I thought I would share what worked with whenever 0.9.7, Ruby 2.4.0, and RAILS 5.0.1. In the previously mentioned answer there are a lot of close tries but syntax error plagues them. Below is what worked and is very simple approach.
这个问题已经开放了很长时间,所以我想我会在 0.9.7、Ruby 2.4.0 和 RAILS 5.0.1 时分享哪些有用的东西。在前面提到的答案中有很多尝试,但语法错误困扰着他们。以下是有效且非常简单的方法。
schedule.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
Update the crontab(dev)
更新 crontab(dev)
whenever --set 'environment=development' --update-crontab
Results(dev)
结果(开发)
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'
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'
Update the crontab(prod)
更新 crontab(prod)
whenever --set 'environment=production' --update-crontab
Results(prod)
结果(产品)
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'
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'
Hopefully this can help someone out. Happy Coding this!
希望这可以帮助某人。快乐编码这个!
回答by Laurynas
Latest whenever allows easy Capistrano integration. You can add following to deploy.rb:
最新的任何时候都允许轻松的 Capistrano 集成。您可以在 deploy.rb 中添加以下内容:
set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}-#{stage}" }
require "whenever/capistrano"
回答by Satishakumar Awati
Add the following line of code at top of config/schedule.rb.
在 config/schedule.rb 的顶部添加以下代码行。
ENV['RAILS_ENV'] = "#{@pre_set_variables[:environment]}"
and update the crontab using following command.
并使用以下命令更新 crontab。
whenever --update-crontab pvcnxt --set 'environment=production'
and then finally restart crontab using command
然后最后使用命令重新启动 crontab
service crond restart
Thats it!
就是这样!
Final config/schedule.rblooks this way
最终的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
回答by Snips
I was having an issue where environment wasn't being set up for whenever cron jobs - /usr/bin/bundle was being picked up instead of /usr/local/bin/bundle.
我遇到了一个问题,即每当 cron 作业时都没有设置环境 - /usr/bin/bundle 而不是 /usr/local/bin/bundle。
The solution was to add following to top of schedule.rb
解决方案是在 schedule.rb 的顶部添加以下内容
env 'PATH', ENV['PATH']

