ActionView::Template::Error: No response from searchd (status: , version: )

I came home from a week long company hackathon to see my newrelic error reporting going crazy …

sphinx error driving new relic CRAZY

sphinx error driving new relic CRAZY

My free bugsnag account had already maxed out of the 2000 error allocation I had for the entire month (I’ll usually get 100 in a month … maybe).

I immediately knew from the exception in new relic that something was wrong with Sphinx, and I figured it was pretty bad because on each page load of news pages, I hit sphinx to give me a list of related news stories, which meant that the crawlers that hit my site every second of the day could be generating loads of exceptions.

However, when I went to pull up one of my news pages … it loaded just fine …

hmmm.

I hit the search page, and got a 500 error. Weird as that may be, I’d encountered that before. I immediately logged into my vps and ran

`rake ts:restart`

at the console and was ready to drop my mic and moonwalk back to my sofa to catch up on “The Americans”.

Then I looked at my new relic account and noticed that the same error was still coming in 10 minutes after it should have been fixed.

hmmmmmmmmmmmm.

I tried hitting the urls specified in the errors and got the 500 error. I realized that some urls were exhibiting the problem while others weren’t.

I’d never seen that before :\

My google-fu quickly turned up this non-upvoted gem that helped me fix the issue. Basically you just have to rotate your sphinx index because that error means parts of it have gone “stale”.

the command I used to accomplish this was

`/usr/bin/indexer –rotate xx_core –config /path/to/your/sphinx/production.sphinx.conf`

your indexer command might be located somewhere different though; to locate mine I just used the `whereis indexer` command

You’ll want to change xx_core to match the name of your index (which you can find by looking in your sphinx.conf file)

Hope this helps you out!

How to freeze old rails gems

You probably came here wondering why

rake rails:freeze:edge RELEASE=2.3.2

doesn’t work, right? (It times out trying to pull the rails gem from http://dev.rubyonrails.org/archives/rails_2.3.2.zip)

Now you could try to hack the rake task in your rails gem

OR

– you could just go to rails github page https://github.com/rails/rails
– click on the ‘switch tags’ button  and select the version of rails you’re looking for
– after the page reloads, click on download and select the ‘zip’ version
– download it to your ‘vendor’ directory in the rails app you’re trying to freeze rails to
– unzip the file and rename the directory to rails
– you’re done!

To make sure you’re running a vendored copy of rails you can just  put

<%= debug Rails.vendor_rails? %>

somewhere in one of your views, or run

Rails.vendor_rails?

from the console.
enjoy.

Uploading a file using the simple_form rails gem

Simple Form is a rails gem that takes a lot of stress out of dealing with forms in Rails. Knowing what field nomenclature handles file uploading should be simple but its not. It took a lot of screwing around and reading the documentation to figure it out.

Simple Form uses the Provided Rails Form helpers so, if you want to specify a file upload field in Rails, just do this

<%= simple_form_for @user, :html => {:multipart => true}  do |f| %>
    <%= f.file_format :photo, :label => 'Your avatar please' %>
 <% end %>

Passenger dying … too many apache processes

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.


How to get a 25% boost in your Rails app?

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.

New Relic monitor app showing 25% improvement in response time

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?

requiring gems from github (specifically flickr-fu and xml_magic) using config.gem

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 😀

How to uninstall a plugin in Rails

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

Cakephp Filter gotcha for rails folks

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 Continue reading

New version (2.0.4) of Phusion passenger/mod rails out

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.

gem install error – ‘cl’ is not recognized as an internal or external command

UPDATE: See my new blog post on the issue

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’ Continue reading

Zed Shaw is a fan of Phusion Passenger (modrails)

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. Continue reading

New mod_rails/passenger memory tools and cool tutorial

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:

Continue reading