This class is capable of spawning Ruby on Rails application instances quickly. This is done by preloading the Ruby on Rails framework into memory, before spawning the application instances.

A single FrameworkSpawner instance can only hold a single Ruby on Rails framework version. So be careful when using FrameworkSpawner: the applications that you spawn through it must require the same RoR version. To handle multiple RoR versions, use multiple FrameworkSpawner instances.

FrameworkSpawner uses ApplicationSpawner internally.

Note: FrameworkSpawner may only be started asynchronously with AbstractServer#start. Starting it synchronously with AbstractServer#start_synchronously has not been tested.

Methods
Included Modules
Classes and Modules
Class PhusionPassenger::Railz::FrameworkSpawner::Error
Public Class methods
new(options = {})

Creates a new instance of FrameworkSpawner.

Valid options are:

  • :version: The Ruby on Rails version to use. It is not checked whether this version is actually installed.
  • :vendor: The directory to the vendor Rails framework to use. This is usually something like "/webapps/foo/vendor/rails".

It is not allowed to specify both version and vendor.

All other options will be passed on to ApplicationSpawner and RequestHandler.

Note that the specified Rails framework will be loaded during the entire life time of the FrameworkSpawner server. If you wish to reload the Rails framework‘s code, then restart the server by calling AbstractServer#stop and AbstractServer#start.

    # File lib/phusion_passenger/railz/framework_spawner.rb, line 64
64:         def initialize(options = {})
65:                 if !options.respond_to?('[]''[]')
66:                         raise ArgumentError, "The 'options' argument not seem to be an options hash"
67:                 end
68:                 @version = options[:version]
69:                 @vendor  = options[:vendor]
70:                 if !@version && !@vendor
71:                         raise ArgumentError, "Either the 'version' or the 'vendor' option must specified"
72:                 elsif @version && @vendor
73:                         raise ArgumentError, "It is not allowed to specify both the 'version' and the 'vendor' options"
74:                 end
75:                 
76:                 super()
77:                 self.max_idle_time = DEFAULT_FRAMEWORK_SPAWNER_MAX_IDLE_TIME
78:                 define_message_handler(:spawn_application, :handle_spawn_application)
79:                 define_message_handler(:reload, :handle_reload)
80:         end
Public Instance methods
reload(app_root = nil)

Remove the cached application instances at the given application root. If nil is specified as application root, then all cached application instances will be removed, no matter the application root.

Long description: Application code might be cached in memory by a FrameworkSpawner. But once it a while, it will be necessary to reload the code for an application, such as after deploying a new version of the application. This method makes sure that any cached application code is removed, so that the next time an application instance is spawned, the application code will be freshly loaded into memory.

Raises:

     # File lib/phusion_passenger/railz/framework_spawner.rb, line 199
199:         def reload(app_root = nil)
200:                 if app_root.nil?
201:                         server.write("reload")
202:                 else
203:                         server.write("reload", normalize_path(app_root))
204:                 end
205:         rescue SystemCallError, IOError, SocketError
206:                 raise Error, "The framework spawner server exited unexpectedly"
207:         end
spawn_application(app_root, options = {})

Spawn a RoR application using the Ruby on Rails framework version associated with this FrameworkSpawner. When successful, an Application object will be returned, which represents the spawned RoR application.

The following options are allowed:

  • lower_privilege and lowest_user: If lower_privilege is true, then ApplicationSpawner will attempt to switch to the user who owns the application‘s config/environment.rb, and to the default group of that user.

    If that user doesn‘t exist on the system, or if that user is root, then ApplicationSpawner will attempt to switch to the username given by lowest_user (and to the default group of that user). If lowest_user doesn‘t exist either, or if switching user failed (because the current process does not have the privilege to do so), then ApplicationSpawner will continue without reporting an error.

  • environment: Allows one to specify the RAILS_ENV environment to use.

All other options will be passed on to ApplicationSpawner and RequestHandler.

FrameworkSpawner will internally cache the code of applications, in order to speed up future spawning attempts. This implies that, if you‘ve changed the application‘s code, you must do one of these things:

Raises:

     # File lib/phusion_passenger/railz/framework_spawner.rb, line 150
150:         def spawn_application(app_root, options = {})
151:                 app_root = normalize_path(app_root)
152:                 assert_valid_app_root(app_root)
153:                 options = sanitize_spawn_options(options)
154:                 options["app_root"] = app_root
155:                 
156:                 exception_to_propagate = nil
157:                 begin
158:                         server.write("spawn_application", *options.to_a.flatten)
159:                         result = server.read
160:                         if result.nil?
161:                                 raise IOError, "Connection closed"
162:                         end
163:                         if result[0] == 'exception'
164:                                 e = unmarshal_exception(server.read_scalar)
165:                                 if e.respond_to?(:child_exception) && e.child_exception
166:                                         #print_exception(self.class.to_s, e.child_exception)
167:                                 end
168:                                 raise e
169:                         else
170:                                 pid, listen_socket_name, socket_type = server.read
171:                                 if pid.nil?
172:                                         raise IOError, "Connection closed"
173:                                 end
174:                                 owner_pipe = server.recv_io
175:                                 return Application.new(app_root, pid, listen_socket_name,
176:                                         socket_type, owner_pipe)
177:                         end
178:                 rescue SystemCallError, IOError, SocketError => e
179:                         raise Error, "The framework spawner server exited unexpectedly"
180:                 end
181:         end
start()

Overrided from AbstractServer#start.

May raise these additional exceptions:

     # File lib/phusion_passenger/railz/framework_spawner.rb, line 87
 87:         def start
 88:                 super
 89:                 begin
 90:                         result = server.read
 91:                         if result.nil?
 92:                                 raise Error, "The framework spawner server exited unexpectedly."
 93:                         else
 94:                                 status = result[0]
 95:                         end
 96:                         if status == 'exception'
 97:                                 child_exception = unmarshal_exception(server.read_scalar)
 98:                                 stop
 99:                                 if @version
100:                                         message = "Could not load Ruby on Rails framework version #{@version}: " <<
101:                                                 "#{child_exception.class} (#{child_exception.message})"
102:                                 else
103:                                         message = "Could not load Ruby on Rails framework at '#{@vendor}': " <<
104:                                                 "#{child_exception.class} (#{child_exception.message})"
105:                                 end
106:                                 options = { :vendor => @vendor, :version => @version }
107:                                 raise FrameworkInitError.new(message, child_exception, options)
108:                         end
109:                 rescue IOError, SystemCallError, SocketError
110:                         stop
111:                         raise Error, "The framework spawner server exited unexpectedly"
112:                 end
113:         end