Background tasks for your Merb

June 23, 2008 | Reporter: hassox

Ezra has just pushed a small but significant change to merb that allows for native, in merb support for running background tasks.

Ezra has just added the run_later method. This very simple method just captures a block and runs it in a worker thread that is started at runtime. This should allow you to run anything you like sequentially in the background of your application :)

It’s funny how these small code changes with big implications come about. This weekend was rails camp in Australia and as usual at this event, I came to realize anew just how much talent there is down in Aus. Talking with our resident cache guru matta (and a bunch of other cool ruby peeps) about possible caching strategies for merb lead to a requirement for out of request cycle processing. I think a framework should have the ability to do this. A quick chat with Ezra and, as usual he was gracious enough to get right on it :)

The results are sweet :) Checkout the Change and also the wiki page

Comment On Background tasks for your Merb

On June 24, 2008 at 00:02 tooky says:

grumble and I just spent a day moving the core parts of workling over to merb!

Great stuff! :)

On June 24, 2008 at 08:08 afrench says:

very spiffy feature, but I wonder how it scales…

On June 24, 2008 at 08:54 maxime says:

Really cool. Useful for sending emails for example.

What about run_later(24.hours)? :p

Ok, I may ask too much :)

On June 24, 2008 at 10:00 jason_l says:

Very cool – I agree with maxime, this is perfect for emails.

One word of caution – I haven’t tested in production environment, but in development environment, if you crash the thread, you’ll crash the server. In other words, a method call that would throw an exception (and result in a 500 error passed back to the client) will not normally take the server down, but the same code when passed to the thread via run_later will take the server down. Of course, a begin..rescue block will prevent the server from crashing. See this paste for an example.

On June 24, 2008 at 16:45 hassox says:

@maxime it is just a simple fire and forget mechanism. It’s simply a queue that you can put jobs that need to be “run later” If you need scheduling of a specific job then it’s worth looking into getting something like background job happening for Merb I think.

On June 24, 2008 at 17:50 evolving_jerk says:

@jason_l:

You can wrap code in the block in begin/rescue if you feel exceptions may be the case.

On June 24, 2008 at 19:56 ezmobius says:

@jason_l good catch. I’ve added code to rescue any exceptiosn in the worker thread, log them and restart the work queue.

Sign in to make your voice heard