So I’ve been struggling with “Queue Wait” spikes on NewRelic. I think I have pretty much determined that the spikes are caused by Passenger spinning up new threads to handle additional requests. Here’s what I did.
First I restarted my app, and let it run for a bit, then I used “passenger-status” to see what the threads were doing:
----------- General information -----------
max = 30
count = 3
active = 0
inactive = 3
Waiting on global queue: 0
----------- Domains -----------
/srv/rails/myapp/current:
PID: 9734 Sessions: 0 Processed: 48 Uptime: 18m 54s
PID: 9704 Sessions: 0 Processed: 49 Uptime: 19m 5s
PID: 9706 Sessions: 0 Processed: 57 Uptime: 19m 2s
I’d basically just run it again every few minutes to see what was happening, and then I got this:
----------- General information -----------
max = 30
count = 4
active = 0
inactive = 4
Waiting on global queue: 0
----------- Domains -----------
/srv/rails/myapp/current:
PID: 9822 Sessions: 0 Processed: 34 Uptime: 4m 57s
PID: 9706 Sessions: 0 Processed: 103 Uptime: 26m 43s
PID: 9704 Sessions: 0 Processed: 107 Uptime: 26m 46s
PID: 9734 Sessions: 0 Processed: 75 Uptime: 26m 35s
Mon May 17 13:54:20 MDT 2010
(I actually started running passenger-status; date so I could have the time I ran the command). The above tells us that at roughly 13:49 Passenger spun up another thread. Refreshing NewRelic, I saw this:

I spent more time investigating, and it does seem that the queue wait spikes, roughly correspond to times that Passenger starts more threads.
Posted in Rails, Ruby, Software Development.
Tagged with Apache, newrelic, passenger, Rails.
By jtanium
– May 17, 2010
I finally decided to bite the bullet and move the main Rails project I work on from FixtureReplacement to Factory Girl. Weighing in at 7404 lines of test code, this was a non-trivial task.
If you peruse the Factory Girl docs, it mentions associations, but only how to handle a simple “belongs to” or maybe “has one” sort of association. My good friend led me to this thread on StackOverflow.
Basically, the user fowlduck came up with the solution of having an “after_build” block.
My problem couldn’t be solved with a simple “has_many/belongs_to” association, because in this case I needed to ensure the invoice seller matched the producer of the product being invoiced. Here was my solution:
Factory.define :invoice do |i|
i.after_build do |invoice|
invoice.buyer = Factory.create :consumer unless invoice.buyer
next if invoice.seller
invoice.line_items = [Factory.create(:line_item)]
invoice.seller = invoice.line_items.first.product.producer
end
end
So first, notice the “unless invoice.buyer” and “next if invoice.seller” these allow me to override these values in my tests. You’re gonna wanna keep that in mind. Next, I create a LineItem, which automatically creates a product, which in turn, creates a producer. After that I can just set the seller to be the producer of the product in the first line item. Voila, product line_item producer matches invoice seller.
I’ve found this tremendously useful. Here’s another example of how I’ve used it:
Factory.define :user do |a|
name = Faker::Name.name
a.real_name { name }
a.username { Faker::Internet.user_name(name) }
a.password 'secret-1'
a.password_confirmation 'secret-1'
a.email_address { Faker::Internet.email(name) }
a.after_build do |login|
next if login.valid?
next unless login.errors.invalid?(:username)
login.username = Faker::Internet.user_name(login.real_name)+String.random(4)
end
end
I did this because, everyone once in a great while my tests would fail because the same username is used more than once. But now, if this happens, I just tack on 4 random characters. Since i did this, I haven’t had the test fail.
Posted in Rails, Ruby, Software Development, Tips and Tricks.
Tagged with activerecord, associations, factory_girl, Rails, Ruby, testing.
By jtanium
– May 17, 2010
So I’ve spent the afternoon slogging through this little problem. Something tells me I’m going to be referring to this post again and again.
Are you using the fetcher plugin and Capistrano for deployment? Hey, me too! Apparently I’m the only one in the world who didn’t immediately know how to make them work together though.
Here are my Capistrano tasks for starting/stopping/restarting my fetcher daemons:
after "deploy:restart", "fetcher:restart"
namespace :fetcher do
desc "Stop the fetcher_mailer daemons"
task :stop, :roles => :app do
run "RAILS_ENV=#{rails_env} ruby #{current_path}/script/mailer_daemon_fetcher stop"
end
desc "Start the fetcher_mailer daemons"
task :start, :roles => :app do
run "RAILS_ENV=#{rails_env} nohup /opt/ruby-enterprise/bin/ruby #{current_path}/script/mailer_daemon_fetcher start", :pty => true
end
desc "Restart (cycle) the fetcher_mailer daemons"
task :restart, :roles => :app do
stop
start
end
end
The real key here is on the :start task, there are two things you need to do to get the shell to let go of the process.:
- Add the
nohup to your command. If you don’t do this the process goes into the background, and as soon as the shell exits the OS says “SIGHUP” (“you don’t have to go home, but you can’t stay here.”)
- The second thing is the
:pty => true. This tells Capistrano to request a pseudo terminal. I don’t know all the nitty gritty, and have neither the time nor the inclination to dig in and find out why.
So there you go, add a nohup and :pty => true and then you can get your fetcher daemon backgrounded.
Posted in Rails, Ruby, Software Development, Tips and Tricks.
Tagged with capistrano, fetcher, nohup, Rails, Ruby, ruby on rails, task.
By jtanium
– March 12, 2010
If you’re having problems testing your helpers because of messages like these:
The error occurred while evaluating nil.url_for
The problem, it turns out is ActionView::TestCase doesn’t like url_for, it seems to expect you’re using named routes. So, in your helper, instead of doing something like:
def link_to_awesomeness(my_object)
if my_object.awesome?
link_to 'Awesomeness', :action => 'show_awesomeness', :id => my_object
else
link_to 'Make Awesome', :action => 'make_awesome', :id => my_object
end
end
Try something more like this:
def link_to_awesomeness(my_object)
if my_object.awesome?
link_to 'Awesomeness', awesomeness_path(my_object)
else
link_to 'Make Awesome', make_awesome_path(my_object)
end
end
Of course this implies you’ve created those named routes in your config/routes.rb, e.g.:
map.awesomeness 'my_objects/:id/awesomeness', :controller => 'my_objects', :action => 'show_awesomeness'
map.make_awesome 'my_objects/:id/make_awesome', :controller => 'my_objects', :action => 'make_awesome'
Posted in Rails, Ruby.
By jtanium
– February 16, 2010
So I was cruising along adding a new model to a project. The model is being used to store credit card information and when I generated the model, I absentmindedly did:
script/generate model CreditCard ... encrypted_expiration_date:date ...
Chugging along writing my tests I wrote a test like this:
class CreditCardTest < ActiveSupport::TestCase
context "A CreditCard instance" do
subject { CreditCard.new(:expiration_date => new_expiration_date) }
should("ensure expiration_date is encrypted") do
assert_equal expiration_date_cipher_text, subject.encrypted_expiration_date
end
def new_expiration_date; Date.today+90; end
def expiration_date_cipher_text; "!!!EXPIRATION_DATE_CIPHER_TEXT!!!"; end
end
And the model looking something like this:
class CreditCard < ActiveRecord::Base
def expiration_date=(exp_date)
@expiration_date = exp_date
self.encrypted_expiration_date = encrypt(exp_date)
end
The test would fail every time like:
Finished in 0.030941 seconds.
1) Failure:
test: should encrypt expiration_date. (CreditCardTest)
[test/unit/credit_card_test.rb:53:in `__bind_1261076018_868915'
shoulda (2.10.2) [v] lib/shoulda/context.rb:351:in `call'
shoulda (2.10.2) [v] lib/shoulda/context.rb:351:in `test: should encrypt expiration_date. ']:
<"!!!EXPIRATION_DATE_CIPHER_TEXT!!!"> expected but was
.
1 tests, 1 assertions, 1 failures, 0 errors
Can you guess the problem? Ten points to Gryffindor if you said it's because the column type is date. And another five points if you can say it's because ActiveRecord is tries to parse it to be a date object, which fails, and returns nil.
Posted in Rails, Ruby, Software Development, Tips and Tricks.
By jtanium
– December 17, 2009
This entry on the Internet Storm Center’s Handler’s Diary was about “Distributed WordPress admin account cracking” — scary stuff.
In the the article they suggest limiting the addresses from which the admin can be accessed. If you’re using Apache, here’s one way using the <Location> directive:
<Location /wp-admin>
Order Deny,Allow
Deny from all
Allow from example.com 10.211.34.83
</Location>
<Location /wp-login.php>
Order Deny,Allow
Deny from all
Allow from example.com 10.211.34.83
</Location>
Posted in Apache, Security, Tips and Tricks.
Tagged with Apache, brute force, Security, tips, wordpress.
By jtanium
– November 30, 2009
If you’re seeing this in your test:
Expected response to be a <:redirect> , but was <0>
You are probably doing something like this in your controller:
redirect_to :action => 'messages', :status => 'read'
Watch out when using :status in your params. :status is used to set the http status code in the redirect_to method, like so:
redirect_to :action=>'atom', :status=>302
Posted in Rails, Ruby, Software Development, Tips and Tricks.
Tagged with Rails, Ruby, ruby on rails, tips.
By jtanium
– November 20, 2009
Sometimes I want to make sure that a script is *not* being run by the root user:
#!/bin/bash
if [[ $EUID -eq 0 ]]; then
echo "Don't run as root!"
exit 1
fi
Posted in Bash, Tips and Tricks.
Tagged with Bash, root, unix.
By jtanium
– November 4, 2009
So I’ve been trying to figure this error out, it just cropped up:
`@my_object[such_and_such_attributes]' is not allowed as an instance variable name
As is usually the case with Rails, the answer is pretty obvious once you understand what’s going on. Here’s the relevant code:
<% fields_for 'my_object[such_and_suches_attributes]', @my_object.such_and_suches.first do |such_and_such_form| -%>
<%= such_and_such_form.text_field :name %>
<% end -%>
So why was the above creating the error? It’s because @my_object.such_and_suches was empty, thus Rails would try and use “my_object[such_and_suches_attributes]” as the attribute name which naturally didn’t work. This is the shortcut that allows this to work:
<% fields_for :posts do |post_form| -%>
<%= post_form.text_field :title %>
<% end -%>
Now in my situation, why the “such_and_suches” is empty is a whole other problem…
Posted in Rails, Ruby, Software Development, Tips and Tricks.
By jtanium
– November 3, 2009
Try and run the following in Firefox, and it’s gonna tell you “you’re screwed”:
var newTable = document.createElement('table');
var rowContent = '<td>some stuff</td><td><em>something else</em></td>';
var newTr = document.createElement('tr');
newTr.innerHTML = rowContent;
newTable.appendChild(newTr);
if ('<td>some stuff</td><td><em>something else</em></td>' == newTr.innerHTML) {
alert("Everything's hunkydory");
}
else {
alert("You're screwed");
}
Why? My guess is Firefox doesn’t like it when you add td tags to elements that aren’t in a table, so it strips them. After doing the innerHTML assignment
<td>some stuff</td><td><em>something else</em></td>
becomes
some stuff<em>something else</em>
Fortunately, the solution is easy, just append the tr before the innerHTML assignment:
newTable.appendChild(newTr);
newTr.innerHTML = rowContent;
Then you’ll see “Everything’s hunkydory”.
Posted in Javascript, Software Development, Tips and Tricks.
Tagged with appendchild, firefox, html, innerhtml, Javascript.
By jtanium
– October 28, 2009
Recent Comments