The spawn manager is capable of spawning Ruby on Rails or Rack application instances. It acts like a simple fascade for the rest of the spawn manager system.
Note: SpawnManager may only be started synchronously with AbstractServer#start_synchronously. Starting asynchronously has not been tested. Don‘t forget to call cleanup after the server‘s main loop has finished.
Ruby on Rails optimizations ===
Spawning a Ruby on Rails application is usually slow. But SpawnManager will preload and cache Ruby on Rails frameworks, as well as application code, so subsequent spawns will be very fast.
Internally, SpawnManager uses Railz::FrameworkSpawner to preload and cache Ruby on Rails frameworks. Railz::FrameworkSpawner, in turn, uses Railz::ApplicationSpawner to preload and cache application code.
In case you‘re wondering why the namespace is "Railz" and not "Rails": it‘s to work around an obscure bug in ActiveSupport‘s Dispatcher.
[ show source ]
# File lib/passenger/spawn_manager.rb, line 48 48: def initialize 49: super() 50: @spawners = {} 51: @lock = Mutex.new 52: @cond = ConditionVariable.new 53: @cleaner_thread = Thread.new do 54: cleaner_thread_main 55: end 56: define_message_handler(:spawn_application, :handle_spawn_application) 57: define_message_handler(:reload, :handle_reload) 58: define_signal_handler('SIGHUP', :reload) 59: 60: GC.start 61: if GC.copy_on_write_friendly? 62: # Preload libraries for copy-on-write semantics. 63: require 'base64' 64: require 'passenger/application' 65: require 'passenger/railz/framework_spawner' 66: require 'passenger/railz/application_spawner' 67: require 'passenger/rack/application_spawner' 68: require 'passenger/html_template' 69: require 'passenger/platform_info' 70: require 'passenger/exceptions' 71: 72: # Commonly used libraries. 73: ['mysql', 'sqlite3'].each do |lib| 74: require lib 75: end 76: end 77: end
Cleanup resources. Should be called when this SpawnManager is no longer needed.
[ show source ]
# File lib/passenger/spawn_manager.rb, line 169 169: def cleanup 170: @lock.synchronize do 171: @cond.signal 172: end 173: @cleaner_thread.join 174: @lock.synchronize do 175: @spawners.each_value do |spawner| 176: if spawner.started? 177: spawner.stop 178: end 179: end 180: @spawners.clear 181: end 182: 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. 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 AbstractServer::SpawnError if something went wrong.
[ show source ]
# File lib/passenger/spawn_manager.rb, line 140 140: def reload(app_root = nil) 141: if app_root 142: begin 143: app_root = normalize_path(app_root) 144: rescue ArgumentError 145: end 146: end 147: @lock.synchronize do 148: if app_root 149: # Delete associated ApplicationSpawner. 150: key = "app:#{app_root}" 151: spawner = @spawners[key] 152: if spawner 153: if spawner.started? 154: spawner.stop 155: end 156: @spawners.delete(key) 157: end 158: end 159: @spawners.each_value do |spawner| 160: # Reload FrameworkSpawners. 161: if spawner.respond_to?(:reload) 162: spawner.reload(app_root) 163: end 164: end 165: end 166: end
Spawn a RoR application When successful, an Application object will be returned, which represents the spawned RoR application.
See Railz::ApplicationSpawner.new for an explanation of the lower_privilege, lowest_user and environment parameters.
The spawn_method argument may be one of "smart" or "conservative". When "smart" is specified (the default), SpawnManager 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 SpawnManager by calling AbstractServer#stop, then AbstractServer#start.
- Reload the application by calling reload with the correct app_root argument.
Caching however can be incompatible with some applications.
The "conservative" spawning method does not involve any caching at all. Spawning will be slower, but is guaranteed to be compatible with all applications.
Raises:
- ArgumentError: app_root doesn‘t appear to be a valid Ruby on Rails application root.
- VersionNotFound: The Ruby on Rails framework version that the given application requires is not installed.
- AbstractServer::ServerError: One of the server processes exited unexpectedly.
- FrameworkInitError: The Ruby on Rails framework that the application requires could not be loaded.
- AppInitError: The application raised an exception or called exit() during startup.
[ show source ]
# File lib/passenger/spawn_manager.rb, line 103 103: def spawn_application(app_root, lower_privilege = true, lowest_user = "nobody", 104: environment = "production", spawn_method = "smart", 105: app_type = "rails") 106: if app_type == "rack" 107: if !defined?(Rack::ApplicationSpawner) 108: require 'passenger/rack/application_spawner' 109: end 110: return Rack::ApplicationSpawner.spawn_application(app_root, 111: lower_privilege, lowest_user, environment) 112: elsif app_type == "wsgi" 113: require 'passenger/wsgi/application_spawner' 114: return WSGI::ApplicationSpawner.spawn_application(app_root, 115: lower_privilege, lowest_user, environment) 116: else 117: if !defined?(Railz::FrameworkSpawner) 118: require 'passenger/application' 119: require 'passenger/railz/framework_spawner' 120: require 'passenger/railz/application_spawner' 121: end 122: return spawn_rails_application(app_root, lower_privilege, lowest_user, 123: environment, spawn_method) 124: end 125: end