When working with applications whose actions may take some time to complete, it may be better to handle the request asynchronously. A quick and easy way to do this is using Starling and Workling. Starling is a light weight message queue based on the Memcache protocol, and Workling is a simple, lightweight consumer. Setup is dead simple:
First, install Starling:
sudo gem install starling |
This will install Starling and it’s dependencies (memcache-client and eventmachine) if you don’t already have them.
Now install Workling. This doesn’t have a gemspec so we will install it as a plugin:
cd ~/path_to_your_project script/plugin install git://github.com/purzelrakete/workling.git |
Finally, tell Workling, which will want to use Spawn by default if it is installed on your machine, to use Starling by placing this in your environment.rb:
Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new |
That is it for the installation process! Easy. Now for actually handling requests. Believe it or not, it is just as simple as the installation. Say you have a controller that has to do several long running tasks:
class SkinnyController < ActionController::Base def fat_action .. do some crazy stuff that takes a few minutes .. end end |
Now typically, you should avoid doing things that take longer than a few seconds to complete. And this is okay for most application requirements, however in some cases, it is inevitable that a few tasks will take much longer, such as above. That is where Workling comes in. Simply refactor the code into a worker (conveniently located in app/workers):
# app/workers/fat_worker.rb class FatWorker < Workling::Base # this method can be named anything you want def do_work(*args) .. you get the picture .. end end |
Now, in your controller, call the worker:
class SkinnyController < ActionController::Base def fat_action FatWorker.asynch_do_work(some_args) end end |
Just start up starling and workling (starling start, and script/workling_client start respectively) And that is all. You can now handle large tasks asynchronously, and because the tasks are queued with starling, the action can be called multiple times, and it will queue up the worker and process it as soon as the previous tasks are complete.




