mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Add plugin mechanism and initial plugin
This commit is contained in:
parent
f788af0c8f
commit
663666c5b7
9 changed files with 161 additions and 10 deletions
|
@ -1,4 +1,5 @@
|
||||||
require 'puma/const'
|
require 'puma/const'
|
||||||
|
require 'uri'
|
||||||
|
|
||||||
module Puma
|
module Puma
|
||||||
class Binder
|
class Binder
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'puma/rack/builder'
|
require 'puma/rack/builder'
|
||||||
|
require 'puma/plugin_loader'
|
||||||
|
|
||||||
module Puma
|
module Puma
|
||||||
|
|
||||||
|
@ -94,13 +95,14 @@ module Puma
|
||||||
def self.from_file(path)
|
def self.from_file(path)
|
||||||
cfg = new
|
cfg = new
|
||||||
|
|
||||||
DSL.new(cfg.options)._load_from path
|
DSL.new(cfg.options, cfg)._load_from path
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(options={})
|
def initialize(options={})
|
||||||
@options = LeveledOptions.new
|
@options = LeveledOptions.new
|
||||||
|
@plugins = PluginLoader.new
|
||||||
|
|
||||||
# options.each do |k,v|
|
# options.each do |k,v|
|
||||||
# @options[k] = v
|
# @options[k] = v
|
||||||
|
@ -108,19 +110,19 @@ module Puma
|
||||||
|
|
||||||
if block_given?
|
if block_given?
|
||||||
@options.shift
|
@options.shift
|
||||||
d = DSL.new(@options)
|
d = DSL.new(@options, self)
|
||||||
yield d
|
yield d
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_reader :options, :plugins
|
||||||
|
|
||||||
def configure
|
def configure
|
||||||
@options.shift
|
@options.shift
|
||||||
d = DSL.new(@options)
|
d = DSL.new(@options, self)
|
||||||
yield d
|
yield d
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :options
|
|
||||||
|
|
||||||
def initialize_copy(other)
|
def initialize_copy(other)
|
||||||
@conf = nil
|
@conf = nil
|
||||||
@cli_options = nil
|
@cli_options = nil
|
||||||
|
@ -169,7 +171,7 @@ module Puma
|
||||||
files.each do |f|
|
files.each do |f|
|
||||||
@options.shift
|
@options.shift
|
||||||
|
|
||||||
DSL.load @options, f
|
DSL.load @options, self, f
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -234,6 +236,10 @@ module Puma
|
||||||
@options[:environment] || ENV['RACK_ENV'] || 'development'
|
@options[:environment] || ENV['RACK_ENV'] || 'development'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def load_plugin(name)
|
||||||
|
@plugins.create name
|
||||||
|
end
|
||||||
|
|
||||||
def self.temp_path
|
def self.temp_path
|
||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
|
|
||||||
|
|
23
lib/puma/convenient.rb
Normal file
23
lib/puma/convenient.rb
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
require 'puma/launcher'
|
||||||
|
require 'puma/configuration'
|
||||||
|
|
||||||
|
module Puma
|
||||||
|
def self.run(opts={})
|
||||||
|
cfg = Puma::Configuration.new do |c|
|
||||||
|
if port = opts[:port]
|
||||||
|
c.port port
|
||||||
|
end
|
||||||
|
|
||||||
|
c.quiet
|
||||||
|
|
||||||
|
yield c
|
||||||
|
end
|
||||||
|
|
||||||
|
cfg.clamp
|
||||||
|
|
||||||
|
events = Puma::Events.strings
|
||||||
|
|
||||||
|
launcher = Puma::Launcher.new cfg, :events => events
|
||||||
|
launcher.run
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,15 +4,16 @@ module Puma
|
||||||
class DSL
|
class DSL
|
||||||
include ConfigDefault
|
include ConfigDefault
|
||||||
|
|
||||||
def self.load(options, path)
|
def self.load(options, cfg, path)
|
||||||
new(options).tap do |obj|
|
new(options, cfg).tap do |obj|
|
||||||
obj._load_from(path)
|
obj._load_from(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
options
|
options
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(config, options)
|
||||||
|
@config = config
|
||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -380,5 +381,11 @@ module Puma
|
||||||
raise "Invalid value for set_remote_address - #{val}"
|
raise "Invalid value for set_remote_address - #{val}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Load the named plugin for use by this configuration
|
||||||
|
#
|
||||||
|
def plugin(name)
|
||||||
|
@config.load_plugin name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -166,6 +166,8 @@ module Puma
|
||||||
def run
|
def run
|
||||||
@config.clamp
|
@config.clamp
|
||||||
|
|
||||||
|
@config.plugins.fire_starts self
|
||||||
|
|
||||||
setup_signals
|
setup_signals
|
||||||
set_process_title
|
set_process_title
|
||||||
@runner.run
|
@runner.run
|
||||||
|
@ -219,7 +221,6 @@ module Puma
|
||||||
end
|
end
|
||||||
|
|
||||||
argv = restart_args
|
argv = restart_args
|
||||||
p argv
|
|
||||||
Dir.chdir(@restart_dir)
|
Dir.chdir(@restart_dir)
|
||||||
argv += [redirects] if RUBY_VERSION >= '1.9'
|
argv += [redirects] if RUBY_VERSION >= '1.9'
|
||||||
Kernel.exec(*argv)
|
Kernel.exec(*argv)
|
||||||
|
|
30
lib/puma/plugin.rb
Normal file
30
lib/puma/plugin.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
require 'puma/plugin_loader'
|
||||||
|
|
||||||
|
module Puma
|
||||||
|
class Plugin
|
||||||
|
def self.extract_name(ary)
|
||||||
|
path = ary.first.split(":").first
|
||||||
|
|
||||||
|
m = %r!puma/plugin/([^/]*)\.rb$!.match(path)
|
||||||
|
return m[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.create(&blk)
|
||||||
|
name = extract_name(caller)
|
||||||
|
|
||||||
|
cls = Class.new(self)
|
||||||
|
|
||||||
|
cls.class_eval(&blk)
|
||||||
|
|
||||||
|
Plugins.register name, cls
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(loader)
|
||||||
|
@loader = loader
|
||||||
|
end
|
||||||
|
|
||||||
|
def in_background(&blk)
|
||||||
|
Thread.new(&blk)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
23
lib/puma/plugin/tmp_restart.rb
Normal file
23
lib/puma/plugin/tmp_restart.rb
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
require 'puma/plugin'
|
||||||
|
|
||||||
|
Puma::Plugin.create do
|
||||||
|
def start(launcher)
|
||||||
|
path = File.join("tmp", "restart.txt")
|
||||||
|
|
||||||
|
File.write path, ""
|
||||||
|
|
||||||
|
orig = File.stat(path).mtime
|
||||||
|
|
||||||
|
in_background do
|
||||||
|
while true
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
if File.stat(path).mtime > orig
|
||||||
|
launcher.restart
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
59
lib/puma/plugin_loader.rb
Normal file
59
lib/puma/plugin_loader.rb
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
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
|
1
test/config/plugin.rb
Normal file
1
test/config/plugin.rb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
plugin :tmp_restart
|
Loading…
Add table
Reference in a new issue