mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Add options to daemonize puma
Previously I was opposed to these options because I felt like puma should always be run under another monitoring process. But after doing some thinking (and some deployment of puma) I realized that the daemon option is simply too useful to not include. I highly suggest it be used with -w, so that if a worker crashes the whole server doesn't mysteriously disappear.
This commit is contained in:
parent
05af17d3f7
commit
4d77ce8347
3 changed files with 71 additions and 4 deletions
|
@ -6,6 +6,7 @@ require 'puma/const'
|
||||||
require 'puma/configuration'
|
require 'puma/configuration'
|
||||||
require 'puma/binder'
|
require 'puma/binder'
|
||||||
require 'puma/detect'
|
require 'puma/detect'
|
||||||
|
require 'puma/daemon_ext'
|
||||||
|
|
||||||
require 'rack/commonlogger'
|
require 'rack/commonlogger'
|
||||||
require 'rack/utils'
|
require 'rack/utils'
|
||||||
|
@ -159,7 +160,8 @@ module Puma
|
||||||
:quiet => false,
|
:quiet => false,
|
||||||
:debug => false,
|
:debug => false,
|
||||||
:binds => [],
|
:binds => [],
|
||||||
:workers => 0
|
:workers => 0,
|
||||||
|
:daemon => false
|
||||||
}
|
}
|
||||||
|
|
||||||
@parser = OptionParser.new do |o|
|
@parser = OptionParser.new do |o|
|
||||||
|
@ -171,6 +173,15 @@ module Puma
|
||||||
@options[:config_file] = arg
|
@options[:config_file] = arg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
o.on "-d", "--daemon", "Daemonize the server into the background" do
|
||||||
|
@options[:daemon] = true
|
||||||
|
@options[:quiet] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
o.on "--dir DIR", "Change to DIR before starting" do |d|
|
||||||
|
@options[:directory] = d.to_s
|
||||||
|
end
|
||||||
|
|
||||||
o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
|
o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
|
||||||
$LOAD_PATH.unshift(*arg.split(':'))
|
$LOAD_PATH.unshift(*arg.split(':'))
|
||||||
end
|
end
|
||||||
|
@ -321,6 +332,10 @@ module Puma
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if dir = @options[:directory]
|
||||||
|
Dir.chdir dir
|
||||||
|
end
|
||||||
|
|
||||||
clustered = @options[:workers] > 0
|
clustered = @options[:workers] > 0
|
||||||
|
|
||||||
if clustered
|
if clustered
|
||||||
|
@ -407,7 +422,11 @@ module Puma
|
||||||
log "*** Sorry signal SIGTERM not implemented, gracefully stopping feature disabled!"
|
log "*** Sorry signal SIGTERM not implemented, gracefully stopping feature disabled!"
|
||||||
end
|
end
|
||||||
|
|
||||||
log "Use Ctrl-C to stop"
|
if @options[:daemon]
|
||||||
|
Process.daemon(true)
|
||||||
|
else
|
||||||
|
log "Use Ctrl-C to stop"
|
||||||
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
server.run.join
|
server.run.join
|
||||||
|
@ -538,9 +557,15 @@ module Puma
|
||||||
#
|
#
|
||||||
@check_pipe, @suicide_pipe = IO.pipe
|
@check_pipe, @suicide_pipe = IO.pipe
|
||||||
|
|
||||||
spawn_workers
|
if @options[:daemon]
|
||||||
|
Process.daemon(true)
|
||||||
|
STDOUT.reopen "/tmp/puma.out", "a"
|
||||||
|
STDOUT.reopen "/tmp/puma.err", "a"
|
||||||
|
else
|
||||||
|
log "Use Ctrl-C to stop"
|
||||||
|
end
|
||||||
|
|
||||||
log "* Use Ctrl-C to stop"
|
spawn_workers
|
||||||
|
|
||||||
begin
|
begin
|
||||||
while !stop
|
while !stop
|
||||||
|
|
|
@ -156,6 +156,12 @@ module Puma
|
||||||
@options[:binds] << url
|
@options[:binds] << url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Daemonize the server into the background. Highly suggest that
|
||||||
|
# this be combined with +pidfile+ and +stdout_redirect+.
|
||||||
|
def daemonize(which=true)
|
||||||
|
@options[:daemon] = which
|
||||||
|
end
|
||||||
|
|
||||||
# Set the environment in which the Rack's app will run.
|
# Set the environment in which the Rack's app will run.
|
||||||
def environment(environment)
|
def environment(environment)
|
||||||
@options[:environment] = environment
|
@options[:environment] = environment
|
||||||
|
@ -187,6 +193,17 @@ module Puma
|
||||||
@options[:rackup] = path.to_s
|
@options[:rackup] = path.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Redirect STDOUT and STDERR to files specified.
|
||||||
|
def stdout_redirect(stdout=nil, stderr=nil, append=false)
|
||||||
|
if stdout
|
||||||
|
STDOUT.reopen stdout, (append ? "a" : "w")
|
||||||
|
end
|
||||||
|
|
||||||
|
if stderr
|
||||||
|
STDOUT.reopen stderr, (append ? "a" : "w")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Configure +min+ to be the minimum number of threads to use to answer
|
# Configure +min+ to be the minimum number of threads to use to answer
|
||||||
# requests and +max+ the maximum.
|
# requests and +max+ the maximum.
|
||||||
#
|
#
|
||||||
|
@ -220,6 +237,11 @@ module Puma
|
||||||
def workers(count)
|
def workers(count)
|
||||||
@options[:workers] = count.to_i
|
@options[:workers] = count.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The directory to operate out of.
|
||||||
|
def directory(dir)
|
||||||
|
@options[:directory] = dir.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
20
lib/puma/daemon_ext.rb
Normal file
20
lib/puma/daemon_ext.rb
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
module Process
|
||||||
|
def self.daemon(nochdir=false, noclose=false)
|
||||||
|
exit if fork # Parent exits, child continues.
|
||||||
|
|
||||||
|
Process.setsid # Become session leader.
|
||||||
|
|
||||||
|
exit if fork # Zap session leader. See [1].
|
||||||
|
|
||||||
|
Dir.chdir "/" unless nochdir # Release old working directory.
|
||||||
|
|
||||||
|
if !noclose
|
||||||
|
null = File.open "/dev/null"
|
||||||
|
STDIN.reopen null
|
||||||
|
STDOUT.reopen null
|
||||||
|
STDERR.reopen null
|
||||||
|
end
|
||||||
|
|
||||||
|
0
|
||||||
|
end unless respond_to?(:daemon)
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue