Expedite is a Ruby preloader manager that allows commands to be executed against preloaded Ruby applications. Preloader applications can derive from other preloaders, allowing derivatives to start faster.
To use expedite you need to define agents and actions in an expedite_helper.rb
that is placed in the root directory of your application. The sample discussed
in this section is in the examples/simple folder.
Logs can be configured by adjusting the environment
:
Expedite.define do
environment do
# Log to STDOUT
self.log_file = STDOUT
# Log to a file
self.log_file = "log.txt"
# No logs
self.log_file = nil
end
end
You define agents which are processes that will process the requests. You can create agents from forking other agents. In the example below, the "development/*" agents are forked from the "parent" agent.
Expedite.define do
agent :parent do
before(:serve) do |name|
$parent_var = name
end
end
agent "development/*" do
self.parent = :parent
before(:serve) do |name|
$child_var = name
end
end
end
The following defines an info
action.
Expedite.define do
action :info do
{
"Process.pid" => Process.pid,
"Process.ppid" => Process.ppid,
"$parent_var" => $parent_var,
"$child_var" => $child_var,
}
end
end
After defining your agents and actions, you can then use it. In the simple
example, the main.rb
calls the info
command on the development/abc
agent.
The invoke
method will execute the action, and return the result. There
is also an exec
method that will replace the current executable with
the action; in that case, the return result is the exit code.
require 'expedite'
puts Expedite.agent("development/abc").invoke("info")
When you run main.rb
, the following output is produced. Note that $sleep_parent
comes from teh parent
agent, and $sleep_child
comes from the development/abc
agent.
# bundle exec ./main.rb
Process.pid = 3855
Process.ppid = 3854
$parent_var = 1
$development_var = development/abc
Calling main.rb
automatically started the expedite server in the background.
In the above example, it does the following:
- Launch the
parent
agent. - Fork from the
parent
agent to create thedevelopment/abc
agent. - Fork from the
development/abc
agent to run theinfo
command, and then quit.
To explicitly stop the server and all the agents, you use:
$ bundle exec expedite stop
You can also start the server in the foreground.
$ bundle exec expedite server
If rails
is in the Gemfile
, then you can also start the rails commands through expedite.
$ bundle exec expedite rails console
$ echo "puts ActiveRecord::Base.connection" | bundle exec expedite rails runner -
Expedite's server core is modified from Spring