/
plugin.rb
116 lines (92 loc) · 2.31 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# frozen_string_literal: true
module Puma
class UnknownPlugin < RuntimeError; end
class PluginLoader
def initialize
@instances = []
end
def create(name)
if cls = Plugins.find(name)
plugin = cls.new
@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 = {}
@background = []
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
def add_background(blk)
@background << blk
end
def fire_background
@background.each_with_index do |b, i|
Thread.new do
Puma.set_thread_name "plugin background #{i}"
b.call
end
end
end
end
Plugins = PluginRegistry.new
class Plugin
# 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
def self.extract_name(ary)
path = ary.first[CALLER_FILE]
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)
Plugins.add_background blk
end
def workers_supported?
return false if Puma.jruby? || Puma.windows?
true
end
end
end