2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-07 01:28:02 -05:00
|
|
|
module Puma
|
2016-02-07 17:51:54 -05:00
|
|
|
class UnknownPlugin < RuntimeError; end
|
|
|
|
|
|
|
|
class PluginLoader
|
|
|
|
def initialize
|
|
|
|
@instances = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(name)
|
|
|
|
if cls = Plugins.find(name)
|
2019-12-24 16:25:40 -05:00
|
|
|
plugin = cls.new
|
2016-02-07 17:51:54 -05:00
|
|
|
@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 = {}
|
2016-07-24 19:00:57 -04:00
|
|
|
@background = []
|
2016-02-07 17:51:54 -05:00
|
|
|
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
|
2016-07-24 19:00:57 -04:00
|
|
|
|
|
|
|
def add_background(blk)
|
|
|
|
@background << blk
|
|
|
|
end
|
|
|
|
|
|
|
|
def fire_background
|
2019-09-18 09:53:29 -04:00
|
|
|
@background.each_with_index do |b, i|
|
2019-09-15 04:52:34 -04:00
|
|
|
Thread.new do
|
2019-09-18 09:53:29 -04:00
|
|
|
Puma.set_thread_name "plugin background #{i}"
|
2019-09-15 04:52:34 -04:00
|
|
|
b.call
|
|
|
|
end
|
2016-07-24 19:00:57 -04:00
|
|
|
end
|
|
|
|
end
|
2016-02-07 17:51:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Plugins = PluginRegistry.new
|
|
|
|
|
2016-02-07 01:28:02 -05:00
|
|
|
class Plugin
|
2016-02-26 07:44:23 -05:00
|
|
|
# Matches
|
|
|
|
# "C:/Ruby22/lib/ruby/gems/2.2.0/gems/puma-3.0.1/lib/puma/plugin/tmp_restart.rb:3:in `<top (required)>'"
|
|
|
|
# AS
|
|
|
|
# C:/Ruby22/lib/ruby/gems/2.2.0/gems/puma-3.0.1/lib/puma/plugin/tmp_restart.rb
|
|
|
|
CALLER_FILE = /
|
|
|
|
\A # start of string
|
|
|
|
.+ # file path (one or more characters)
|
|
|
|
(?= # stop previous match when
|
|
|
|
:\d+ # a colon is followed by one or more digits
|
|
|
|
:in # followed by a colon followed by in
|
|
|
|
)
|
|
|
|
/x
|
|
|
|
|
2016-02-07 01:28:02 -05:00
|
|
|
def self.extract_name(ary)
|
2016-02-26 07:44:23 -05:00
|
|
|
path = ary.first[CALLER_FILE]
|
2016-02-07 01:28:02 -05:00
|
|
|
|
|
|
|
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 in_background(&blk)
|
2016-07-24 19:00:57 -04:00
|
|
|
Plugins.add_background blk
|
2016-02-07 01:28:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|