简体   繁体   English

我的Rails代码如何分辨它是运行在服务器(例如瘦服务器)还是rspec中?

[英]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.

相关问题 Rails + Rspec:如何测试环境特定的操作? 例如“......除非Rails.env.production?” - Rails + Rspec: How to test for environment specific actions? e.g. “… unless Rails.env.production?” 如何将多行代码复制粘贴到 rails 控制台(例如从脚本复制粘贴) - How to copy paste multiple lines of code into rails console (e.g. copy paste from a script) 如何使用Rails中的minitest测试我的控制器中的(例如)@project.save的失败? - How do I test for failure of (e.g.) @project.save in my controller using minitest in Rails? 如果用户未使用我的Web(导轨)应用程序(例如使用API​​),我该如何对他们进行身份验证 - How do I authenticate a user if they are not using my web (rails) application, e.g. using an API 如何获取我的Rails应用程序的基本URL(例如http:// localhost:3000)? - How do I get the base URL (e.g. http://localhost:3000) of my Rails app? Rails:我的应用程序如何判断它是在MRI还是JRuby中运行? - Rails: How can my app tell if it is running in MRI or JRuby? rspec中是否有一个“ not”等效项,例如逻辑上“ and_return”不是 - Is there a “not” equivalent in rspec, for e.g. the logical not for “and_return” RSpec:交换方法? 例如,将一种方法与另一种方法存根 - RSpec: Swap methods? e.g. stub one method with another Ruby on Rails问题-如何从请求首次命中应用程序时开始衡量响应时间? (例如,使用“基准”) - Ruby on Rails question - how can I measure response time from when request first hits app?? (e.g. using 'benchmark') Rails应用程序具有一组模型,但是有两个服务器进程(例如,用户和管理部分是分开的) - Rails app with one set of models, but two server processes (e.g. users and admin sections are separate)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM