微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby – 如何在Rake中获得当前的Rack环境?

有没有办法在Rake中获取有关当前Rack环境的信息?例如,如何判断Rack是在开发模式还是生产模式下运行?

我知道Rake不支持Rack.我试图避免在生产和开发环境之间的几乎相同的Rake任务中复制代码.

解决方法

问题很老但从来没有取得最佳实践答案或令人满意的答案.

真正的问题是:如何确保在Rake任务中使用哪个环境以便加载正确的配置/命中正确的if条件.

Note: As Rake doesn’t give much about Rack (Rake is not using HTTP) to
rely on the RACK_ENV is basically wrong but common and handy if a Rake
task loads your main Sinatra application (the RACK_ENV is required to let
Sinatras development? / test? / production? being set correctly).

答案:使用每个Rake任务调用设置环境.

命令行调用

/usr/bin/rake namespace:task_name RACK_ENV=production

Cronjob调用(在crontab中):

cd /into/your/app/root && /usr/bin/rake namespace:task_name RACK_ENV=production --silent

Note: To specify the path of the Rake bin is not necessary if you have it in your global system variables. Your path might differs from mine used in the examples,check on Unix systems with: whereis rake

您可以通过以下方式检查任务中的RACK_ENV:

puts ENV["RACK_ENV"]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐