Spawning of Rails 1 and Rails 2 applications.
ClassicRails::ApplicationSpawner can operate in two modes:
- Smart mode. In this mode, the Rails application‘s code is first preloaded into a temporary process, which can then further fork off application processes. Once the code has been preloaded, forking off application processes is very fast, and all the forked off application processes can share code memory with each other. To use this mode, create an ApplicationSpawner object, start it, and call #spawn_application on it. A single ApplicationSpawner object can only handle a single Rails application.
- Conservative mode. In this mode, a Rails app process is directly spawned without any preloading. This increases compatibility with applications. To use this mode, call ApplicationSpawner.spawn_application.
| [R] | app_root | The application root of this spawner. |
The following options are accepted:
- ‘app_root‘
See SpawnManager#spawn_application for information about the options.
[ show source ]
# File lib/phusion_passenger/classic_rails/application_spawner.rb, line 116
116: def initialize(options)
117: super()
118: @options = sanitize_spawn_options(options)
119: @app_root = @options["app_root"]
120: @canonicalized_app_root = canonicalize_path(@app_root)
121: self.max_idle_time = DEFAULT_APP_SPAWNER_MAX_IDLE_TIME
122: define_message_handler(:spawn_application, :handle_spawn_application)
123: end
Spawns an instance of a Rails application. When successful, an AppProcess object will be returned, which represents the spawned Rails application.
This method spawns the application directly, without preloading its code. This method may only be called if no Rails framework has been loaded in the current Ruby VM.
The "app_root" option must be given. All other options are passed to the request handler‘s constructor.
Raises:
- AppInitError: The Ruby on Rails application raised an exception or called exit() during startup.
- SystemCallError, IOError, SocketError: Something went wrong.
[ show source ]
# File lib/phusion_passenger/classic_rails/application_spawner.rb, line 80
80: def self.spawn_application(options)
81: options = sanitize_spawn_options(options)
82:
83: a, b = UNIXSocket.pair
84: pid = safe_fork('application', true) do
85: a.close
86:
87: file_descriptors_to_leave_open = [0, 1, 2, b.fileno]
88: NativeSupport.close_all_file_descriptors(file_descriptors_to_leave_open)
89: close_all_io_objects_for_fds(file_descriptors_to_leave_open)
90:
91: channel = MessageChannel.new(b)
92: success = report_app_init_status(channel) do
93: prepare_app_process('config/environment.rb', options)
94: require File.expand_path('config/environment')
95: require 'dispatcher'
96: after_loading_app_code(options)
97: end
98: if success
99: start_request_handler(channel, false, options)
100: end
101: end
102: b.close
103: Process.waitpid(pid) rescue nil
104:
105: channel = MessageChannel.new(a)
106: unmarshal_and_raise_errors(channel, options["print_exceptions"])
107:
108: # No exception was raised, so spawning succeeded.
109: return AppProcess.read_from_channel(channel)
110: end
Spawns an instance of the Rails application. When successful, an AppProcess object will be returned, which represents the spawned Rails application.
options will be passed to the request handler‘s constructor.
Raises:
- AbstractServer::ServerNotStarted: The ApplicationSpawner server hasn‘t already been started.
- ApplicationSpawner::Error: The ApplicationSpawner server exited unexpectedly.
[ show source ]
# File lib/phusion_passenger/classic_rails/application_spawner.rb, line 133
133: def spawn_application(options = {})
134: connect do |channel|
135: channel.write("spawn_application", *options.to_a.flatten)
136: return AppProcess.read_from_channel(channel)
137: end
138: rescue SystemCallError, IOError, SocketError => e
139: raise Error, "The application spawner server exited unexpectedly: #{e}"
140: end
Overrided from AbstractServer#start.
May raise these additional exceptions:
- AppInitError: The Ruby on Rails application raised an exception or called exit() during startup.
- ApplicationSpawner::Error: The ApplicationSpawner server exited unexpectedly.
[ show source ]
# File lib/phusion_passenger/classic_rails/application_spawner.rb, line 148
148: def start
149: super
150: begin
151: channel = MessageChannel.new(@owner_socket)
152: unmarshal_and_raise_errors(channel, @options["print_exceptions"])
153: rescue IOError, SystemCallError, SocketError => e
154: stop if started?
155: raise Error, "The application spawner server exited unexpectedly: #{e}"
156: rescue
157: stop if started?
158: raise
159: end
160: end