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 'uri'
|
||||
|
||||
module Puma
|
||||
class Binder
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'puma/rack/builder'
|
||||
require 'puma/plugin_loader'
|
||||
|
||||
module Puma
|
||||
|
||||
|
@ -94,13 +95,14 @@ module Puma
|
|||
def self.from_file(path)
|
||||
cfg = new
|
||||
|
||||
DSL.new(cfg.options)._load_from path
|
||||
DSL.new(cfg.options, cfg)._load_from path
|
||||
|
||||
return cfg
|
||||
end
|
||||
|
||||
def initialize(options={})
|
||||
@options = LeveledOptions.new
|
||||
@plugins = PluginLoader.new
|
||||
|
||||
# options.each do |k,v|
|
||||
# @options[k] = v
|
||||
|
@ -108,19 +110,19 @@ module Puma
|
|||
|
||||
if block_given?
|
||||
@options.shift
|
||||
d = DSL.new(@options)
|
||||
d = DSL.new(@options, self)
|
||||
yield d
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :options, :plugins
|
||||
|
||||
def configure
|
||||
@options.shift
|
||||
d = DSL.new(@options)
|
||||
d = DSL.new(@options, self)
|
||||
yield d
|
||||
end
|
||||
|
||||
attr_reader :options
|
||||
|
||||
def initialize_copy(other)
|
||||
@conf = nil
|
||||
@cli_options = nil
|
||||
|
@ -169,7 +171,7 @@ module Puma
|
|||
files.each do |f|
|
||||
@options.shift
|
||||
|
||||
DSL.load @options, f
|
||||
DSL.load @options, self, f
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -234,6 +236,10 @@ module Puma
|
|||
@options[:environment] || ENV['RACK_ENV'] || 'development'
|
||||
end
|
||||
|
||||
def load_plugin(name)
|
||||
@plugins.create name
|
||||
end
|
||||
|
||||
def self.temp_path
|
||||
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
|
||||
include ConfigDefault
|
||||
|
||||
def self.load(options, path)
|
||||
new(options).tap do |obj|
|
||||
def self.load(options, cfg, path)
|
||||
new(options, cfg).tap do |obj|
|
||||
obj._load_from(path)
|
||||
end
|
||||
|
||||
options
|
||||
end
|
||||
|
||||
def initialize(options)
|
||||
def initialize(config, options)
|
||||
@config = config
|
||||
@options = options
|
||||
end
|
||||
|
||||
|
@ -380,5 +381,11 @@ module Puma
|
|||
raise "Invalid value for set_remote_address - #{val}"
|
||||
end
|
||||
end
|
||||
|
||||
# Load the named plugin for use by this configuration
|
||||
#
|
||||
def plugin(name)
|
||||
@config.load_plugin name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -166,6 +166,8 @@ module Puma
|
|||
def run
|
||||
@config.clamp
|
||||
|
||||
@config.plugins.fire_starts self
|
||||
|
||||
setup_signals
|
||||
set_process_title
|
||||
@runner.run
|
||||
|
@ -219,7 +221,6 @@ module Puma
|
|||
end
|
||||
|
||||
argv = restart_args
|
||||
p argv
|
||||
Dir.chdir(@restart_dir)
|
||||
argv += [redirects] if RUBY_VERSION >= '1.9'
|
||||
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…
Reference in a new issue