Class Mongrel::Configurator
In: lib/mongrel.rb
Parent: Object

Implements a simple DSL for configuring a Mongrel server for your purposes. More used by framework implementers to setup Mongrel how they like, but could be used by regular folks to add more things to an existing mongrel configuration.

It is used like this:

  require 'mongrel'
  config = Mongrel::Configurator.new :host => "127.0.0.1" do
    listener :port => 3000 do
      uri "/app", :handler => Mongrel::DirHandler.new(".", load_mime_map("mime.yaml"))
    end
    run
  end

This will setup a simple DirHandler at the current directory and load additional mime types from mimy.yaml. The :host => "127.0.0.1" is actually not specific to the servers but just a hash of default parameters that all server or uri calls receive.

When you are inside the block after Mongrel::Configurator.new you can simply call functions that are part of Configurator (like server, uri, daemonize, etc) without having to refer to anything else. You can also call these functions on the resulting object directly for additional configuration.

A major thing about Configurator is that it actually lets you configure multiple listeners for any hosts and ports you want. These are kept in a map config.listeners so you can get to them.

  • :pid_file => Where to write the process ID.

Methods

Attributes

defaults  [R] 
listeners  [R] 
needs_restart  [R] 

Public Class methods

You pass in initial defaults and then a block to continue configuring.

Public Instance methods

Change privilege of the process to specified user and group.

Do not call this. You were warned.

generates a class for cloaking the current self and making the DSL nicer

Daemonizes the current Ruby script turning all the listeners into an actual "server" or detached process. You must call this before frameworks that open files as otherwise the files will be closed by this function.

Does not work for Win32 systems (the call is silently ignored).

Requires the following options or defaults:

  • :cwd => Directory to change to.
  • :log_file => Where to write STDOUT and STDERR.

It is safe to call this on win32 as it will only require the daemons gem/library if NOT win32.

Calling this before you register your URIs to the given location will setup a set of handlers that log open files, objects, and the parameters for each request. This helps you track common problems found in Rails applications that are either slow or become unresponsive after a little while.

You can pass an extra parameter what to indicate what you want to debug. For example, if you just want to dump rails stuff then do:

  debug "/", what = [:rails]

And it will only produce the log/mongrel_debug/rails.log file. Available options are: :objects, :rails, :files, :threads, :params

NOTE: Use [:files] to get accesses dumped to stderr like with WEBrick.

This method should actually be called outside of the Configurator block so that you can control it. In other words do it like: config.join.

Starts a listener block. This is the only one that actually takes a block and then you make Configurator.uri calls in order to setup your URIs and handlers. If you write your Handlers as GemPlugins then you can use load_plugins and plugin to load them.

It expects the following options (or defaults):

  • :host => Host name to bind.
  • :port => Port to bind.
  • :num_processors => The maximum number of concurrent threads allowed. (950 default)
  • :timeout => 1/100th of a second timeout between requests. (10 is 1/10th, 0 is timeout)
  • :user => User to change to, must have :group as well.
  • :group => Group to change to, must have :user as well.

Loads the MIME map file and checks that it is correct on loading. This is commonly passed to Mongrel::DirHandler or any framework handler that uses DirHandler to serve files. You can also include a set of default MIME types as additional settings. See Mongrel::DirHandler for how the MIME types map is organized.

Uses the GemPlugin system to easily load plugins based on their gem dependencies. You pass in either an :includes => [] or :excludes => [] setting listing the names of plugins to include or exclude from the when determining the dependencies.

Easy way to load a YAML file and apply default settings.

Logs a simple message to STDERR (or the mongrel log if in daemon mode).

Loads and creates a plugin for you based on the given name and configured with the selected options. The options are merged with the defaults prior to passing them in.

Let’s you do redirects easily as described in Mongrel::RedirectHandler. You use it inside the configurator like this:

  redirect("/test", "/to/there") # simple
  redirect("/to", /t/, 'w') # regexp
  redirect("/hey", /(w+)/) {|match| ...}  # block

This will resolve the given options against the defaults. Normally just used internally.

Works like a meta run method which goes through all the configured listeners. Use the Configurator.join method to prevent Ruby from exiting until each one is done.

Used to allow you to let users specify their own configurations inside your Configurator setup. You pass it a script name and it reads it in and does an eval on the contents passing in the right binding so they can put their own Configurator statements.

Sets up the standard signal handlers that are used on most Ruby It only configures if the platform is not win32 and doesn’t do a HUP signal since this is typically framework specific.

Requires a :pid_file option given to Configurator.new to indicate a file to delete. It sets the MongrelConfig.needs_restart attribute if the start command should reload. It’s up to you to detect this and do whatever is needed for a "restart".

This command is safely ignored if the platform is win32 (with a warning)

Calls .stop on all the configured listeners so they stop processing requests (gracefully). By default it assumes that you don’t want to restart and that the pid file should be unlinked on exit.

Called inside a Configurator.listener block in order to add URI->handler mappings for that listener. Use this as many times as you like. It expects the following options or defaults:

  • :handler => HttpHandler — Handler to use for this location.
  • :in_front => true/false — Rather than appending, it prepends this handler.

Writes the PID file but only if we’re on windows.

[Validate]