[英]How can my Rails code tell whether it is running in a server (e.g. thin) or in rspec?
It turns out that the thin server is very particular about starting the EventMachine reactor for itself. 事实证明,瘦服务器对于自己启动EventMachine反应器非常特别。 That would be fine except that there are circumstances where I need to start the reactor because the process is not being run inside of thin.
可以,除非在某些情况下我需要启动反应堆,因为该过程不是在瘦身内部运行的。
So the simple question is how can I programatically determine that my Rails application is being started by a thin server? 因此,简单的问题是如何以编程方式确定瘦服务器正在启动我的Rails应用程序? In that case I won't start my own EventMachine reactor, otherwise I have to.
在那种情况下,我将不会启动自己的EventMachine反应堆,否则我将不得不启动。
After walking through both Rails and Thin initialization, it appears that there really is no penetration into the Application
instance from Thin::Server
. 在经历了Rails和Thin初始化之后,似乎确实没有从
Thin::Server
渗透到Application
实例中。 That seems to mean that I can't examine my app in order to see the server running it. 这似乎意味着我无法检查我的应用程序才能看到服务器正在运行它。
So I simply opted to check whether Thin::Server
was defined. 因此,我只是选择检查是否定义了
Thin::Server
。 Since I have thin loaded with: 由于我瘦了:
gem 'thin', require: false
I'm only going to have the Thin::Server
class initialized if the application is being started from thin. 如果应用程序是从瘦启动的,我只会初始化
Thin::Server
类。 I've checked the rails console, rake tasks and delayed_jobs workers and the assumption seems to hold. 我检查了Rails控制台,rake任务和delay_jobs工人,并且这个假设似乎成立了。
So, in my application.rb: 因此,在我的application.rb中:
# Start Faye...
config.middleware.delete Rack::Lock
thin_server = defined?(Thin::Server)
config.after_initialize do |app|
Faye.logger = Rails.logger
unless thin_server
Faye.logger.debug "Ensure reactor running!"
Faye.ensure_reactor_running!
end
end
faye_params = {mount: '/faye', timeout: 25}
faye_params[:server] = 'thin' if thin_server
config.middleware.use FayeRails::Middleware, faye_params
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.