Rails 3: Rack-Throttle your Rack API

Sometimes, you will want to decouple your Rails app and place your API in its own special folder in lib. You could be using Sinatra, Grape, or even your own Rack app. The advantage is you can separate out your API code, making it just that much easier to port over to another app. This guide is not intended to show you how to extract an API from your code, but how to throttle an API that has already been extracted. For some tips on how you can mount your API in your Rails app, take a look at the Inductor blog for a quick intro.

Now that you’ve skimmed the basics of mounting your Rack app (e.g. API) in your Rails app without touching your rackup file, you’re all set for the next step: throttling! testing!

Let’s go over a quick example with Cucumber. Don’t worry, I’ll keep it simple.
Continue reading

Posted in Ruby on Rails | Tagged , , | Leave a comment

Play Sound (Universal)

I received some requests to update the Play Sound Automator action to work with newer Macs, so I went and posted the source to Github. Upon restoring the source code from a backup, it was quite a surprise finding out that it was already a git repo… five years ago!

I also rebuilt a binary version of the action, which is incredibly simple to install: just double-click. It is also compatible with both PPC and Intel Macs, I had success running it with 10.4 on a G4 Mac Mini. You can download from Github or download from my site.

Additionally, it is now open source so if you see any changes (the code has been changed in over five years), fork it on Github and let me know!

Special Thanks:
StackOverflow — Create Universal Automator Action
Google Fodder — 64-bit Automator and Snow Leopard

Posted in Cocoa, Development, Local Talk | Leave a comment

Passenger and nginx Maintenance Page

I recently had the pleasure of moving a Rails app from Apache/Passenger based system to an nginx/Passenger system, deployed by Capistrano. In Apache, I had a setup where I could take a production site down for maintenance by touching ‘tmp/stop.txt’ and using the following directive in my VirtualHost (courtesy of Otto Hilska at Nodeta):

<VirtualHost *:80>
	ServerName www.example.com
	DocumentRoot /u/apps/example/current/public
	ErrorDocument 503 /maintenance.html
	RewriteEngine on
	RewriteCond %{DOCUMENT_ROOT}/../tmp/stop.txt -f
	RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
	RewriteCond %{SCRIPT_FILENAME} !maintenance.html
	RewriteRule ^(.*)$ /$1 [R=503,L]

	<Directory /u/apps/example/current/public>
		RailsEnv production
		Options -MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>
</VirtualHost>

This worked perfectly, and allowed me to use images/css/etc on my error documents without them being redirected to 503s as well. Based on Eli Miller’s solution, I was able to write something similar for my nginx server config:

server {
	listen 80;
	server_name www.example.com;
	error_page 503 /maintenance.html;
	root /u/apps/example/current/public;
	passenger_enabled on;
	rails_env production;

	# If the maintenance stop file exists
	if (-f $document_root/../tmp/stop.txt) {
		set $maintenance 1;
	}

	# If the request exists as a static file in public
	if (-f $document_root/$uri) {
		set $maintenance 0;
	}

	if ($maintenance) {
		return 503;
	}
}

I was able to test this successfully with nginx 0.7.67 and 0.8.53, and Passenger 2.2.15 and 3.0.1. If you know a better/clearer way to do this, please share!

Posted in Development, Ruby on Rails | Tagged , , , | 1 Comment