2013-07-05 19:08:13 -04:00
|
|
|
require 'puma/runner'
|
2016-09-05 14:29:16 -04:00
|
|
|
require 'puma/detect'
|
|
|
|
require 'puma/plugin'
|
2013-07-05 19:08:13 -04:00
|
|
|
|
|
|
|
module Puma
|
2018-05-01 16:44:08 -04:00
|
|
|
# This class is instantiated by the `Puma::Launcher` and used
|
|
|
|
# to boot and serve a Ruby application when no puma "workers" are needed
|
|
|
|
# i.e. only using "threaded" mode. For example `$ puma -t 1:5`
|
|
|
|
#
|
|
|
|
# At the core of this class is running an instance of `Puma::Server` which
|
|
|
|
# gets created via the `start_server` method from the `Puma::Runner` class
|
|
|
|
# that this inherits from.
|
2013-07-05 19:08:13 -04:00
|
|
|
class Single < Runner
|
|
|
|
def stats
|
2017-12-11 16:05:36 -05:00
|
|
|
b = @server.backlog || 0
|
|
|
|
r = @server.running || 0
|
2013-07-05 19:08:13 -04:00
|
|
|
%Q!{ "backlog": #{b}, "running": #{r} }!
|
|
|
|
end
|
|
|
|
|
|
|
|
def restart
|
|
|
|
@server.begin_restart
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
@server.stop false
|
|
|
|
end
|
|
|
|
|
|
|
|
def halt
|
|
|
|
@server.halt
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_blocked
|
|
|
|
log "- Gracefully stopping, waiting for requests to finish"
|
2013-07-09 01:36:43 -04:00
|
|
|
@control.stop(true) if @control
|
2013-07-05 19:08:13 -04:00
|
|
|
@server.stop(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def jruby_daemon?
|
2016-02-06 22:00:29 -05:00
|
|
|
daemon? and Puma.jruby?
|
|
|
|
end
|
|
|
|
|
|
|
|
def jruby_daemon_start
|
|
|
|
require 'puma/jruby_restart'
|
2016-03-05 19:28:39 -05:00
|
|
|
JRubyRestart.daemon_start(@restart_dir, @launcher.restart_args)
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
already_daemon = false
|
|
|
|
|
|
|
|
if jruby_daemon?
|
|
|
|
require 'puma/jruby_restart'
|
|
|
|
|
|
|
|
if JRubyRestart.daemon?
|
|
|
|
# load and bind before redirecting IO so errors show up on stdout/stderr
|
|
|
|
load_and_bind
|
2016-07-24 16:56:02 -04:00
|
|
|
redirect_io
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
already_daemon = JRubyRestart.daemon_init
|
|
|
|
end
|
|
|
|
|
2013-07-05 20:09:18 -04:00
|
|
|
output_header "single"
|
2013-07-05 19:08:13 -04:00
|
|
|
|
|
|
|
if jruby_daemon?
|
2013-08-13 19:05:51 -04:00
|
|
|
if already_daemon
|
|
|
|
JRubyRestart.perm_daemonize
|
|
|
|
else
|
2013-07-05 19:08:13 -04:00
|
|
|
pid = nil
|
|
|
|
|
|
|
|
Signal.trap "SIGUSR2" do
|
|
|
|
log "* Started new process #{pid} as daemon..."
|
2013-08-08 18:48:50 -04:00
|
|
|
|
|
|
|
# Must use exit! so we don't unwind and run the ensures
|
|
|
|
# that will be run by the new child (such as deleting the
|
|
|
|
# pidfile)
|
2013-08-21 01:25:25 -04:00
|
|
|
exit!(true)
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
2013-07-11 14:46:18 -04:00
|
|
|
Signal.trap "SIGCHLD" do
|
2015-10-17 13:31:55 -04:00
|
|
|
log "! Error starting new process as daemon, exiting"
|
2013-07-11 14:46:18 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2016-04-07 14:08:53 -04:00
|
|
|
jruby_daemon_start
|
2013-07-05 19:08:13 -04:00
|
|
|
sleep
|
|
|
|
end
|
|
|
|
else
|
2013-07-05 20:09:18 -04:00
|
|
|
if daemon?
|
|
|
|
log "* Daemonizing..."
|
|
|
|
Process.daemon(true)
|
2014-12-12 20:52:15 -05:00
|
|
|
redirect_io
|
2013-07-05 20:09:18 -04:00
|
|
|
end
|
2014-11-24 14:46:32 -05:00
|
|
|
|
|
|
|
load_and_bind
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
2016-07-24 19:00:57 -04:00
|
|
|
Plugins.fire_background
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@launcher.write_state
|
2013-07-05 19:08:13 -04:00
|
|
|
|
2013-07-09 01:36:43 -04:00
|
|
|
start_control
|
|
|
|
|
2013-07-06 00:13:29 -04:00
|
|
|
@server = server = start_server
|
2013-07-05 19:08:13 -04:00
|
|
|
|
2014-12-12 20:52:15 -05:00
|
|
|
unless daemon?
|
2013-07-05 19:08:13 -04:00
|
|
|
log "Use Ctrl-C to stop"
|
2014-12-12 20:52:15 -05:00
|
|
|
redirect_io
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 22:00:29 -05:00
|
|
|
@launcher.events.fire_on_booted!
|
2013-07-05 19:54:15 -04:00
|
|
|
|
2013-07-05 19:08:13 -04:00
|
|
|
begin
|
|
|
|
server.run.join
|
|
|
|
rescue Interrupt
|
|
|
|
# Swallow it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|