bluepill 0.1.6 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -1
- data/CONTRIBUTING.md +2 -2
- data/README.md +106 -9
- data/bin/bluepill +25 -30
- data/bluepill.gemspec +21 -28
- data/lib/bluepill/application/lifecycle.rb +166 -0
- data/lib/bluepill/application/listener.rb +124 -0
- data/lib/bluepill/application/previous_daemon.rb +81 -0
- data/lib/bluepill/application.rb +56 -254
- data/lib/bluepill/base_directory.rb +93 -0
- data/lib/bluepill/clock.rb +25 -0
- data/lib/bluepill/condition_watch.rb +136 -58
- data/lib/bluepill/controller/directory.rb +59 -0
- data/lib/bluepill/controller.rb +99 -94
- data/lib/bluepill/daemonize.rb +234 -0
- data/lib/bluepill/dsl/app_proxy.rb +30 -6
- data/lib/bluepill/dsl/process_factory/children.rb +71 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
- data/lib/bluepill/dsl/process_factory.rb +81 -92
- data/lib/bluepill/dsl/process_proxy.rb +109 -21
- data/lib/bluepill/group.rb +187 -78
- data/lib/bluepill/logger.rb +139 -48
- data/lib/bluepill/pid_file.rb +23 -0
- data/lib/bluepill/process/attributes.rb +379 -0
- data/lib/bluepill/process/children.rb +70 -0
- data/lib/bluepill/process/pid.rb +154 -0
- data/lib/bluepill/process/state_machine.rb +196 -0
- data/lib/bluepill/process/transition.rb +45 -0
- data/lib/bluepill/process/transition_callbacks.rb +101 -0
- data/lib/bluepill/process/user_commands.rb +71 -0
- data/lib/bluepill/process/watches.rb +141 -0
- data/lib/bluepill/process.rb +60 -657
- data/lib/bluepill/process_commands/execution.rb +89 -0
- data/lib/bluepill/process_commands/hooks.rb +186 -0
- data/lib/bluepill/process_commands/stop_signals.rb +114 -0
- data/lib/bluepill/process_commands.rb +106 -174
- data/lib/bluepill/process_conditions/always_true.rb +1 -1
- data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
- data/lib/bluepill/process_conditions/file_time.rb +4 -5
- data/lib/bluepill/process_conditions/http.rb +52 -24
- data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
- data/lib/bluepill/process_conditions/process_condition.rb +34 -3
- data/lib/bluepill/process_conditions/running_time.rb +1 -25
- data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
- data/lib/bluepill/process_conditions.rb +3 -2
- data/lib/bluepill/process_journal/cleanup.rb +200 -0
- data/lib/bluepill/process_journal/lock.rb +106 -0
- data/lib/bluepill/process_journal.rb +126 -233
- data/lib/bluepill/process_statistics.rb +2 -2
- data/lib/bluepill/socket.rb +60 -20
- data/lib/bluepill/system/command_setup.rb +98 -0
- data/lib/bluepill/system/commands.rb +219 -248
- data/lib/bluepill/system/daemon.rb +161 -0
- data/lib/bluepill/system/ps.rb +85 -0
- data/lib/bluepill/system/ps_table.rb +163 -0
- data/lib/bluepill/system.rb +45 -126
- data/lib/bluepill/trigger.rb +52 -26
- data/lib/bluepill/triggers/flapping.rb +8 -16
- data/lib/bluepill/units.rb +126 -0
- data/lib/bluepill/util/inflections.rb +34 -0
- data/lib/bluepill/util/levenshtein.rb +53 -0
- data/lib/bluepill/util/rotational_array.rb +1 -1
- data/lib/bluepill/version.rb +13 -12
- data/lib/bluepill.rb +33 -36
- data/sig/bluepill/application.rbs +257 -0
- data/sig/bluepill/checks.rbs +203 -0
- data/sig/bluepill/dsl.rbs +126 -0
- data/sig/bluepill/logger.rbs +104 -0
- data/sig/bluepill/process.rbs +542 -0
- data/sig/bluepill/system.rbs +316 -0
- data/sig/bluepill.rbs +50 -871
- metadata +40 -45
|
@@ -1,13 +1,18 @@
|
|
|
1
|
+
require "bluepill/dsl/process_factory/children"
|
|
2
|
+
require "bluepill/dsl/process_factory/registry"
|
|
3
|
+
|
|
1
4
|
module Bluepill
|
|
2
5
|
# Create the processes that a config file describes, and check them
|
|
3
6
|
#
|
|
4
7
|
# A factory turns a process block into a {Process}, after it checks the
|
|
5
8
|
# config for mistakes, such as two processes with the same name or pid
|
|
6
|
-
# file
|
|
7
|
-
# them.
|
|
9
|
+
# file, which it looks up in the application's {Registry}. A process that
|
|
10
|
+
# monitors its children gets a factory of its own for them.
|
|
8
11
|
#
|
|
9
12
|
# @api private
|
|
10
13
|
class ProcessFactory
|
|
14
|
+
include Children
|
|
15
|
+
|
|
11
16
|
# Return the attributes that each process starts with
|
|
12
17
|
#
|
|
13
18
|
# @return [Hash{Symbol => Object}] the attributes
|
|
@@ -15,79 +20,54 @@ module Bluepill
|
|
|
15
20
|
# @api private
|
|
16
21
|
attr_reader :attributes
|
|
17
22
|
|
|
18
|
-
# Only the keys matter
|
|
19
|
-
@@process_keys = {} #: Hash[Array[String?], nil]
|
|
20
|
-
@@pid_files = {} #: Hash[String, nil]
|
|
21
|
-
|
|
22
23
|
# Create a factory of processes
|
|
23
24
|
#
|
|
24
25
|
# @param [Hash{Symbol => Object}] attributes the attributes that each
|
|
25
26
|
# process starts with
|
|
26
27
|
# @param [Proc] process_block the block that configures each process
|
|
28
|
+
# @param [Registry] registry the process names and pid files that the
|
|
29
|
+
# application's other processes use
|
|
27
30
|
#
|
|
28
31
|
# @return [void]
|
|
29
32
|
#
|
|
30
33
|
# @api private
|
|
31
|
-
def initialize(attributes, process_block)
|
|
34
|
+
def initialize(attributes, process_block, registry = Registry.new)
|
|
32
35
|
@attributes = attributes
|
|
33
36
|
@process_block = process_block
|
|
37
|
+
@registry = registry
|
|
34
38
|
end
|
|
35
39
|
|
|
36
40
|
# Create a process from the factory's block, and check its config
|
|
37
41
|
#
|
|
38
|
-
#
|
|
42
|
+
# The process starts with a copy of the factory's attributes, so the
|
|
43
|
+
# factory's own stay as they were. A process without a pid_file gets a
|
|
44
|
+
# default one in pids_dir.
|
|
39
45
|
#
|
|
40
46
|
# @param [String] name the process's name
|
|
41
47
|
# @param [String] pids_dir the directory of the default pid file
|
|
42
48
|
#
|
|
43
|
-
# @return [Process] the process
|
|
49
|
+
# @return [Array(Process, Object)] the process, and the group that its
|
|
50
|
+
# block set, or nil for none
|
|
44
51
|
#
|
|
45
52
|
# @api private
|
|
46
53
|
def create_process(name, pids_dir)
|
|
47
|
-
|
|
54
|
+
attributes = @attributes.dup
|
|
55
|
+
default_pid_file = assign_default_pid_file(attributes, name, pids_dir)
|
|
48
56
|
|
|
49
|
-
process = ProcessProxy.new(name,
|
|
50
|
-
child_process_block =
|
|
51
|
-
|
|
57
|
+
process = ProcessProxy.new(name, attributes, @process_block)
|
|
58
|
+
child_process_block = attributes.delete(:child_process_block)
|
|
59
|
+
attributes[:child_process_factory] = ProcessFactory.new(attributes, child_process_block) if attributes[:monitor_children]
|
|
52
60
|
|
|
53
61
|
validate_process! process
|
|
54
62
|
warn_unless_pid_known(process, default_pid_file)
|
|
55
|
-
process.to_process
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
# Create the process of a child that bluepill found
|
|
59
|
-
#
|
|
60
|
-
# The child gets its parent's grace times, and its state from whether
|
|
61
|
-
# it's running.
|
|
62
|
-
#
|
|
63
|
-
# @param [String] name the child's name
|
|
64
|
-
# @param [Integer] pid the child's pid
|
|
65
|
-
# @param [Logger] logger the child's logger
|
|
66
|
-
#
|
|
67
|
-
# @return [Process] the child's process
|
|
68
|
-
#
|
|
69
|
-
# @api private
|
|
70
|
-
def create_child_process(name, pid, logger)
|
|
71
|
-
attributes = {} #: Hash[Symbol, untyped]
|
|
72
|
-
# A grace time the parent leaves unset keeps the Process default
|
|
73
|
-
[:start_grace_time, :stop_grace_time, :restart_grace_time].each do |a|
|
|
74
|
-
attributes[a] = @attributes.fetch(a) if @attributes.key?(a)
|
|
75
|
-
end
|
|
76
|
-
attributes[:actual_pid] = pid
|
|
77
|
-
attributes[:logger] = logger
|
|
78
|
-
|
|
79
|
-
child = ProcessProxy.new(name, attributes, @process_block)
|
|
80
|
-
validate_child_process! child
|
|
81
|
-
process = child.to_process
|
|
82
|
-
|
|
83
|
-
process.determine_initial_state
|
|
84
|
-
process
|
|
63
|
+
[process.to_process, attributes[:group]]
|
|
85
64
|
end
|
|
86
65
|
|
|
87
|
-
|
|
66
|
+
protected
|
|
88
67
|
|
|
89
68
|
# Set the default pid file, unless the process sets its own
|
|
90
69
|
#
|
|
70
|
+
# @param [Hash{Symbol => Object}] attributes the process's attributes
|
|
91
71
|
# @param [String] process_name the process's name
|
|
92
72
|
# @param [String] pids_dir the directory of the pid file
|
|
93
73
|
#
|
|
@@ -95,18 +75,15 @@ module Bluepill
|
|
|
95
75
|
# sets its own
|
|
96
76
|
#
|
|
97
77
|
# @api private
|
|
98
|
-
def assign_default_pid_file(process_name, pids_dir)
|
|
99
|
-
return if
|
|
78
|
+
def assign_default_pid_file(attributes, process_name, pids_dir)
|
|
79
|
+
return if attributes.key?(:pid_file)
|
|
100
80
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
@attributes[:pid_file] = File.join(pids_dir, "#{default_pid_name}.pid")
|
|
81
|
+
default_pid_name = [attributes[:group], process_name].compact.join("_").gsub(/[^A-Za-z0-9_-]/, "_")
|
|
82
|
+
attributes[:pid_file] = File.join(pids_dir, "#{default_pid_name}.pid")
|
|
104
83
|
end
|
|
105
84
|
|
|
106
85
|
# Check a process's config, and exit on a mistake
|
|
107
86
|
#
|
|
108
|
-
# The process shares its attributes hash with the factory.
|
|
109
|
-
#
|
|
110
87
|
# @param [ProcessProxy] process the process's proxy
|
|
111
88
|
#
|
|
112
89
|
# @return [void]
|
|
@@ -115,8 +92,8 @@ module Bluepill
|
|
|
115
92
|
def validate_process!(process)
|
|
116
93
|
validate_unique_process!(process)
|
|
117
94
|
validate_required_attributes!(process)
|
|
118
|
-
validate_unique_pid_file!
|
|
119
|
-
validate_stop_signals!
|
|
95
|
+
validate_unique_pid_file!(process)
|
|
96
|
+
validate_stop_signals!(process)
|
|
120
97
|
end
|
|
121
98
|
|
|
122
99
|
# Exit if another process in the group has the same name
|
|
@@ -127,15 +104,12 @@ module Bluepill
|
|
|
127
104
|
#
|
|
128
105
|
# @api private
|
|
129
106
|
def validate_unique_process!(process)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
config_error!(message)
|
|
137
|
-
end
|
|
138
|
-
@@process_keys[process_key] = nil
|
|
107
|
+
attributes = process.attributes
|
|
108
|
+
return if @registry.add_process?(attributes[:group], process.name)
|
|
109
|
+
|
|
110
|
+
message = "Config Error: You have two entries for the process name '#{process.name}'"
|
|
111
|
+
message += " in the group '#{attributes.fetch(:group)}'" if attributes.key?(:group)
|
|
112
|
+
config_error!(message)
|
|
139
113
|
end
|
|
140
114
|
|
|
141
115
|
# Exit unless the process has a start_command
|
|
@@ -146,48 +120,77 @@ module Bluepill
|
|
|
146
120
|
#
|
|
147
121
|
# @api private
|
|
148
122
|
def validate_required_attributes!(process)
|
|
149
|
-
return if attributes.key?(:start_command)
|
|
123
|
+
return if process.attributes.key?(:start_command)
|
|
150
124
|
|
|
151
125
|
config_error!("Config Error: You must specify a start_command for '#{process.name}'")
|
|
152
126
|
end
|
|
153
127
|
|
|
154
128
|
# Exit if another process has the same pid file
|
|
155
129
|
#
|
|
130
|
+
# @param [ProcessProxy] process the process's proxy
|
|
131
|
+
#
|
|
156
132
|
# @return [void]
|
|
157
133
|
#
|
|
158
134
|
# @api private
|
|
159
|
-
def validate_unique_pid_file!
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
135
|
+
def validate_unique_pid_file!(process)
|
|
136
|
+
pid_file = process.attributes.fetch(:pid_file).strip
|
|
137
|
+
return if @registry.add_pid_file?(pid_file)
|
|
138
|
+
|
|
139
|
+
config_error!("Config Error: You have two entries with the pid file: #{pid_file}")
|
|
163
140
|
end
|
|
164
141
|
|
|
165
|
-
# Exit
|
|
142
|
+
# Exit on a mistake in stop_signals
|
|
166
143
|
#
|
|
167
144
|
# The signals and delays must alternate, starting and ending with a
|
|
168
|
-
# signal, and stop_grace_time must be longer than the delays.
|
|
169
|
-
#
|
|
145
|
+
# signal, and stop_grace_time must be longer than the delays.
|
|
146
|
+
#
|
|
147
|
+
# @param [ProcessProxy] process the process's proxy
|
|
170
148
|
#
|
|
171
149
|
# @return [void]
|
|
172
150
|
#
|
|
173
151
|
# @api private
|
|
174
|
-
def validate_stop_signals!
|
|
175
|
-
stop_signals = attributes[:stop_signals]
|
|
152
|
+
def validate_stop_signals!(process)
|
|
153
|
+
stop_signals = process.attributes[:stop_signals]
|
|
176
154
|
return if stop_signals.nil?
|
|
177
155
|
|
|
156
|
+
validate_stop_signal_order!(stop_signals)
|
|
157
|
+
validate_stop_grace_time!(stop_signals, process.attributes[:stop_grace_time])
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Exit unless stop_signals alternates signals and delays
|
|
161
|
+
#
|
|
162
|
+
# The signals and delays must alternate, starting and ending with a
|
|
163
|
+
# signal. Checks the more specific problems before the 'odd number'
|
|
164
|
+
# message.
|
|
165
|
+
#
|
|
166
|
+
# @param [Array<Symbol, Integer>] stop_signals the signals and delays
|
|
167
|
+
#
|
|
168
|
+
# @return [void]
|
|
169
|
+
#
|
|
170
|
+
# @api private
|
|
171
|
+
def validate_stop_signal_order!(stop_signals)
|
|
178
172
|
stop_signals.each_with_index do |value, i|
|
|
179
|
-
expected, label = i.even? ? [Symbol,
|
|
173
|
+
expected, label = i.even? ? [Symbol, "a symbol (signal)"] : [Integer, "a number (delay)"]
|
|
180
174
|
next if value.is_a?(expected)
|
|
181
175
|
|
|
182
176
|
config_error!("Config Error: Invalid stop_signals! Expected #{label} at position #{i} instead of '#{value}'.")
|
|
183
177
|
end
|
|
184
|
-
config_error!(
|
|
178
|
+
config_error!("Config Error: Invalid stop_signals! Expected an odd number of elements.") unless stop_signals.size.odd?
|
|
179
|
+
end
|
|
185
180
|
|
|
186
|
-
|
|
181
|
+
# Exit unless stop_grace_time is longer than the stop_signals delays
|
|
182
|
+
#
|
|
183
|
+
# @param [Array<Symbol, Integer>] stop_signals the signals and delays
|
|
184
|
+
# @param [Integer, nil] stop_grace_time the process's stop_grace_time
|
|
185
|
+
#
|
|
186
|
+
# @return [void]
|
|
187
|
+
#
|
|
188
|
+
# @api private
|
|
189
|
+
def validate_stop_grace_time!(stop_signals, stop_grace_time)
|
|
187
190
|
delays = stop_signals.select.with_index { |_, i| i.odd? }
|
|
188
191
|
return unless stop_grace_time.nil? || stop_grace_time <= delays.inject(0, :+)
|
|
189
192
|
|
|
190
|
-
config_error!(
|
|
193
|
+
config_error!("Config Error: Stop_grace_time should be greater than the sum of stop_signals delays!")
|
|
191
194
|
end
|
|
192
195
|
|
|
193
196
|
# Warn when bluepill can't find the process's pid
|
|
@@ -203,11 +206,12 @@ module Bluepill
|
|
|
203
206
|
#
|
|
204
207
|
# @api private
|
|
205
208
|
def warn_unless_pid_known(process, default_pid_file)
|
|
209
|
+
attributes = process.attributes
|
|
206
210
|
return if attributes[:daemonize] || attributes[:pid_command] || !attributes.fetch(:pid_file).eql?(default_pid_file)
|
|
207
211
|
|
|
208
212
|
warning = "Config Warning: '#{process.name}' sets no daemonize, pid_file or pid_command, " \
|
|
209
213
|
"so bluepill cannot find its pid unless start_command writes it to #{default_pid_file}. " \
|
|
210
|
-
|
|
214
|
+
"Set daemonize = true if start_command runs in the foreground."
|
|
211
215
|
$stderr.puts warning
|
|
212
216
|
end
|
|
213
217
|
|
|
@@ -222,20 +226,5 @@ module Bluepill
|
|
|
222
226
|
$stderr.puts message
|
|
223
227
|
exit(6)
|
|
224
228
|
end
|
|
225
|
-
|
|
226
|
-
# Exit unless a monitored child has a stop_command
|
|
227
|
-
#
|
|
228
|
-
# @param [ProcessProxy] child the child's proxy
|
|
229
|
-
#
|
|
230
|
-
# @return [void]
|
|
231
|
-
#
|
|
232
|
-
# @api private
|
|
233
|
-
def validate_child_process!(child)
|
|
234
|
-
return if child.attributes.key?(:stop_command)
|
|
235
|
-
|
|
236
|
-
$stderr.puts "Config Error: Invalid child process monitor for #{child.name}"
|
|
237
|
-
$stderr.puts 'You must specify a stop command to monitor child processes.'
|
|
238
|
-
exit(6)
|
|
239
|
-
end
|
|
240
229
|
end
|
|
241
230
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "bluepill/util/levenshtein"
|
|
2
|
+
|
|
1
3
|
module Bluepill
|
|
2
4
|
# The self of a process block in a config file
|
|
3
5
|
#
|
|
@@ -58,7 +60,8 @@ module Bluepill
|
|
|
58
60
|
#
|
|
59
61
|
# A name ending in `=`, or one given a value, sets the attribute. A name
|
|
60
62
|
# ending in `!` sets it to true. A name alone reads an attribute that's
|
|
61
|
-
# set.
|
|
63
|
+
# set. Only the attributes a process has can be set, so a misspelled one
|
|
64
|
+
# fails when the config loads instead of being ignored.
|
|
62
65
|
#
|
|
63
66
|
# @example
|
|
64
67
|
# process.start_command = '/usr/bin/my_server'
|
|
@@ -71,27 +74,21 @@ module Bluepill
|
|
|
71
74
|
#
|
|
72
75
|
# @return [Object] the value that was set or read
|
|
73
76
|
#
|
|
74
|
-
# @raise [NoMethodError] for an attribute
|
|
75
|
-
# value
|
|
77
|
+
# @raise [NoMethodError] for an attribute a process doesn't have, one
|
|
78
|
+
# that isn't set yet, or more than one value
|
|
76
79
|
#
|
|
77
80
|
# @api public
|
|
78
81
|
def method_missing(name, *args)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if attribute.end_with?('!')
|
|
86
|
-
@attributes[attribute.chomp('!').to_sym] = true
|
|
87
|
-
elsif @attributes.key?(name)
|
|
88
|
-
@attributes.fetch(name)
|
|
89
|
-
else
|
|
90
|
-
super
|
|
91
|
-
end
|
|
92
|
-
else
|
|
93
|
-
super
|
|
82
|
+
key, reader, value = attribute_call(name, args)
|
|
83
|
+
if key
|
|
84
|
+
return @attributes.fetch(key) if reader && @attributes.key?(key)
|
|
85
|
+
|
|
86
|
+
unknown_attribute!(key) unless self.class.attribute?(key)
|
|
87
|
+
return @attributes[key] = value unless reader
|
|
94
88
|
end
|
|
89
|
+
# A call with the wrong number of values, or the reader of a known
|
|
90
|
+
# attribute that isn't set yet
|
|
91
|
+
super
|
|
95
92
|
end
|
|
96
93
|
|
|
97
94
|
# Return whether the proxy answers a name through method_missing
|
|
@@ -101,12 +98,38 @@ module Bluepill
|
|
|
101
98
|
# @param [Symbol] name the method's name
|
|
102
99
|
# @param [Boolean, nil] _include_private ignored
|
|
103
100
|
#
|
|
104
|
-
# @return [Boolean] true for
|
|
105
|
-
# set
|
|
101
|
+
# @return [Boolean] true for the setter or `name!` of an attribute a
|
|
102
|
+
# process has, or the reader of one that's set
|
|
106
103
|
#
|
|
107
104
|
# @api private
|
|
108
105
|
def respond_to_missing?(name, _include_private)
|
|
109
|
-
|
|
106
|
+
attribute = name.to_s
|
|
107
|
+
key = attribute.sub(/[=!]\z/, "")
|
|
108
|
+
return @attributes.key?(name) if key.eql?(attribute)
|
|
109
|
+
|
|
110
|
+
self.class.attribute?(key.to_sym)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Return whether a process has an attribute that a config can set
|
|
114
|
+
#
|
|
115
|
+
# These are a process's configurable attributes, and the group it's in.
|
|
116
|
+
#
|
|
117
|
+
# @param [Symbol] name the attribute's name
|
|
118
|
+
#
|
|
119
|
+
# @return [Boolean] true for an attribute a config can set
|
|
120
|
+
#
|
|
121
|
+
# @api private
|
|
122
|
+
def self.attribute?(name)
|
|
123
|
+
attribute_names.include?(name)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Return the attributes a config can set on a process
|
|
127
|
+
#
|
|
128
|
+
# @return [Array<Symbol>] a process's configurable attributes, and :group
|
|
129
|
+
#
|
|
130
|
+
# @api private
|
|
131
|
+
def self.attribute_names
|
|
132
|
+
Process::CONFIGURABLE_ATTRIBUTES + [:group]
|
|
110
133
|
end
|
|
111
134
|
|
|
112
135
|
# Add a check of the process, or a trigger such as :flapping
|
|
@@ -157,5 +180,70 @@ module Bluepill
|
|
|
157
180
|
def to_process
|
|
158
181
|
Process.new(@name, @watches, @attributes)
|
|
159
182
|
end
|
|
183
|
+
|
|
184
|
+
private
|
|
185
|
+
|
|
186
|
+
# Return the attribute a call names, and the value it sets
|
|
187
|
+
#
|
|
188
|
+
# A name alone with no value reads the attribute. A name ending in `=`,
|
|
189
|
+
# or one alone, sets it to one value, and a name ending in `!` sets it
|
|
190
|
+
# to true with none.
|
|
191
|
+
#
|
|
192
|
+
# @param [Symbol] name the method's name
|
|
193
|
+
# @param [Array<Object>] args the call's arguments
|
|
194
|
+
#
|
|
195
|
+
# @return [Array(Symbol, Boolean), Array(Symbol, Boolean, Object), nil]
|
|
196
|
+
# the attribute's name, whether the call reads it, and the value a
|
|
197
|
+
# setter sets, or nil for a call with the wrong number of values
|
|
198
|
+
#
|
|
199
|
+
# @api private
|
|
200
|
+
def attribute_call(name, args)
|
|
201
|
+
attribute = name.to_s
|
|
202
|
+
key = attribute.sub(/[=!]\z/, "").to_sym
|
|
203
|
+
return [key, true] if args.empty? && key.to_s.eql?(attribute)
|
|
204
|
+
|
|
205
|
+
bang = attribute.end_with?("!")
|
|
206
|
+
value, = args
|
|
207
|
+
[key, false, bang || value] if args.size.equal?(bang ? 0 : 1)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Fail on an attribute a process doesn't have
|
|
211
|
+
#
|
|
212
|
+
# The message names the process and the attribute, and the closest
|
|
213
|
+
# attribute a process has, when there's one close enough.
|
|
214
|
+
#
|
|
215
|
+
# @param [Symbol] key the attribute's name
|
|
216
|
+
#
|
|
217
|
+
# @return [void] it never returns
|
|
218
|
+
#
|
|
219
|
+
# @raise [NoMethodError] always
|
|
220
|
+
#
|
|
221
|
+
# @api private
|
|
222
|
+
def unknown_attribute!(key)
|
|
223
|
+
message = "Unknown attribute '#{key}' for process '#{@name}'"
|
|
224
|
+
suggestion = suggest(key)
|
|
225
|
+
message += ". Did you mean '#{suggestion}'?" if suggestion
|
|
226
|
+
raise NoMethodError.new(message, key) # steep:ignore ArgumentTypeMismatch
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Return the attribute a process has that's closest to a misspelled one
|
|
230
|
+
#
|
|
231
|
+
# The closest is the one the fewest edits away, if that's at most a
|
|
232
|
+
# third of the misspelled name's length. Ruby's did_you_mean would do,
|
|
233
|
+
# but Bundler leaves it out on Rubies before 2.7, so bluepill compares
|
|
234
|
+
# the names itself and suggests the same attribute on every Ruby.
|
|
235
|
+
#
|
|
236
|
+
# @param [Symbol] key the misspelled attribute
|
|
237
|
+
#
|
|
238
|
+
# @return [String, nil] the closest attribute, or nil
|
|
239
|
+
#
|
|
240
|
+
# @api private
|
|
241
|
+
def suggest(key)
|
|
242
|
+
word = key.to_s
|
|
243
|
+
distances = self.class.attribute_names.collect { |name| [Util.levenshtein_distance(word, name.to_s), name.to_s] } #: Array[[Integer, String]]
|
|
244
|
+
closest = distances.min #: [Integer, String]
|
|
245
|
+
distance, name = closest
|
|
246
|
+
name if distance <= word.length / 3
|
|
247
|
+
end
|
|
160
248
|
end
|
|
161
249
|
end
|