我在Heroku上部署了运行Puma的Rails应用程序。我正在使用gem websockets-rails
进行实时聊天和通知。
我最初在连接到websockets(RuntimeError (eventmachine not initialized: evma_install_oneshot_timer):
)时遇到了一些问题,但我通过添加
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
到config/initializers/eventmachine.rb
。同样,当我运行heroku local
并且几分钟后没有超时/变为eventmachine not initialized
时,一切正常。
但是,每次在Heroku上部署/重新部署时,WebSockets在前几分钟都能正常工作,但之后就失败了。我在客户端和堆栈跟踪中出现WebSocket connection to 'wss://www.studbuds.com/websocket' failed: Error during WebSocket handshake: Unexpected response code: 500
错误
2016-01-20T17:59:12.672733+00:00 app[web.1]: RuntimeError (eventmachine not initialized: evma_install_oneshot_timer):
2016-01-20T17:59:12.672734+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/eventmachine-1.0.8/lib/eventmachine.rb:331:in `add_oneshot_timer'
2016-01-20T17:59:12.672735+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/eventmachine-1.0.8/lib/eventmachine.rb:331:in `add_timer'
2016-01-20T17:59:12.672735+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/eventmachine-1.0.8/lib/em/timers.rb:50:in `schedule'
2016-01-20T17:59:12.672736+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/eventmachine-1.0.8/lib/em/timers.rb:37:in `initialize'
2016-01-20T17:59:12.672736+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_adapters.rb:167:in `new'
2016-01-20T17:59:12.672737+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_adapters.rb:167:in `start_ping_timer'
2016-01-20T17:59:12.672738+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_adapters.rb:46:in `initialize'
2016-01-20T17:59:12.672739+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_adapters/web_socket.rb:10:in `initialize'
2016-01-20T17:59:12.672739+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_adapters.rb:14:in `new'
2016-01-20T17:59:12.672740+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_adapters.rb:14:in `establish_connection'
2016-01-20T17:59:12.672741+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_manager.rb:78:in `open_connection'
2016-01-20T17:59:12.672741+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/websocket-rails-0.7.0/lib/websocket_rails/connection_manager.rb:55:in `call'
2016-01-20T17:59:12.672742+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/mapper.rb:51:in `serve'
2016-01-20T17:59:12.672742+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/journey/router.rb:43:in `block in serve'
2016-01-20T17:59:12.672743+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/journey/router.rb:30:in `each'
2016-01-20T17:59:12.672743+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/journey/router.rb:30:in `serve'
2016-01-20T17:59:12.672744+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:819:in `call'
2016-01-20T17:59:12.672744+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/warden-1.2.4/lib/warden/manager.rb:35:in `block in call'
2016-01-20T17:59:12.672745+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/warden-1.2.4/lib/warden/manager.rb:34:in `catch'
2016-01-20T17:59:12.672745+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/warden-1.2.4/lib/warden/manager.rb:34:in `call'
2016-01-20T17:59:12.672746+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/etag.rb:24:in `call'
2016-01-20T17:59:12.672747+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/conditionalget.rb:25:in `call'
2016-01-20T17:59:12.672747+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
2016-01-20T17:59:12.672748+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
2016-01-20T17:59:12.672748+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/flash.rb:260:in `call'
2016-01-20T17:59:12.672748+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225:in `context'
2016-01-20T17:59:12.672749+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in `call'
2016-01-20T17:59:12.672749+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/cookies.rb:560:in `call'
2016-01-20T17:59:12.672749+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/query_cache.rb:36:in `call'
2016-01-20T17:59:12.672750+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
2016-01-20T17:59:12.672751+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
2016-01-20T17:59:12.672751+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:88:in `call'
2016-01-20T17:59:12.672751+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:88:in `_run_callbacks'
2016-01-20T17:59:12.672752+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
2016-01-20T17:59:12.672752+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
2016-01-20T17:59:12.672752+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
2016-01-20T17:59:12.672753+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
2016-01-20T17:59:12.672753+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
2016-01-20T17:59:12.672753+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2016-01-20T17:59:12.672757+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:38:in `call_app'
2016-01-20T17:59:12.672757+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `block in call'
2016-01-20T17:59:12.672758+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2016-01-20T17:59:12.672758+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:26:in `tagged'
2016-01-20T17:59:12.672758+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `tagged'
2016-01-20T17:59:12.672759+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `call'
2016-01-20T17:59:12.672759+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/request_store-1.3.0/lib/request_store/middleware.rb:9:in `call'
2016-01-20T17:59:12.672760+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2016-01-20T17:59:12.672760+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2016-01-20T17:59:12.672761+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2016-01-20T17:59:12.672761+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2016-01-20T17:59:12.672762+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/static.rb:113:in `call'
2016-01-20T17:59:12.672762+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2016-01-20T17:59:12.672762+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/ssl.rb:24:in `call'
2016-01-20T17:59:12.672763+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:518:in `call'
2016-01-20T17:59:12.672763+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application.rb:164:in `call'
2016-01-20T17:59:12.672764+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
2016-01-20T17:59:12.672765+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.15.3/lib/puma/server.rb:541:in `handle_request'
2016-01-20T17:59:12.672765+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.15.3/lib/puma/server.rb:388:in `process_client'
2016-01-20T17:59:12.672766+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.15.3/lib/puma/server.rb:270:in `block in run'
2016-01-20T17:59:12.672766+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.15.3/lib/puma/thread_pool.rb:106:in `call'
2016-01-20T17:59:12.672766+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.15.3/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
如果我重新启动dynos,它会再次起作用但不久就会死掉。
注意我使用的是多线程的Puma,那么我的应用程序是不是线程安全的呢?