Posts filed under 'ruby on rails'
I encountered this problem when I first started working with Passenger, and have run into it repeatedly over the last year, so I thought I’d just throw out the fix I have for it.
Usually people having these problems are usually running the prefork MPM and have a section in their apache config file that looks like this
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
#Aside: To find out if what multi processing module (mpm) you are runing (worker or prefork) just run this command.
apache2 -l
(it may also be httpd/httpd2/apache/apache2ctl/apachectl -l)
The apache processes themselves don’t usually take up a lot of memory, I’ve seen anywhere from 0.2MB – 14MB … but they usually lie in the range of 2 – 6MB.
The problem comes if you get a traffic spike and Apache spawns up to 150 process to handle the traffic.
You can see that you can easily have 300MB – 1GB of memory allocated to the processes and, consequently, run out of memory so that your server is now unresponsive.
The fix for this is to set MaxClients to, at minimum, your StartServers + Min Spare Servers. But you also want to take into consideration the Max Instances of your app that you’ve set for passenger and how many requests you want to be able to serve at a time.
From Apache documentation
The MaxClients directive sets the limit on the number of simultaneous requests that will be served. Any connection attempts over the MaxClients limit will normally be queued, up to a number based on the ListenBacklog directive. Once a child process is freed at the end of a different request, the connection will then be serviced.
Be sure to increase this number if you are running php apps on your server as well.
Hope this helps.
August 25th, 2009
I was working on a rails app today, and got to wondering about the very many
SHOW FIELDS FROM
MySql queries I was seeing.
I had always thought about whether there was a way to get rid of those completely, or reducing the number of them by caching or something.
Well, today I actually did some digging and found this little gold nugget about “SHOW FIELDS” calls in Rails.
Apparently, Rails does cache MySql “SHOW FIELDS ” with one notable exception.
However, upon inspection it turns out that Rails DOES NOT cache ‘SHOW FIELDS’ queries for has_and_belongs_to_many associations. So every time a select or an insert is done via a Rails has_and_belongs_to_many association, a ‘SHOW FIELDS’ on the join table is executed.
So the dreaded HABTM association strikes again!
Those darned things are pure evil eh?
Well the awesome blog writer figured out a patch to fix the problem.
And in his tests he reported this (see image below for reference) …
After rolling this change out we have seen a very noticeable improvement on performance. By using New Relic’s Compare With Yesterday feature (see chart below) we can see that our application response time dropped from an average of about 560 ms per request to 420 ms per request. This is roughly a 25% performance increase. The yellow line shows today, the blue line is yesterday. CPU and database load decreased by about 25% as well.
A 25% improvement in application response time!?!?!
Sign. me. up.

I followed the Rails ticket on this particular issue and couldn’t figure out whether the fix had been rolled into a Rails version yet. I asked the blog author and he says the patch has been included in Rails 2.3, so if you’re running that, this does not apply to you.
I’m about to apply this patch to a couple of applications, hopefully it helps you too.
Please post in the comments about your experience. Same? better? worse?
June 29th, 2009
So the rails way of requiring a gem in your app is by using the config.gem instruction in environment.rb (as opposed to sticking a require statement in your environment.rb).
However including gems from github is a different beast … sometimes the gem author will tell you exactly how to do it, or sometimes they won’t, as the case with flickr-fu.
Just fyi … typically when you are including a gem from git hub you will want to do something like this …
config.gem 'gem-name-from-'gem -list'-command', :lib => 'github_gem_name', :source => 'http://gems.github.com'
“gem name from gem -list command” … by this I mean that you should run a gem -list command from your command line and use the name that shows up for your gem there.
… so the will_paginate gem config.gem statement looks like this …
config.gem 'mislav-will_paginate', :lib => 'will_paginate', :source => 'http://gems.github.com'
but for the xml-magic gem, its this ….
config.gem 'xml-magic', :lib => 'xml_magic', :source => 'http://gems.github.com'
and for the flickr-fu gem, its this ….
config.gem 'flickr-fu', :lib => 'flickr_fu', :source => 'http://gems.github.com'
Its not documented anywhere.
I had to figure it out by trial-and error, now you don’t have to
May 22nd, 2009
This one is simple, but I couldn’t find a decent google result for it.
Before you uninstall the plugin, you have to get its name … go to the vendors directory in your app folder and get the name of the folder

In this case the name of the plugin is “active_scaffold”
Go to the directory of the app you want the plugin removed from and type in
ruby script/plugin remove active_scaffold
or if you’re on a Linux box
./script/plugin remove active scaffold
February 17th, 2009
Usually in Rails, if you specify a before_filter in the base controller ‘ApplicationController’ (in application.rb), every other controller in that app inherits that filter, so that even if you specify a before_filter in another controller … the filter in application.rb always runs
Example:
class ApplicationController < ActionController::Base
before_filter :check_login
end
class UploadsController < ApplicationController
before_filter :get_data
end
The :check_login method is always run even though UploadsController specifies another before_filter.
(You can stop this behavior by specifying a skip_before_filter :check_login in the Uploads Controller)
However if you take this mindset with you to cakephp you’re in for a frustrating time. Because if you do something like this like this …
class AppController extends Controller{
function beforeFilter(){
if($this->Session->read('authenticated') != 'true'):
$this->redirect('/login');
endif;
}
}
class UploadsController extends AppController{
function beforeFilter(){
$this->set('file_categories', $this->FileCategory->find('list', array('order' => 'name')));
}
}
… you’re going to be left wondering why your very hastily thrown together authentication scheme, doesn’t work on the Uploads controller :\
Because the beforeFilter specified in each controller completely *overrides* the one from its parent (in php Object oriented programming, methods in child classes override the matching method in the parent), what you have to do *in PHP* is call the beforeFilter of the parent controller, first, before doing what you want to do.
So instead of the above, you do this …(note the emphasis)
class UploadsController extends AppController{
function beforeFilter(){
parent::beforeFilter();
$this->set('file_categories', $this->FileCategory->find('list', array('order' => 'name')));
}
}
February 16th, 2009
New release announced
Global queuing
We recently announced that we’ve developed a feature called global queuing. This feature was requested by 37signals. When global queuing is on, Phusion Passenger will load balance all incoming requests into the first available backend process. This is especially useful if you have long-running requests, e.g. requests that perform heavy calculations and can take several seconds to finish.
Fixed compatibility with the latest Rake version and RubyGems version
When compiling Phusion Passenger using the latest Rake, compilation commands are not shown while various warnings are being shown. This has been fixed. Various RubyGems deprecation warnings have also been fixed.
Running background programs from within the Rails app won’t freeze the request
In previous versions of Phusion Passenger, if one executes
system(”sleep 10 &”)
from within the Rails app, then Phusion Passenger won’t finish the request until 10 seconds have gone by. In other words, Phusion Passenger would wait until the background program has finished. This issue is caused by file descriptor leaks, and has been fixed.
Fixed Mac OS X crash
A Mac OS X related crash has been fixed.
Various bug fixes
The title says it all. If you experienced any kind of problems with previous releases, please try this release as the bug may have been fixed.
December 2nd, 2008
If you’re here, you are probably getting an error like this during a gem install on a windows box.
cl -c -nologo -O1 -MD -Zi -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAV
E_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX -
O1 -MD -Zi -DNDEBUG -DVERSION=\”6.4\” -DXS_VERSION=\”6.4\” -IC:\Perl\lib\CO
RE BitVector.c
‘cl’ is not recognized as an internal or external command,
operable program or batch file.
NMAKE : U1077:
Stop.
The fix is easy if you have visual studio or the .NET SDK installed.
If you don’t then either go download Visual Studio Express or the .NET SDK (anyone out of 1.1, 2.0 or 3.5 should do)
Now, do a search on your local machine for ‘cl.exe’
I use the phenomenal windows desktop search engine locate32), here were the results I got.

Now all I had to do was use the nifty windows Path Editor “Path Ed” to add one of those paths to the system path variable

and that error goes away.
Only to be replaced by an even more cryptic one in my case
(I was trying to install passenger … I know I can’t, but I thought I’d try)

August 18th, 2008
Its old but I stumbled onto this just today. Zed Shaw (the creator of that ol’ faithful Ruby server mongrel) is a fan of the Apache Passenger (modrails) guys
I also met the Phusion Passenger guys and holy fucking crap are they on to something. If anyone is going to actually take on Mongrel in the hosting area it’s Passenger. The developers are super cool nice guys (unlike me) and even DHH likes their stuff. He really never liked anything I built, so hopefully those guys get more support. About the only thing keeping them from taking over is that they use forking so a few libraries that keep resources open will have serious problems. They’ll probably have to think up some kind of thing for that soon, but I think most Rails deployments could get pretty far with Passenger.
…
Honestly though, it shouldn’t be that hard to beat Mongrel since Mongrel is crippled by Ruby. What the Phusion guys are pulling off is just using Apache to do the heavy lifting and then let their module do the work to stream out to Rails. It’s not a new idea, they’re just doing a great job marketing it and educating people while keeping things simple. The kicker is that they also have support for Rack and WSGI. Now that’s fucking sexy.
Now, thats one hell of an endorsement
August 16th, 2008
I’ve been mucking around with passenger quite a bit these days. I’ve got two staging servers running it for two different projects and another running a production version of one of those projects.
In digging through the documentation I discovered two new command line tools that come included with the 2.0 version of Passenger.
From the documentation
passenger-status
One can inspect Phusion Passenger’s internal status with the tool passenger-status. This tool must typically be run as root. For example:

passenger-memory-stats
August 15th, 2008
If you’re seeing errors like this
... gems/activesupport-2.1.0/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method': undefined method `find_full_template_path' for class `ActionView::Base' (NameError)
You’re not crazy.
Active Scaffold (1.1.1) doesn’t play nice with Rails 2.1!
Go here to get an Active Scaffold version (master) that does.

August 2nd, 2008
There has never been a statue erected to honor a critic.
- Ziglar, Zig
I couldn’t believe that someone would have the gall to actually go on record with grouses as trite as these against the creators of mod_rails … but they did
Basically here are their complaints about Phusion Passenger/mod_rails
- Their product has ‘Enterprise’ in the title
- They shouldn’t be up to version 2.0 already (yes … they actually said that)
- They release FREE products ‘late’ (late being an excruciating 2 weeks)
- They’d like for them them to be more ‘modest’ (like they haven’t heard of DHH)
- The marketing seems too slick and something ‘fishy’ is going on because they’re trying to make money with an Enterprise ready version of Passenger/mod_rails?!?!
- The ‘rails community’ will not embrace mod_rails because of these laughable quibbles … I guess these guys don’t count as part of the ‘rails community’ right?
ridiculous.
PS: Note that the original ‘rant’ appeared on Ruby flow, where it was rightly eviscerated
June 16th, 2008
The kickass developers over at Phusion have released an update to Phusion passenger (or as I prefer to call it … Apache mod rails).
You can see the list of original improvements here

It seems that this particular problem with mod-rails hanging after a few hours and taking down apache with it (I experienced this in an apache deployment that I tried personally … and its a bit scary) was part of the motivation behind the original mod_rails/Passenger Release Candidate.
Updating mod_rails to to 2.0 made all my problems go away (mod_rails also had some trouble interacting with the soap4r plugin), but this update has even more fixes.
I have a site running on mod_rails that was formerly served up using 3 mongrels in a cluster and it runs a bit faster than before, but deployment and application management is obviously MUCH less of a headache.
These guys are doing something for Ruby on Rails, that I don’t think many understand the implication of, but cannot be overstated. Big cheers to the guys at Phusion!
June 16th, 2008
Just got bitten by this one in Ruby on Rails.
If you use empty? to check a hash slice that doesn’t exist you get an error, instead of the behavior of nil? which simply reports that it is actually nil …
irb(main):004:0> test = {}
=> {}
irb(main):005:0> test = {'test' => '1'}
=> {"test"=>"1"}
irb(main):006:0> test
=> {"test"=>"1"}
irb(main):007:0> test.empty?
=> false
irb(main):008:0> test[test]
=> nil
irb(main):009:0> test['test']
=> "1"
irb(main):010:0> test['test'].empty?
=> false
irb(main):011:0> test['test'].nil?
=> false
irb(main):012:0> test['testx'].nil?
=> true
irb(main):013:0> test['testx'].empty?
NoMethodError: undefined method `empty?' for nil:NilClass
from (irb):13
irb(main):014:0>
June 16th, 2008
I ran into this problem after I installed mod-rails on a cent OS server.
To get my changes to take … I kept running
httpd -k graceful
and it kept working until, all of a sudden, it didn’t.
This was the error I got
[XXXX@XXXXXXXXX]# httpd -k graceful
httpd not running, trying to start
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:443
no listening sockets available, shutting down
unable to open logs
argh!
Then I found this marvellous blog posting on how to fix the “Address already in use: make_sock: could not bind to address – Apache – HTTPD Error”
Here is what I did. (click to enlarge)

worked like a charm too.
June 9th, 2008
Previous Posts