如果我正在运行rake命令,如何在rails中进行检测? [英] How do I detect in rails if I am running a rake command?

查看:44
本文介绍了如果我正在运行rake命令,如何在rails中进行检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails中,您可能希望您的环境在启动时执行不同的操作,具体取决于您是否正在运行rake任务.例如,我的用例是在应用程序启动时将数百MB的缓存加载到内存中.我们显然不希望在rake命令上发生这种情况.

In Rails, you might want your environment to do different things on startup depending on if you are running a rake task or not. For instance, my use case was having several hundred MB of cache loaded into memory on app start. We obviously don't want this to happen on rake commands.

-更新-以下是可靠的解决方案,可与heroku一起使用.

---update--- The following is reliable solution and works with heroku.

is_rake = (ENV['RACK_ENV'].blank? || ENV['RAILS_ENV'].blank? || !("#{ENV.inspect}" =~ /worker/i).blank?)

推荐答案

如果您使用的是heroku并且使用的是工作程序,则这是执行此检查的一种更可靠的方法.

If you are using heroku AND you are using workers, here is a more reliable way to do this check.

is_rake = (ENV['RACK_ENV'].blank? || ENV['RAILS_ENV'].blank? || !("#{ENV.inspect}" =~ /worker/i).blank?)

这篇关于如果我正在运行rake命令,如何在rails中进行检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆