Rooster: A smarter daemon solution for Rails apps

I’ve been wanting to put up a little presentation I did at SocialVibe on a Rails plugin I wrote called Rooster.  It’s a daemon_generator replacement that uses EventMachine and Rufus::Scheduler to manage daemon/background processes.

Here’s the presentation:

Check out the project on github.

Using the latest memcache-client in Rails

One annoyance with ActiveSupport is waiting for the vendored libraries to get upgraded. Recently, we wanted the latest and greatest version (1.7.4) of the memcache-client as we were experiencing some interesting timeout issues.

Mike Perham, the memcache-client maintainer, documented how to do this (”Using memcache-client 1.6.x in Rails < 2.3″).  His hack works great if the memcache-client gem is installed, but not if you vendor the gem.  I like to have my gems under source control, so here is a how I hacked ActiveSupport to use memcache-client 1.7.4.


Working great so far :-)

FAILSAFE “bad content type” when POSTing multipart data in Rails

My coworker and I spent way too much time on this issue.

The task: Post an image from a Flash/ActionScript frontend to a Rails controller, populating an attachment_fu model. After several attempts, we kept getting the same result: A FAILSAFE error (”bad content type”) called from read_multipart. Here is the stack trace:

The controller’s create action simple populates the appropriate file field like so:
@dog_image.uploaded_data = params[:dog_image]

So what was going on? Well, the “bad content type” message was informative, but barely so. It turns out that the multipart data being POSTed from the SWF was malformed, and Rails didn’t like that. Only one of the many ActionScript multipart libraries seems to use proper formatting, and that one is MultipartURLLoader, as described in this post.

I had to write this post in the hopes that someone will benefit from it, as it took many hours of research to figure this out!


Fun with script/console: Ever tried app.get?

I came across a reference to a script/console session that looked like this:

Sweet. I had never heard of ActionController::Integration::Session, but now this little gem will be a regular tool for me for testing controller requests.

Submitting a Rails documentation patch for rescue_from

The existing documentation for Rails’ rescue_from is lacking in my opinion.

Intuitively, I thought that the following was a simple enough setup:

I was wrong. My thought was that just like rescue blocks, the more specific instances of the Exception should be listed first, followed by more generic Exception classes (perhaps finishing with StandardError should you want a catch-all handler). In fact, the exact opposite ordering is used: The more generic Exception classes should be listed first, followed by more specific classes. Listing a superclass such as StandardError first actually precludes other handlers from running.

Thanks to this comment on ApiDock for the guidance. I have requested access to lifo’s Rails documentation repo on github so that I can commit a docpatch.  I also plan to include in the docpatch a better usage example that shows off the rescuing of multiple exception types in one declaration, like so:

Stay tuned…

A handy monkey-patch: Add to_xml to Ruby’s StandardError

While working on some error handler refactoring in a Rails app, I added this bit of code to my application controller (application.rb):

Then I found that the method StandardError#to_xml that I expected to be defined, actually was not. So I put this into an initializer:

Itch scratched!

Rails plugin mysql_replication_adapter for Rails 2.2+

Just a quick post to make available to the internet that I’ve updated the mysql_replication_adapter gem/plugin to be compatible with Rails 2.2+.

The original mysql_replication_adapter was first made available by RapLeaf.  It was a gem that was released in mid-2007 and hosted on rubyforge.  However, development stalled and when Rails when to version 2, the gem was no longer compatible.  Fortunately, in mid-2008 rubyforge user “ckiernan” patched the gem and converted it into plugin, making it compatible with Rails 2.  The patch was never committed to the main repository, so the plugin was solely available on this rubyforge tracker page.

However, the plugin again became useless with the release of Rails 2.2.  The call to “require_mysql” was bombing as that method was apparently removed in Rails 2.2.  And so I fixed that and put it on github for all of the Rails world to enjoy.  “Git” it here:

http://github.com/findchris/mysql_replication_adapter/tree/master

Hope someone benefits from this.