检测应用程序是否作为 HTTP 服务器启动(rake 任务、rconsole 等) [英] Detect if application was started as HTTP server or not (rake task, rconsole etc)

查看:24
本文介绍了检测应用程序是否作为 HTTP 服务器启动(rake 任务、rconsole 等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 EventMachineMomentally 与我的 rails 应用程序一起启动 e TCP 服务器.这是从 config/initializers/momentively.rb 开始的.

I'm using EventMachine and Monetarily to start e TCP server along with my rails application. This is started from config/initializers/momentarily.rb.

我的问题是当我运行 rake 任务时它也会启动,比如 db:migrate.我只希望它在我启动 HTTP 服务器时启动.环境无济于事,因为服务器启动和 rake 任务都在开发环境下.有没有办法知道应用程序正在运行 HTTP 服务器而不是其他任何东西?请注意,这不仅是 rake 任务,如果我运行 Rails 控制台,EM 也会启动,这对我来说又是不可取的.

My problem is that it starts also when I run rake tasks, like db:migrate. I only want it to start when when I start the HTTP server. Environments won't help, since both the server start and rake tasks are under Development environment. Is there a way of knowing that the application is running the HTTP server as opposed to anything else? Note that is not only rake tasks, the EM starts also if I run the rails console, which is again something not desirable for my case.

推荐答案

据我所知,没有什么好的方法可以做到这一点.您可以复制 newrelic 的方法(检查 discover_dispatcherlocal_environment.rb) 基本上有一个启发式列表,用于检测它是否跑在里面的乘客,瘦瘦的等

There's not a great way of doing this that I know of. You could copy newrelic's approach (check discover_dispatcher in local_environment.rb) which basically has a list of heuristics used to detect if it is running inside passenger, thin, etc.

为乘客检查

defined?(::PhusionPassenger)

细细检查

if defined?(::Thin) && defined?(::Thin::Server)

这篇关于检测应用程序是否作为 HTTP 服务器启动(rake 任务、rconsole 等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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