Class: Merb::Rack::AbstractAdapter
- Defined in:
- lib/merb-core/rack/adapter/abstract.rb
Class Method Summary collapse
-
.exit_process(status = 0) ⇒ Object
Exit the process with the specified status.
-
.new_server(port) ⇒ Object
This method is designed to be overridden in a rack adapter.
-
.process_title(whoami, port) ⇒ Object
Set the process title.
-
.spawn_worker(port) ⇒ Object
Spawn a new worker process at a port.
-
.start(opts = {}) ⇒ Object
The main start method for bootloaders that support forking.
-
.start_at_port(port, opts = @opts) ⇒ Object
Fork a server on the specified port and start the app.
-
.start_server ⇒ Object
This method is designed to be overridden in a rack adapter.
-
.stop(status) ⇒ Object
This method is designed to be overridden in a rack adapter.
Class Method Details
.exit_process(status = 0) ⇒ Object
Exit the process with the specified status.
Parameters
- status<Integer>
-
The exit code of the process.
:api: private
314 315 316 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 314 def self.exit_process(status = 0) exit(status) end |
.new_server(port) ⇒ Object
This method is designed to be overridden in a rack adapter. It will be called to create a new instance of the server for the adapter to start. The adapter should attempt to bind to a port at this point. This is called from the AbstractAdapter start method.
Parameters
- port<Integer>
-
The port the server should listen on
:api: plugin
68 69 70 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 68 def self.new_server(port) raise NotImplemented end |
.process_title(whoami, port) ⇒ Object
Set the process title.
Parameters
- whoami<Symbol>
-
Either :spawner for the master process or :worker for any of the worker
processes.
- port<Integer>
-
The base port that the app is running on.
:api: private
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 326 def self.process_title(whoami, port) name = Merb::Config[:name] app = "merb#{" : #{name}" if (name && name != "merb")}" max_port = Merb::Config[:cluster] ? (Merb::Config[:cluster] - 1) : 0 numbers = ((whoami != :worker) && (max_port > 0)) ? "#{port}..#{port + max_port}" : port file = Merb::Config[:socket_file] % port if Merb::Config[:socket_file] listening_on = if Merb::Config[:socket] "socket#{'s' if max_port > 0 && whoami != :worker} #{numbers} "\ "#{file ? file : "#{Merb.log_path}/#{name}.#{port}.sock"}" else "port#{'s' if max_port > 0 && whoami != :worker} #{port}" end "#{app} : #{whoami} (#{listening_on})" end |
.spawn_worker(port) ⇒ Object
Spawn a new worker process at a port.
Parameters
- port<Integer>
-
The port to start the worker process on.
:api: private
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 93 def self.spawn_worker(port) worker_pid = Kernel.fork # If we have a worker_pid, we're in the parent. if worker_pid.nil? # Seed the random number generator Kernel.srand # Restart the run_later worker, unless we're in the parent or it's alive. Merb::Worker.restart unless Merb::Worker.alive? # Spawn the worker start_at_port(port, @opts) throw(:new_worker) end @pids[port] = worker_pid $WORKERS = @pids.values end |
.start(opts = {}) ⇒ Object
The main start method for bootloaders that support forking. This method launches the adapters which inherit using the new_server and start_server methods. This method should not be overridden in adapters which want to fork.
Parameters
- opts<Hash>
-
A hash of options
socket: the socket to bind to
port: the port to bind to
cluster: the number
:api: private
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 126 def self.start(opts={}) @opts = opts $WORKERS ||= [] parent = nil @pids = {} port = (opts[:socket] || opts[:port]).to_i max_port = Merb::Config[:cluster] ? Merb::Config[:cluster] - 1 : 0 # If we only have a single merb, just start it up and dispense with # the spawner/worker setup. if max_port == 0 start_at_port(port) return end $0 = process_title(:spawner, port) # For each port, spawn a new worker. The parent will continue in # the loop, while the worker will throw :new_worker and be booted # out of the loop. catch(:new_worker) do 0.upto(max_port) do |i| parent = spawn_worker(port + i) end end # If we're in a worker, we're done. Otherwise, we've completed # setting up workers and now need to watch them. return unless parent # For each worker, set up a thread in the spawner to watch it 0.upto(max_port) do |i| Thread.new do catch(:new_worker) do loop do pid, status = @pids[port + i], nil poller = Merb::System::PortablePoller.new(pid) begin i = 0 loop do # Watch for the pid to exit. _, status = Process.wait2(pid, Process::WNOHANG) break if status if (i % 120 == 0) && Merb::Config[:max_memory] && poller.memory > Merb::Config[:max_memory] Process.kill("INT", pid) if (Process.kill(0, pid) rescue false) sleep Merb::Config[:hang_time] || 5 Process.kill(9, pid) Process.wait2(pid) if (Process.kill(0, pid) rescue false) end status = Struct.new(:exitstatus).new(nil) break end i += 1 sleep 0.25 end # If the pid doesn't exist, we want to silently exit instead of # raising here. rescue SystemCallError => e ensure # If there was no worker with that PID, the status was non-0 # (we send back a status of 128 when ABRT is called on a # worker, and Merb.fatal! exits with a status of 1), or if # Merb is in the process of exiting, *then* don't respawn. # Note that processes killed with kill -9 will return no # exitstatus, and we respawn them. if !status || (status.exitstatus && status.exitstatus != 0) || Merb.exiting then Thread.exit end end # Otherwise, respawn the worker, and watch it again. spawn_worker(port + i) end end end end # The spawner process will make it here, and when it does, it should just # sleep so it can pick up ctrl-c if it's in console mode. sleep end |
.start_at_port(port, opts = @opts) ⇒ Object
Fork a server on the specified port and start the app.
Parameters
- port<Integer>
-
The port to start the server on
- opts<Hash>
-
The hash of options, defaults to the @opts
instance variable.
:api: private
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 224 def self.start_at_port(port, opts = @opts) at_exit do Merb::Server.remove_pid(port) end # If Merb is daemonized, trap INT. If it's not daemonized, # we let the master process' ctrl-c control the cluster # of workers. if Merb::Config[:daemonize] Merb.trap('INT') do Merb.exiting = true stop Merb.logger.warn! "Exiting port #{port}\n" exit_process end # If it was not fork_for_class_load, we already set up # ctrl-c handlers in the master thread. elsif Merb::Config[:fork_for_class_load] if Merb::Config[:console_trap] Merb::Server.add_irb_trap end end # In daemonized mode or not, support HUPing the process to # restart it. Merb.trap('HUP') do Merb.exiting = true stop Merb.logger.warn! "Exiting port #{port} on #{Process.pid}\n" exit_process end # ABRTing the process will kill it, and it will not be respawned. Merb.trap('ABRT') do Merb.exiting = true stopped = stop(128) Merb.logger.warn! "Exiting port #{port}\n" if stopped exit_process(128) end # Each worker gets its own `ps' name. $0 = process_title(:worker, port) # Store the PID for this worker Merb::Server.store_pid(port) Merb::Config[:log_delimiter] = "#{process_title(:worker, port)} ~ " Merb.reset_logger! Merb.logger.warn!("Starting #{self.name.split("::").last} at port #{port}") # If we can't connect to the port, keep trying until we can. Print # a warning about this once. Try every 0.25s. printed_warning = false loop do begin # Call the adapter's new_server method, which should attempt # to bind to a port. new_server(port) rescue Errno::EADDRINUSE => e if Merb::Config[:bind_fail_fatal] Merb.fatal! "Could not bind to #{port}. It was already in use", e end unless printed_warning Merb.logger.warn! "Port #{port} is in use, " \ "Waiting for it to become available." printed_warning = true end sleep 0.25 next end break end Merb.logger.warn! "Successfully bound to port #{port}" Merb::Server.change_privilege # Call the adapter's start_server method. start_server end |
.start_server ⇒ Object
This method is designed to be overridden in a rack adapter. It will be called to start a server created with the new_server method. This is called from the AbstractAdapter start method.
:api: plugin
54 55 56 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 54 def self.start_server raise NotImplemented end |
.stop(status) ⇒ Object
This method is designed to be overridden in a rack adapter. It will be called to stop the adapter server.
Parameters
- status<Integer>
-
The exit status the adapter should exit with.
Returns
- Boolean
-
True if the server was properly stopped.
:api: plugin
83 84 85 |
# File 'lib/merb-core/rack/adapter/abstract.rb', line 83 def self.stop(status) raise NotImplemented end |