Runner is a background task processor which is based on both spawn and delayed_job Unlike delayed_job, Runner doesn't need a separate rake task which runs your background tasks.
Easy way to implement other backends. Currently only supports ActiveRecord.
Convenient way to run methods as a separate process (see Quick-how-to).
Different concurrency methods supported (Forking, Threading and yielding)
Methods can be queued to be run at a later time giving you the option to run jobs at night by cron for example.
TaskSpawner which can be used to run queued tasks in the background.
You can use the latest Rails 3 gem with the latest Runner gem and include it in your Gemfile:
gem 'runner', :git => 'git://github.com/stygeo/runner.git'
After you installed Runner thru bundler, you need to run the generator:
rails g runner
The runner generator generates a Migration file and an initializer file. If you use migrations please run:
Please refer to the Migration file if you rather set up your table your self.
A Quick how to:
Running a task in the background is as easy as doing:
Instance methods are also supported:
klass = Klass.new klass.spawn.some_method_name
If you wish you may also use blocks instead of methods for spawn
class Klass def my_method 2 + 2 spawn do # Execute this long running block in the background end end end
And of course you can just pass options to the spawn block
spawn(:with => :thread) do # Execute end
If you'd like to have an instance method always run in the background you could use the handle_asynch class method. This forces the method to be run in the background no matter what:
class MyClass def my_method 2 + 2 end handle_asynch :my_method end c = MyClass.new; c.my_method # => my_method will run in the background.
If don't want your method to be run immediately you can choose to queue the call, which gives you the option to run it at a later time in the background:
Klass.spawn(:method => :queue).some_method_name
Somewhere else in your application you could start up a task spawner which runs all tasks which are currently queued for running:
c = Runner::TaskSpawner.new c.start_handlers
If you would like to overwrite the default concurrency method you could pass :with to spawn for non-queued processes:
Klass.spawn(:with => :thread).hello # => Forces the hello method to be executed in a thread rather than the default option.
For queued tasks you could force the TaskSpawner to use a specific concurrency method
Klass.spawn(:method => :queue, :with => :yield).some_method_name # => :with is ignored
Somewhere else in your application you could do:
c = Runner::TaskSpawner.new(:with => :fork) c.start_handlers # => Forces all tasks to be forked
Please note that queued calls are subject to chance.
TODO Update this README file :)