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.
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.
[ show source ]
# File lib/phusion_passenger/railz/framework_spawner.rb, line 69 69: def initialize(options = {}) 70: if !options.respond_to?('[]''[]') 71: raise ArgumentError, "The 'options' argument not seem to be an options hash" 72: end 73: @version = options[:version] 74: @vendor = options[:vendor] 75: if !@version && !@vendor 76: raise ArgumentError, "Either the 'version' or the 'vendor' option must specified" 77: elsif @version && @vendor 78: raise ArgumentError, "It is not allowed to specify both the 'version' and the 'vendor' options" 79: end 80: 81: super() 82: self.max_idle_time = DEFAULT_FRAMEWORK_SPAWNER_MAX_IDLE_TIME 83: define_message_handler(:spawn_application, :handle_spawn_application) 84: define_message_handler(:reload, :handle_reload) 85: end
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:
- ArgumentError: app_root doesn‘t appear to be a valid Ruby on Rails application root.
- FrameworkSpawner::Error: The FrameworkSpawner server exited unexpectedly.
[ show source ]
# File lib/phusion_passenger/railz/framework_spawner.rb, line 203 203: def reload(app_root = nil) 204: if app_root.nil? 205: server.write("reload") 206: else 207: server.write("reload", app_root) 208: end 209: rescue SystemCallError, IOError, SocketError 210: raise Error, "The framework spawner server exited unexpectedly" 211: end
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:
- Restart this FrameworkSpawner by calling AbstractServer#stop, then AbstractServer#start.
- Reload the application by calling reload with the correct app_root argument.
Raises:
- AbstractServer::ServerNotStarted: The FrameworkSpawner server hasn‘t already been started.
- InvalidAppRoot: app_root doesn‘t appear to be a valid Ruby on Rails application root.
- AppInitError: The application raised an exception or called exit() during startup.
- ApplicationSpawner::Error: The ApplicationSpawner server exited unexpectedly.
- FrameworkSpawner::Error: The FrameworkSpawner server exited unexpectedly.
[ show source ]
# File lib/phusion_passenger/railz/framework_spawner.rb, line 155 155: def spawn_application(app_root, options = {}) 156: assert_valid_app_root(app_root) 157: options = sanitize_spawn_options(options) 158: options["app_root"] = app_root 159: 160: exception_to_propagate = nil 161: begin 162: server.write("spawn_application", *options.to_a.flatten) 163: result = server.read 164: if result.nil? 165: raise IOError, "Connection closed" 166: end 167: if result[0] == 'exception' 168: e = unmarshal_exception(server.read_scalar) 169: if e.respond_to?(:child_exception) && e.child_exception 170: #print_exception(self.class.to_s, e.child_exception) 171: end 172: raise e 173: else 174: pid, listen_socket_name, socket_type = server.read 175: if pid.nil? 176: raise IOError, "Connection closed" 177: end 178: owner_pipe = server.recv_io 179: return Application.new(app_root, pid, listen_socket_name, 180: socket_type, owner_pipe) 181: end 182: rescue SystemCallError, IOError, SocketError => e 183: raise Error, "The framework spawner server exited unexpectedly" 184: end 185: end
Overrided from AbstractServer#start.
May raise these additional exceptions:
- FrameworkInitError: The specified Ruby on Rails framework could not be loaded.
- FrameworkSpawner::Error: The FrameworkSpawner server exited unexpectedly.
[ show source ]
# File lib/phusion_passenger/railz/framework_spawner.rb, line 92 92: def start 93: super 94: begin 95: result = server.read 96: if result.nil? 97: raise Error, "The framework spawner server exited unexpectedly." 98: else 99: status = result[0] 100: end 101: if status == 'exception' 102: child_exception = unmarshal_exception(server.read_scalar) 103: stop 104: if @version 105: message = "Could not load Ruby on Rails framework version #{@version}: " << 106: "#{child_exception.class} (#{child_exception.message})" 107: else 108: message = "Could not load Ruby on Rails framework at '#{@vendor}': " << 109: "#{child_exception.class} (#{child_exception.message})" 110: end 111: options = { :vendor => @vendor, :version => @version } 112: raise FrameworkInitError.new(message, child_exception, options) 113: end 114: rescue IOError, SystemCallError, SocketError 115: stop 116: raise Error, "The framework spawner server exited unexpectedly" 117: end 118: end