有没有办法在Rake中获取有关当前Rack环境的信息?例如,如何判断Rack是在开发模式还是生产模式下运行?
解决方法
问题很老但从来没有取得最佳实践答案或令人满意的答案.
真正的问题是:如何确保在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"]