1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Manage nested configuration better

This commit is contained in:
Evan Phoenix 2016-02-07 14:51:54 -08:00
parent 75f016cd20
commit 33e0fa9999
10 changed files with 149 additions and 104 deletions

View file

@ -132,8 +132,7 @@ module Puma
o.on "-p", "--port PORT", "Define the TCP port to bind to",
"Use -b for more advanced options" do |arg|
binds = (@cli_options[:binds] ||= [])
binds << "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
c.bind "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
end
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|

View file

@ -113,12 +113,13 @@ module Puma
diff.times do
idx = next_worker_index
(@options[:before_worker_fork] || []).each { |h| h.call(idx) }
@launcher.config.run_hooks :before_worker_fork, idx
pid = fork { worker(idx, master) }
debug "Spawned worker: #{pid}"
@workers << Worker.new(idx, pid, @phase, @options)
(@options[:after_worker_boot] || []).each { |h| h.call }
@launcher.config.run_hooks :after_worker_fork, idx
end
if diff > 0
@ -221,8 +222,7 @@ module Puma
# Invoke any worker boot hooks so they can get
# things in shape before booting the app.
hooks = @options[:before_worker_boot] || []
hooks.each { |h| h.call(index) }
@launcher.config.run_hooks :before_worker_boot, index
server = start_server
@ -254,8 +254,7 @@ module Puma
# Invoke any worker shutdown hooks so they can prevent the worker
# exiting until any background operations are completed
hooks = @options[:before_worker_shutdown] || []
hooks.each { |h| h.call(index) }
@launcher.config.run_hooks :before_worker_shutdown, index
ensure
@worker_write << "t#{Process.pid}\n" rescue nil
@worker_write.close
@ -399,8 +398,7 @@ module Puma
@master_read, @worker_write = read, @wakeup
hooks = @options[:before_fork] || []
hooks.each { |h| h.call }
@launcher.config.run_hooks :before_fork, nil
spawn_workers

View file

@ -1,5 +1,5 @@
require 'puma/rack/builder'
require 'puma/plugin_loader'
require 'puma/plugin'
module Puma
@ -87,6 +87,18 @@ module Puma
options
end
def explain
indent = ""
@set.each do |o|
o.keys.sort.each do |k|
puts "#{indent}#{k}: #{o[k].inspect}"
end
indent = " #{indent}"
end
end
end
class Configuration
@ -100,7 +112,7 @@ module Puma
return cfg
end
def initialize(options={})
def initialize(options={}, &blk)
@options = LeveledOptions.new
@plugins = PluginLoader.new
@ -108,19 +120,16 @@ module Puma
# @options[k] = v
# end
if block_given?
@options.shift
d = DSL.new(@options, self)
yield d
if blk
configure(&blk)
end
end
attr_reader :options, :plugins
def configure
def configure(&blk)
@options.shift
d = DSL.new(@options, self)
yield d
DSL.new(@options, self)._run(&blk)
end
def initialize_copy(other)
@ -240,6 +249,10 @@ module Puma
@plugins.create name
end
def run_hooks(key, arg)
@options.all_of(key).each { |b| b.call arg }
end
def self.temp_path
require 'tmpdir'

View file

@ -15,7 +15,7 @@ module Puma
cfg.clamp
events = Puma::Events.strings
events = Puma::Events.null
launcher = Puma::Launcher.new cfg, :events => events
launcher.run

View file

@ -5,20 +5,52 @@ module Puma
include ConfigDefault
def self.load(options, cfg, path)
new(options, cfg).tap do |obj|
obj._load_from(path)
end
d = new(options, cfg)
d._load_from(path)
options
ensure
d._offer_plugins
end
def initialize(options, config)
@config = config
@options = options
@plugins = []
end
def _load_from(path)
instance_eval(File.read(path), path, 1) if path
ensure
_offer_plugins
end
def _offer_plugins
@plugins.each do |o|
if o.respond_to? :config
@options.shift
o.config self
end
end
@plugins.clear
end
def _run(&blk)
blk.call self
ensure
_offer_plugins
end
def inject(&blk)
instance_eval(&blk)
end
# Load the named plugin for use by this configuration
#
def plugin(name)
@plugins << @config.load_plugin(name)
end
# Use +obj+ or +block+ as the Rack app. This allows a config file to
@ -57,14 +89,14 @@ module Puma
# Load additional configuration from a file
def load(file)
(@options[:config_files] ||= []) << file
_ary(:config_files) << file
end
# Bind the server to +url+. tcp:// and unix:// are the only accepted
# protocols.
#
def bind(url)
(@options.cur[:binds] ||= []) << url
_ary(:binds) << url
end
# Define the TCP port to bind to. Use +bind+ for more advanced options.
@ -106,7 +138,7 @@ module Puma
# This can be called multiple times to add code each time.
#
def on_restart(&block)
(@options.cur[:on_restart] ||= []) << block
_ary(:on_restart) << block
end
# Command to use to restart puma. This should be just how to
@ -203,7 +235,7 @@ module Puma
# This can be called multiple times to add hooks.
#
def before_fork(&block)
(@options.cur[:before_fork] ||= []) << block
_ary(:before_fork) << block
end
# *Cluster mode only* Code to run immediately before a worker shuts
@ -214,7 +246,7 @@ module Puma
# This can be called multiple times to add hooks.
#
def on_worker_shutdown(&block)
(@options[:before_worker_shutdown] ||= []) << block
_ary(:before_worker_shutdown) << block
end
# *Cluster mode only* Code to run when a worker boots to setup
@ -223,7 +255,7 @@ module Puma
# This can be called multiple times to add hooks.
#
def on_worker_boot(&block)
(@options.cur[:before_worker_boot] ||= []) << block
_ary(:before_worker_boot) << block
end
# *Cluster mode only* Code to run when a master process is
@ -232,7 +264,7 @@ module Puma
# This can be called multiple times to add hooks.
#
def on_worker_fork(&block)
(@options[:before_worker_fork] ||= []) << block
_ary(:before_worker_fork) << block
end
# *Cluster mode only* Code to run when a worker boots to setup
@ -241,7 +273,7 @@ module Puma
# This can be called multiple times to add hooks.
#
def after_worker_boot(&block)
(@options[:after_worker_boot] ||= []) << block
_ary(:after_worker_fork) << block
end
# The directory to operate out of.
@ -382,14 +414,10 @@ module Puma
end
end
# Load the named plugin for use by this configuration
#
def plugin(name)
plugin = @config.load_plugin name
private
if plugin.respond_to? :config
plugin.config self
end
def _ary(key)
(@options.cur[key] ||= [])
end
end
end

View file

@ -138,5 +138,10 @@ module Puma
def self.stdio
Events.new $stdout, $stderr
end
def self.null
n = NullIO.new
Events.new n, n
end
end
end

View file

@ -198,9 +198,7 @@ module Puma
end
def restart!
(@options[:on_restart] || []).each do |block|
block.call self
end
@config.run_hooks :on_restart, self
if Puma.jruby?
close_binder_listeners

View file

@ -36,5 +36,14 @@ module Puma
def size
0
end
def sync=(v)
end
def puts(*ary)
end
def write(*ary)
end
end
end

View file

@ -1,6 +1,62 @@
require 'puma/plugin_loader'
module Puma
class UnknownPlugin < RuntimeError; end
class PluginLoader
def initialize
@instances = []
end
def create(name)
if cls = Plugins.find(name)
plugin = cls.new(Plugin)
@instances << plugin
return plugin
end
raise UnknownPlugin, "File failed to register properly named plugin"
end
def fire_starts(launcher)
@instances.each do |i|
if i.respond_to? :start
i.start(launcher)
end
end
end
end
class PluginRegistry
def initialize
@plugins = {}
end
def register(name, cls)
@plugins[name] = cls
end
def find(name)
name = name.to_s
if cls = @plugins[name]
return cls
end
begin
require "puma/plugin/#{name}"
rescue LoadError
raise UnknownPlugin, "Unable to find plugin: #{name}"
end
if cls = @plugins[name]
return cls
end
raise UnknownPlugin, "file failed to register a plugin"
end
end
Plugins = PluginRegistry.new
class Plugin
def self.extract_name(ary)
path = ary.first.split(":").first

View file

@ -1,61 +0,0 @@
require 'puma/plugin'
module Puma
class UnknownPlugin < RuntimeError; end
class PluginLoader
def initialize
@instances = []
end
def create(name)
if cls = Plugins.find(name)
plugin = cls.new(self)
@instances << plugin
return plugin
end
raise UnknownPlugin, "File failed to register properly named plugin"
end
def fire_starts(launcher)
@instances.each do |i|
if i.respond_to? :start
i.start(launcher)
end
end
end
end
class PluginRegistry
def initialize
@plugins = {}
end
def register(name, cls)
@plugins[name] = cls
end
def find(name)
name = name.to_s
if cls = @plugins[name]
return cls
end
begin
require "puma/plugin/#{name}"
rescue LoadError
raise UnknownPlugin, "Unable to find plugin: #{name}"
end
if cls = @plugins[name]
return cls
end
raise UnknownPlugin, "file failed to register a plugin"
end
end
Plugins = PluginRegistry.new
end