= RakeServer
=== What is it?
RakeServer is a lightweight client/server architecture for running Rake tasks. The server is a long-running process that loads tasks from a Rakefile and waits for requests to execute tasks. Each time a task is requested, the server forks, runs the task, and streams the output from the task back to the client.
When the server is started, it can be given tasks to eager-run; this is mainly useful for loading application code into the pre-fork environment.
=== What's the point?
The main intended use case for RakeServer is to provide a means for production deployments to run Rake tasks in the background (e.g. via a cron) without needing to load the application environment from scratch each time a task is executed. It may also be useful for remote invocation of Rake tasks, but that is not the main goal.
=== Basic usage
$ cd /path/to/my/rails/app $ rake-server start environment $ rake-client db:migrate
Valid commands to rake-server are start
, stop
, run
, and restart
. Any
additional arguments name Rake tasks that should be eagerly run when the server
starts. In the above example, we call the environment
task, which loads the
Rails environment for fast invocation of Rails-dependent tasks by the client.
=== Fork hooks
RakeServer forks a separate process each time the client invokes a task. In a Rails environment, it's necessary to re-connect to ActiveRecord each time the application forks. RakeServer provides pre-fork and post-fork hooks to make this easy.
First, in your Rakefile:
namespace :rake_server task :fork_hooks RakeServer.before_fork { ActiveRecord::Base.remove_connection } RakeServer.after_fork { ActiveRecord::Base.establish_connection } end end
Then, when you start the server, specify your fork hooks setup as an eager task:
$ rake-server start environment rake_server:fork_hooks