Expedite
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.
Usage
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.
This is the "parent" agent:
Expedite.define do
agent :parent do
before(:serve) do |name|
$parent_var = name
end
end
end
You can define agents that are based on other agents. You can also have wildcard matchers.
Expedite.define do
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
parentagent. - Fork from the
parentagent to create thedevelopment/abcagent. - Fork from the
development/abcagent to run theinfocommand, 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 -
Acknowledgements
Expedite's server core is modified from Spring