如何随时检测内部Rails环境
这个问题可能只有在您了解用于创建的 whenever gem 时才有意义cron 作业。
对于我的应用,我希望在所有环境中whenever
使用,包括测试和开发。
我的 schedule.rb
看起来像这样:
set :output, {
:error => "#{path}/log/error.log",
:standard => "#{path}/log/cron.log"
}
set :environment, Rails.env.to_sym
every 5.minutes do
rake 'db:activity:synchronize'
end
但它在 Rails.env.to_sym
上失败(同样代表 RAILS_ENV):
/home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `eval': uninitialized constant Whenever::JobList::Rails (NameError)
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `eval'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `initialize'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever.rb:15:in `new'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever.rb:15:in `cron'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/command_line.rb:41:in `run'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/command_line.rb:8:in `execute'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/bin/whenever:38:in `<top (required)>'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/bin/whenever:19:in `load'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/bin/whenever:19:in `<main>'
所以,我的问题基本上可以归结为:
- 我如何访问当前环境,或者
- 我应该怎么做才能在所有环境中使用
whenever
?
This question will probably only make sense if you know about the whenever gem for creating cron jobs.
For my app, I want to use whenever
in all the environments, including testing and development.
My schedule.rb
looks like this:
set :output, {
:error => "#{path}/log/error.log",
:standard => "#{path}/log/cron.log"
}
set :environment, Rails.env.to_sym
every 5.minutes do
rake 'db:activity:synchronize'
end
but it fails on Rails.env.to_sym
(and the same stands for RAILS_ENV
):
/home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `eval': uninitialized constant Whenever::JobList::Rails (NameError)
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `eval'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/job_list.rb:21:in `initialize'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever.rb:15:in `new'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever.rb:15:in `cron'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/command_line.rb:41:in `run'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/lib/whenever/command_line.rb:8:in `execute'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/gems/whenever-0.6.8/bin/whenever:38:in `<top (required)>'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/bin/whenever:19:in `load'
from /home/marius/.rvm/gems/ruby-1.9.2-p290@uxolo/bin/whenever:19:in `<main>'
So, my question basically boils down to:
- How do I access the current environment, or
- What should I do to use
whenever
in all the environments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
至少在新版本中,只要可以使用
@environment
访问环境即可。例如,如果您希望每次只为生产中的某些作业生成 cron 条目:At least in newer version of whenever it is possible to access the environment with
@environment
. For example if you want whenever to only generate cron entries for some jobs in production:错误消息表明 Rails 未定义。即,当您询问 Rails 运行的环境是什么时,框架未加载。
事实上,通过查看“每当”的代码,看起来 Rails 并不是它的必需条件(即,即使系统上没有安装 Rails,您也可以安装并运行“每当”)。因此,无论何时,都无法查看您的 Rails 环境(据我所知)
The error message suggests that Rails isn't defined. i.e the framework isn't loaded when you're asking the question what environment is rails running with.
In fact from looking at the code for Whenever it looks like rails isn't a requirement for it (i.e. You can install and run Whenever without rails even being installed on your system). Hence there's no way for Whenever to look at your rails environment (as far as i can tell)
根据gem 作者的推荐,解决方案是将当前环境作为变量传递:
Chris Bailey 是对的:
Whenever
本身不会加载轨道环境。As recommended by the gem author, the solution is to pass in the current environment as a variable:
And Chris Bailey is right:
Whenever
itself doesn't load the Rails environment.类似问题的第一个答案的变体为我工作。添加
到
schedule.rb
的顶部,您将能够调用 Rails.env 来访问当前的 Rails 环境。注意:如果您的
environment.rb
文件不在/app/config
中,上述路径将会有所不同A variation of the first answer to a similar question worked for me. Add
to the top of
schedule.rb
and you'll be able to call Rails.env to access the current Rails environment.Note: the above path would be different if your
environment.rb
file isn't in/app/config
我实现了
Rails.env
我发现 此处(通过单击“源”),并使用它来初始化config/schedule.rb
开头的::Rails
模块,这将创建Rails 模块,并使其环境返回您在
whenever
命令行中作为--setenvironment=...
提供的内容,正如脚本作者所建议的那样。但是,无论何时将
@environment
默认设置为生产环境,因此这个大的“或”可能不太有用。现在,Whenever 脚本中的
Rails.env
调用就可以工作了。对我来说更重要的是,它也适用于我包含在schedule.rb
中的其他脚本,例如加载application.yml
的脚本。PS
eval
调用用于从模块定义内部访问schedule.rb
脚本范围内可用的@environment
。I took the implementation of
Rails.env
I found here (by clicking on "source"), and used it to initialize the::Rails
module at the beginning of theconfig/schedule.rb
This creates the
Rails
module, and makes its environment return what you supplied as--set environment=...
in thewhenever
command line, as the script author suggests.However, whenever sets the
@environment
to production by default, so this large "or" may be not quite useful.Now the
Rails.env
call in the Whenever script would work. What was more important in my case, it also worked in other scripts I included intoschedule.rb
, such as the one that loadedapplication.yml
.P.S. The
eval
call is used to access@environment
available in the scope of theschedule.rb
script from inside the definition of a module.