简体   繁体   中英

Rails + Faye + Apache: Moving Faye from Thin to Passenger

Deploying with our main app on Passenger and Faye on Thin is currently working. But I'm having some issues transitioning from that set-up to Faye using passenger as well.

It suggests here ( https://github.com/faye/faye-websocket-ruby ) that I can run Faye on Passenger Standalone and start said server with this command

passenger start -p 9292

However, this doesn't even work locally. First it returns this error stating it cannot find faye.js at

http://localhost:9292/faye?message=%5B%7B%22channel%22%3A%22%2Fmeta%2Fhandshake%22%2C%22version%22%3A%221.0%22%2C%22supportedConnectionTypes%22%3A%5B%22callback-polling%22%5D%2C%22id%22%3A%221%22%7D%5D&jsonp=__jsonp1__

The error is this

Failed to load resource: the server responded with a status of 404 (Not Found) 

When you change the browser to the indicated location it says

no route matches [GET] "/faye"

Looking at the passenger logs it seems to first run into this error

Started GET "/faye" for 127.0.0.1 at 2014-05-09 10:04:23 -0700

ActionController::RoutingError (No route matches [GET] "/faye"):

and then run into this one

Started OPTIONS "/faye" for 127.0.0.1 at 2014-05-09 10:04:58 -0700

ActionController::RoutingError (No route matches [OPTIONS] "/faye"):

While running using Thin I used the following command to start the server

bundle exec rackup faye.ru -s thin --daemonize -E production

There are a few differences between the passenger start I'm trying and the thin, but the largest one is that faye.ru never gets run. My faye.ru is basic

require 'faye'
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
Faye::WebSocket.load_adapter('thin')
run faye_server

To switch that over to Passenger I know I need to remove the third line...but then what? When I just added this straight into my config.ru file the faye server ended up taking over the entire app, which definitely was not my aim. I thought perhaps putting it in an initializer would work, but I run into the following error:

undefined method `run' for main:Object (NoMethodError)

Which I assume is because this isn't a .ru file but a .rb file.

Regardless, I find myself rather confused, and I'd definitely appreciate any tips/directions to go.

Edit:

This site ( http://rubydoc.org/github/jamesotron/faye-rails/frames ) states

If you want to run faye-rails on passenger, make sure you are using passenger 4.0 standalone or passenger 4.0 on nginx 1.4+ for nginx with websocket support. Passenger on apache is not supported. Because passenger uses a multi-process model, you must use the faye redis backend. Add gem 'faye-redis' to your Gemfile and configure your routes like this:

config.middleware.use FayeRails::Middleware, mount: '/faye', :timeout => 25, server: 'passenger', engine: {type: Faye::Redis, host: 'localhost'}

However I've tried adding the relevant code to my application.rb, where I have several config commands going on within

module App
  class Application < Rails::Application

but simply adding below that (where all my other config.whatever are) the code suggested above leads to this error

uninitialized constant App::Application::FayeRails (NameError)

Edit:

Added the faye-rails gem because I'm an idiot (see comments). This also required adding

config.middleware.delete Rack::Lock

because (console output)

faye-rails can't work when Rack::Lock is enabled, as it will cause
a deadlock on every request.

However, now I'm running into this error

/Users/WEF6/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require': cannot load such file -- passenger (LoadError)

This must be related to some of the changes I made as outlined in this post as I outlined above how I was receiving errors after a successful passenger start-up locally. Running passenger now the server throws the following error

Could not spawn process for group location#default: An error occured while starting up the preloader.
     in 'void Passenger::ApplicationPool2::SmartSpawner::handleErrorResponse(Passenger::ApplicationPool2::SmartSpawner::StartupDetails &)' (SmartSpawner.h:455)
     in 'string Passenger::ApplicationPool2::SmartSpawner::negotiatePreloaderStartup(Passenger::ApplicationPool2::SmartSpawner::StartupDetails &)' (SmartSpawner.h:566)
     in 'void Passenger::ApplicationPool2::SmartSpawner::startPreloader()' (SmartSpawner.h:206)
     in 'virtual ProcessPtr Passenger::ApplicationPool2::SmartSpawner::spawn(const Passenger::ApplicationPool2::Options &)' (SmartSpawner.h:752)
     in 'void Passenger::ApplicationPool2::Group::spawnThreadRealMain(const SpawnerPtr &, const Passenger::ApplicationPool2::Options &, unsigned int)' (Implementation.cpp:804)

[ 2014-05-09 12:15:15.1055 71107/0x10c9ce000 agents/HelperAgent/RequestHandler.h:2222 ]: [Client 21] Cannot checkout session.
Error page:
cannot load such file -- passenger (LoadError)

You must declare the faye-rails gem in your gemfile including this line:

'faye-rails', '~> 2.0.0'

Update:

I think you can solve the rack lock issue adding the line below in your application.rb

config.middleware.delete Rack::Lock

I hope this helps you.

I noticed you're using faye.ru when running Thin. Phusion Passenger only supports config.ru as the filename for the Rack startup file. Maybe the solution is as easy as renaming faye.ru to config.ru?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM