Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A port of Merb's run_later functionality to Rails 2.2 - updated for EventMachine support.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README
Octocat-spinner-32 Rakefile
Octocat-spinner-32 TODO
Octocat-spinner-32 init.rb
README
RunLater
========

Simply a blatant port of Merb's run_later method, taking advantage of Rails 2.2 thread safety.
Runs longer running tasks outside the current request thread, so that the request will return
the content to the user without blocking. Once the request is done, the worker will run the 
specified block in a separate thread.

It's merely a proof-of-concept for now, and my entry into the thread-safe Rails world, so I can't
guarantee anything about the code.

Should an error occur inside the block handed to run_later, the block will be discarded, and the
worker thread will just continue its work without looking back. If you're executing important code
inside the block, ensure that it does proper error handling.
 
Testing
=======

For testing you can explicitly disable threading of your blocks. Set RunLater.run_now to true in
your config/environments/test.rb and your blocks will be executed within the test case for your
testing pleasure.

Example
=======

class AccountController < ApplicationController
  def signup
    @user = User.new(params[:user])
    if @user.save
      run_later do
        AccountMailer.deliver_signup(@user)
      end
    end
  end
end

Known Issues
============

When using Passenger (http://modrails.com) and you're running tasks that take a little longer
you will experience a delay in development mode. This is because after each request, Rails
cleans up its environment and unloads all classes. run_later will defer the cleanup until
the worker thread is done or a timeout has occured (default is 10 seconds). This is true
for at least Passenger 2.0.6, but not in 2.0.3 and 2.1.1, and it will also happen when
using Mongrel, but not in production mode.

License, Props
==============

Thanks to Yehuda Katz for showing off run_later at the Merb BoF session at RailsConf Europe 2008.

Ported to Rails by Mathias Meyer (http://paperplanes.de).

Released under the MIT license
Something went wrong with that request. Please try again.