views:

3548

answers:

6

To jump on the band-wagon of Phusion Passenger we've setup a staging server for a small rails app to test things out.

So far it has been very nice to use, it makes installing/configuring and deploying apps a breeze. The problem is the site we're using doesn't get hit very often and it seems to shut down the servers in the background. Meaning when someone goes to the site they have a really long wait until it starts up a new server to handle the request. We've read through the documentation, tried quite a few different set-ups (smart/smart-lv2 modes, passengeridletime etc) and still haven't found a real solution.

After ploughing through Google results we can't really find useful information. Currently we have a cron job that makes a request every-so-often in an attempt to keep the servers running.

Is anyone else experiencing this problem and do you have any advice for a fix?

+20  A: 

What's happening is that your Application and/or ApplicationSpawners are shutting down due to time-out. To process your new request, Passenger has to startup a new copy of your application, which can take several seconds, even on a fast machine. To fix the issue, there are a few Apache configuration options you can use to keep your Application alive.

Here's specifically what I've done on my servers. The RailsSpawnMethod and RailsAppSpawnerIdleTime are the configuration options most important in your situation.

# Speeds up spawn time tremendously -- if your app is compatible. 
# RMagick seems to be incompatible with smart spawning
RailsSpawnMethod smart

# Keep the application instances alive longer. Default is 300 (seconds)
PassengerPoolIdleTime 1000

# Keep the spawners alive, which speeds up spawning a new Application
# listener after a period of inactivity at the expense of memory.
RailsAppSpawnerIdleTime 0

# Additionally keep a copy of the Rails framework in memory. If you're 
# using multiple apps on the same version of Rails, this will speed up
# the creation of new RailsAppSpawners. This isn't necessary if you're
# only running one or 2 applications, or if your applications use
# different versions of Rails.
RailsFrameworkSpawnerIdleTime 0

# Just in case you're leaking memory, restart a listener 
# after processing 5000 requests
PassengerMaxRequests 5000

# only check for restart.txt et al up to once every 5 seconds, 
# instead of once per processed request
PassengerStatThrottleRate 5

By using "smart" spawning mode and turning off RailsAppSpawnerIdleTime, Passenger will keep 1 copy of your application in memory at all times (after the first request after starting Apache). Individual Application listeners will be forked from this copy, which is a super-cheap operation. It happens so quickly you can't tell whether or not your application has had to spawn a listener.

If your app is incompatible with smart spawning, I'd recommend keeping a large PassengerPoolIdleTime and hitting your site periodically using curl and a cronjob or monit or something to ensure the listener stays alive.

The Passenger User Guide is an awesome reference for these and more configuration options.

Finally, but unrelated to your question, consider using Ruby Enterprise Edition if you go with Smart spawning. It can save you a lot of memory.

John Douthat
Thank you very much for your answer. I believe we have tried most of those setting but maybe not in the correct combination. I will get testing tomorrow and revert.
tsdbrown
This is awesome. I was having the same problem with my Nginx/Phusion Passenger install and this helped me tremendously.
Scott Anderson
A: 

If your host is a shared server, like mine, you can't change the settings and are stuck with a cron job.

For this particular application thankfully it's not. But I'll bare that in mind for the future thanks.
tsdbrown
+1  A: 

RE:

# Additionally keep a copy of the Rails framework in memory. If you're 
# using multiple apps on the same version of Rails, this will speed up
# the creation of new RailsAppSpawners. This isn't necessary if you're
# only running one or 2 applications, or if your applications use
# different versions of Rails.
RailsFrameworkSpawnerIdleTime 0

Just something to add and might be useful.

The default spawn method in the current release is "smart-lv2", which skips the framework spawner, so setting the framework spawner timeout wouldn't have effect anyway unless you explicitly set the spawn method to "smart".

Source: http://groups.google.com/group/phusion-passenger/browse_thread/thread/c21b8d17cdb073fd?pli=1

Shuoling Liu
A: 
Putting this in <VirtualHost ...> sections in Apache works just fine for me.
Gav
+5  A: 

Just incase there are any nginx server users stumbling upon this question, both the 'PassengerMaxRequests' and 'PassengerStatThrottleRate' directives don't translate to nginx. However the others do:

rails_spawn_method smart;
rails_app_spawner_idle_time 0;
rails_framework_spawner_idle_time 0;
passenger_pool_idle_time 1000;

HTH!

Gav
Thanks for this. One thing to note is that I had to stuff the passenger_pool_idle_time in my main nginx.conf with the other global settings instead of just in the specific site config where rails was enabled.
Scott Anderson
A: 

Where does John Douthat's configuration parameters go?

apache2.conf

or

passenger.conf

Coderama
apache apache apahce (sorry, 15 chars min for comments)
Espen