如何随时检测内部Rails环境

发布于 2024-11-30 01:52:30 字数 1845 浏览 1 评论 0原文

这个问题可能只有在您了解用于创建的 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>'

所以,我的问题基本上可以归结为:

  1. 我如何访问当前环境,或者
  2. 我应该怎么做才能在所有环境中使用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:

  1. How do I access the current environment, or
  2. What should I do to use whenever in all the environments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

檐上三寸雪 2024-12-07 01:52:30

至少在新版本中,只要可以使用 @environment 访问环境即可。例如,如果您希望每次只为生产中的某些作业生成 cron 条目:

case @environment
when 'production'
  every 1.day, :at => '0:00 am' do
    rake "some:task"
  end 
end

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:

case @environment
when 'production'
  every 1.day, :at => '0:00 am' do
    rake "some:task"
  end 
end
吝吻 2024-12-07 01:52:30

错误消息表明 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)

烟雨扶苏 2024-12-07 01:52:30

根据gem 作者的推荐,解决方案是将当前环境作为变量传递

$ whenever --set environment=test
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/marius/uxolo && RAILS_ENV=test rake db:activity:synchronize --silent >> /home/marius/uxolo/log/cron.log 2>> /home/marius/uxolo/log/error.log'

$ whenever --set environment=development
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/marius/uxolo && RAILS_ENV=development rake db:activity:synchronize --silent >> /home/marius/uxolo/log/cron.log 2>> /home/marius/uxolo/log/error.log'

Chris Bailey 是对的:Whenever 本身不会加载轨道环境。

As recommended by the gem author, the solution is to pass in the current environment as a variable:

$ whenever --set environment=test
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/marius/uxolo && RAILS_ENV=test rake db:activity:synchronize --silent >> /home/marius/uxolo/log/cron.log 2>> /home/marius/uxolo/log/error.log'

$ whenever --set environment=development
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /home/marius/uxolo && RAILS_ENV=development rake db:activity:synchronize --silent >> /home/marius/uxolo/log/cron.log 2>> /home/marius/uxolo/log/error.log'

And Chris Bailey is right: Whenever itself doesn't load the Rails environment.

樱花落人离去 2024-12-07 01:52:30

类似问题的第一个答案的变体为我工作。添加

require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

schedule.rb 的顶部,您将能够调用 Rails.env 来访问当前的 Rails 环境。

注意:如果您的 environment.rb 文件不在 /app/config 中,上述路径将会有所不同

A variation of the first answer to a similar question worked for me. Add

require File.expand_path(File.dirname(__FILE__) + "/../config/environment")

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

辞别 2024-12-07 01:52:30

我实现了 Rails.env 我发现 此处(通过单击“源”),并使用它来初始化 config/schedule.rb 开头的 ::Rails 模块,

eval %Q(module ::Rails
  def self.env
    '#{@environment}' || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
  end
end
)

这将创建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 the config/schedule.rb

eval %Q(module ::Rails
  def self.env
    '#{@environment}' || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
  end
end
)

This creates the Rails module, and makes its environment return what you supplied as --set environment=... in the whenever 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 into schedule.rb, such as the one that loaded application.yml.

P.S. The eval call is used to access @environment available in the scope of the schedule.rb script from inside the definition of a module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文