Archive for the ‘Ruby on Rails’ Category

Sleeping Between API Requests

Tuesday, December 22nd, 2009

I spent a few minutes today debugging the reason why one of my “rake” tasks was getting 503 connections (Service Unavailable) from Yahoo’s API.

I had mistakenly thought this was due to the fact that Yahoo limits the number of requests to their API in a given day. I decided to put a “sleep” call in my code to be sure it wasn’t being denied access due to server-side throttling. Sure enough — all requests succeeded after adding a brief call to the “sleep” function.

One of my rails applications relies on daily updated statistics from a variety of APIs for a large set of domains (all within a loop). After adding a 2 second pause just before making the relevant API calls was enough to allow all my requests to succeed.


for dir in @directories
  sleep 2
  # do the api_call()
end

The caveat here is that the number of requests you are making will depend on how long it takes to complete…adding a 2 second pause will substantially increase the completion time. In my case it takes about 33 minutes (2 seconds x 991 domains)….obviously with twice as many domains, it would take over an hour to complete.

I have not tried, but using a fractional sleep (ie: .5 seconds) would probably be enough to succeed, depending on whatever Yahoo’s servers are willing to allow (ie: sleep .3). Also, I have considered extending the amount of time in between batch runs. I currently am collecting stats every 3 hours. I could decrease this crontab job to run once a day, or only run the report on domains that haven’t had a new stat created in the last 7 days (for example).

VN:F [1.8.4_1055]
Rating: 9.7/10 (3 votes cast)
VN:F [1.8.4_1055]
Rating: +1 (from 1 vote)

Quick and Dirty Guide to Deploying Rails with Capistrano

Wednesday, July 29th, 2009

Capistrano is one of those problem solving apps that can be tricky to setup at first, but is a no-brainer once you’ve deployed with it.

I added a few notes about hosting on Dreamhost because they only support rails 2.2.2 at the time of this writing. My current app is actually developed under 2.3.2.

With Dreamhost, you only get a RAILS_ENV=’production’ setup on Passenger hosting…but that is good enough. I prefer to run test.the-app.com locally anyway.

If an app of mine gets popular, I would move it off of dreamhost regardless ( security, reliability, etc ). Try slicehost or linode if you’re looking for a more professional web hosting solution.


#create the_rails_app_production database
#setup db.the-rails-app.com db server
#setup domain the-rails-app.com to look in ./current

#!/bin/bash
cd ~/the_rails_app
mkdir config/deploy
for e in 'staging production testing'; do touch config/deploy/$e.rb; done;

echo '#!/bin/bash' > script/spin
echo 'touch tmp/restart' >> script/spin && chmod 755 script/spin

# dreamhost only:
# change config/environment.rb to require rails 2.2.2
# change config/database.yaml (production:) and delete the socket ref for mysql
# add symlink to older application.rb controller

svn ci -m 'initial capify'

capify .
cap deploy:setup
cap deploy:check
cap deploy:update #fix all errors until this succeeds
cap deploy:migrate #initial migration of db
#if using 2.2.2 be sure to symlink application_controller.rb to application.rb

cap deploy:start

# if you modify anything, be sure to checkin and deploy:update again

VN:F [1.8.4_1055]
Rating: 10.0/10 (1 vote cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)

Dreamhost Rails Hosting Review

Monday, June 22nd, 2009

I have tried both mongrel and passenger rails hosting on Dreamhost.

Here are some pitfalls I found:

  • no clustering (mongrel_cluster not installed)
  • one proxy per app
  • one mongrel per app
  • apache + fastcgi works fine on the shared host plan
  • PrivateSersver is 3 times the cost for 1/4 the performance
  • your rails environment is *always* production
  • they refuse to switch people back to shared.

I have about 30 domains, not a huge amount of traffic, well under “unlimited” … and now I’m paying $30-60/month for what cost me $10 before, nothing much gained from a rails standpoint, because I can’t host multiple environments anyway. The default environment supported for Passenger implementations is “production” — you could override that in the application’s configuration file, but it goes against the principle of easy deployment to any environment.

The disk reports as full at my usage of 6.1 gigs out of my 351 gigs allowed and I’m constantly getting “DISK QUOTA” errors to which they reply — “Naah…check your again.” After repeated emails, they ask what the command was again that caused the error (pasted in original ticket).

I have since started developing a rails app for Link directories on a Slicehost server instead, although the basic package is fairly limited (256mb ram) it handles pretty well with low traffic.

VN:F [1.8.4_1055]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)