bluepill 0.1.7 → 0.2.0

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.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -21
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +100 -20
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +59 -259
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +129 -65
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -118
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -152
  20. data/lib/bluepill/dsl/process_proxy.rb +100 -51
  21. data/lib/bluepill/dsl.rb +0 -23
  22. data/lib/bluepill/group.rb +187 -78
  23. data/lib/bluepill/logger.rb +139 -48
  24. data/lib/bluepill/pid_file.rb +23 -0
  25. data/lib/bluepill/process/attributes.rb +379 -0
  26. data/lib/bluepill/process/children.rb +70 -0
  27. data/lib/bluepill/process/pid.rb +154 -0
  28. data/lib/bluepill/process/state_machine.rb +196 -0
  29. data/lib/bluepill/process/transition.rb +45 -0
  30. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  31. data/lib/bluepill/process/user_commands.rb +71 -0
  32. data/lib/bluepill/process/watches.rb +141 -0
  33. data/lib/bluepill/process.rb +64 -667
  34. data/lib/bluepill/process_commands/execution.rb +89 -0
  35. data/lib/bluepill/process_commands/hooks.rb +186 -0
  36. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  37. data/lib/bluepill/process_commands.rb +106 -174
  38. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  39. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  40. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  41. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  42. data/lib/bluepill/process_conditions/http.rb +48 -45
  43. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  44. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  45. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  46. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  47. data/lib/bluepill/process_conditions.rb +3 -2
  48. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  49. data/lib/bluepill/process_journal/lock.rb +106 -0
  50. data/lib/bluepill/process_journal.rb +126 -233
  51. data/lib/bluepill/process_statistics.rb +2 -2
  52. data/lib/bluepill/socket.rb +60 -20
  53. data/lib/bluepill/system/command_setup.rb +98 -0
  54. data/lib/bluepill/system/commands.rb +219 -248
  55. data/lib/bluepill/system/daemon.rb +161 -0
  56. data/lib/bluepill/system/ps.rb +85 -0
  57. data/lib/bluepill/system/ps_table.rb +163 -0
  58. data/lib/bluepill/system.rb +45 -126
  59. data/lib/bluepill/trigger.rb +52 -25
  60. data/lib/bluepill/triggers/flapping.rb +8 -16
  61. data/lib/bluepill/units.rb +126 -0
  62. data/lib/bluepill/util/inflections.rb +34 -0
  63. data/lib/bluepill/util/levenshtein.rb +53 -0
  64. data/lib/bluepill/util/rotational_array.rb +1 -1
  65. data/lib/bluepill/version.rb +13 -12
  66. data/lib/bluepill.rb +27 -35
  67. data/sig/bluepill/application.rbs +257 -0
  68. data/sig/bluepill/checks.rbs +203 -0
  69. data/sig/bluepill/dsl.rbs +126 -0
  70. data/sig/bluepill/logger.rbs +104 -0
  71. data/sig/bluepill/process.rbs +542 -0
  72. data/sig/bluepill/system.rbs +316 -0
  73. data/sig/bluepill.rbs +47 -994
  74. metadata +39 -49
  75. data/lib/bluepill/clock.rb +0 -48
  76. data/lib/bluepill/deprecation.rb +0 -74
  77. data/lib/bluepill/trigger/custom.rb +0 -147
  78. data/lib/bluepill/util/log_error.rb +0 -29
@@ -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. A process that monitors its children gets a factory of its own for
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,111 +20,54 @@ module Bluepill
15
20
  # @api private
16
21
  attr_reader :attributes
17
22
 
18
- # The warning about a pre_start_command, which bluepill 0.2.0 limits
19
- #
20
- # @api private
21
- PRE_START_COMMAND = 'bluepill 0.2.0 kills a pre_start_command that runs longer than hook_timeout, 30 seconds by default, ' \
22
- "and starts the process even when the command can't run, where 0.1 waits for it however long it takes. " \
23
- 'Set hook_timeout, which 0.1 ignores, if it can take longer.'.freeze
24
-
25
- # The warning about a process with a uid and no gid, which bluepill
26
- # 0.2.0 runs in the user's groups instead of root's
27
- #
28
- # @api private
29
- UID_WITHOUT_GID = 'bluepill 0.2.0 runs a process that sets uid but not gid in the primary group and the other groups ' \
30
- "of that user, where 0.1 keeps it in root's groups. Set gid and supplementary_groups to the groups it needs.".freeze
31
-
32
- # The warning about a process with a uid and a gid, which bluepill 0.2.0
33
- # also runs in the user's other groups
34
- #
35
- # @api private
36
- UID_WITH_GID = 'bluepill 0.2.0 also runs a process that sets uid in every other group that user is in, ' \
37
- 'where 0.1 gives it only its gid and supplementary_groups. ' \
38
- "Make sure the user's groups don't give it access it shouldn't have.".freeze
39
-
40
- # The warning about a process that monitors its children and runs as
41
- # another user, whose children's commands bluepill 0.2.0 runs as that
42
- # user too
43
- #
44
- # @api private
45
- CHILDREN_USER = "bluepill 0.2.0 runs the commands of a monitor_children process's children, such as their stop_command, " \
46
- "as the parent's uid, gid and supplementary_groups, where 0.1 runs them as root. " \
47
- 'Make sure they work as that user, or run one that needs root with sudo.'.freeze
48
-
49
- # Only the keys matter
50
- @@process_keys = {} #: Hash[Array[String?], nil]
51
- @@pid_files = {} #: Hash[String, nil]
52
-
53
23
  # Create a factory of processes
54
24
  #
55
25
  # @param [Hash{Symbol => Object}] attributes the attributes that each
56
26
  # process starts with
57
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
58
30
  #
59
31
  # @return [void]
60
32
  #
61
33
  # @api private
62
- def initialize(attributes, process_block)
34
+ def initialize(attributes, process_block, registry = Registry.new)
63
35
  @attributes = attributes
64
36
  @process_block = process_block
37
+ @registry = registry
65
38
  end
66
39
 
67
40
  # Create a process from the factory's block, and check its config
68
41
  #
69
- # A process without a pid_file gets a default one in pids_dir.
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.
70
45
  #
71
46
  # @param [String] name the process's name
72
47
  # @param [String] pids_dir the directory of the default pid file
73
48
  #
74
- # @return [Process] the process
49
+ # @return [Array(Process, Object)] the process, and the group that its
50
+ # block set, or nil for none
75
51
  #
76
52
  # @api private
77
53
  def create_process(name, pids_dir)
78
- default_pid_file = assign_default_pid_file(name, pids_dir)
54
+ attributes = @attributes.dup
55
+ default_pid_file = assign_default_pid_file(attributes, name, pids_dir)
79
56
 
80
- process = ProcessProxy.new(name, @attributes, @process_block)
81
- child_process_block = @attributes.delete(:child_process_block)
82
- @attributes[:child_process_factory] = ProcessFactory.new(@attributes, child_process_block) if @attributes[:monitor_children]
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]
83
60
 
84
61
  validate_process! process
85
62
  warn_unless_pid_known(process, default_pid_file)
86
- warn_of_0_2_changes
87
- process.to_process
63
+ [process.to_process, attributes[:group]]
88
64
  end
89
65
 
90
- # Create the process of a child that bluepill found
91
- #
92
- # The child gets its parent's grace times, and its state from whether
93
- # it's running.
94
- #
95
- # @param [String] name the child's name
96
- # @param [Integer] pid the child's pid
97
- # @param [Logger] logger the child's logger
98
- #
99
- # @return [Process] the child's process
100
- #
101
- # @api private
102
- def create_child_process(name, pid, logger)
103
- attributes = {} #: Hash[Symbol, untyped]
104
- # A grace time the parent leaves unset keeps the Process default
105
- [:start_grace_time, :stop_grace_time, :restart_grace_time].each do |a|
106
- attributes[a] = @attributes.fetch(a) if @attributes.key?(a)
107
- end
108
- attributes[:actual_pid] = pid
109
- attributes[:logger] = logger
110
-
111
- child = ProcessProxy.new(name, attributes, @process_block)
112
- validate_child_process! child
113
- process = child.to_process
114
-
115
- process.determine_initial_state
116
- process
117
- end
118
-
119
- protected
66
+ protected
120
67
 
121
68
  # Set the default pid file, unless the process sets its own
122
69
  #
70
+ # @param [Hash{Symbol => Object}] attributes the process's attributes
123
71
  # @param [String] process_name the process's name
124
72
  # @param [String] pids_dir the directory of the pid file
125
73
  #
@@ -127,18 +75,15 @@ module Bluepill
127
75
  # sets its own
128
76
  #
129
77
  # @api private
130
- def assign_default_pid_file(process_name, pids_dir)
131
- return if @attributes.key?(:pid_file)
78
+ def assign_default_pid_file(attributes, process_name, pids_dir)
79
+ return if attributes.key?(:pid_file)
132
80
 
133
- group_name = @attributes[:group]
134
- default_pid_name = [group_name, process_name].compact.join('_').gsub(/[^A-Za-z0-9_-]/, '_')
135
- @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")
136
83
  end
137
84
 
138
85
  # Check a process's config, and exit on a mistake
139
86
  #
140
- # The process shares its attributes hash with the factory.
141
- #
142
87
  # @param [ProcessProxy] process the process's proxy
143
88
  #
144
89
  # @return [void]
@@ -147,8 +92,8 @@ module Bluepill
147
92
  def validate_process!(process)
148
93
  validate_unique_process!(process)
149
94
  validate_required_attributes!(process)
150
- validate_unique_pid_file!
151
- validate_stop_signals!
95
+ validate_unique_pid_file!(process)
96
+ validate_stop_signals!(process)
152
97
  end
153
98
 
154
99
  # Exit if another process in the group has the same name
@@ -159,15 +104,12 @@ module Bluepill
159
104
  #
160
105
  # @api private
161
106
  def validate_unique_process!(process)
162
- # A process name may contain ':', so the key keeps the parts apart.
163
- # Like Application#add_process, it treats Symbols and Strings alike.
164
- process_key = [attributes[:group], process.name].collect { |part| part && part.to_s }
165
- if @@process_keys.key?(process_key)
166
- message = "Config Error: You have two entries for the process name '#{process.name}'"
167
- message += " in the group '#{attributes.fetch(:group)}'" if attributes.key?(:group)
168
- config_error!(message)
169
- end
170
- @@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)
171
113
  end
172
114
 
173
115
  # Exit unless the process has a start_command
@@ -178,48 +120,77 @@ module Bluepill
178
120
  #
179
121
  # @api private
180
122
  def validate_required_attributes!(process)
181
- return if attributes.key?(:start_command)
123
+ return if process.attributes.key?(:start_command)
182
124
 
183
125
  config_error!("Config Error: You must specify a start_command for '#{process.name}'")
184
126
  end
185
127
 
186
128
  # Exit if another process has the same pid file
187
129
  #
130
+ # @param [ProcessProxy] process the process's proxy
131
+ #
188
132
  # @return [void]
189
133
  #
190
134
  # @api private
191
- def validate_unique_pid_file!
192
- pid_key = attributes.fetch(:pid_file).strip
193
- config_error!("Config Error: You have two entries with the pid file: #{pid_key}") if @@pid_files.key?(pid_key)
194
- @@pid_files[pid_key] = nil
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}")
195
140
  end
196
141
 
197
- # Exit unless stop_signals alternates signals and delays
142
+ # Exit on a mistake in stop_signals
198
143
  #
199
144
  # The signals and delays must alternate, starting and ending with a
200
- # signal, and stop_grace_time must be longer than the delays. Checks the
201
- # more specific problems before the 'odd number' message.
145
+ # signal, and stop_grace_time must be longer than the delays.
146
+ #
147
+ # @param [ProcessProxy] process the process's proxy
202
148
  #
203
149
  # @return [void]
204
150
  #
205
151
  # @api private
206
- def validate_stop_signals!
207
- stop_signals = attributes[:stop_signals]
152
+ def validate_stop_signals!(process)
153
+ stop_signals = process.attributes[:stop_signals]
208
154
  return if stop_signals.nil?
209
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)
210
172
  stop_signals.each_with_index do |value, i|
211
- expected, label = i.even? ? [Symbol, 'a symbol (signal)'] : [Integer, 'a number (delay)']
173
+ expected, label = i.even? ? [Symbol, "a symbol (signal)"] : [Integer, "a number (delay)"]
212
174
  next if value.is_a?(expected)
213
175
 
214
176
  config_error!("Config Error: Invalid stop_signals! Expected #{label} at position #{i} instead of '#{value}'.")
215
177
  end
216
- config_error!('Config Error: Invalid stop_signals! Expected an odd number of elements.') unless stop_signals.size.odd?
178
+ config_error!("Config Error: Invalid stop_signals! Expected an odd number of elements.") unless stop_signals.size.odd?
179
+ end
217
180
 
218
- stop_grace_time = attributes[:stop_grace_time]
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)
219
190
  delays = stop_signals.select.with_index { |_, i| i.odd? }
220
191
  return unless stop_grace_time.nil? || stop_grace_time <= delays.inject(0, :+)
221
192
 
222
- config_error!('Config Error: Stop_grace_time should be greater than the sum of stop_signals delays!')
193
+ config_error!("Config Error: Stop_grace_time should be greater than the sum of stop_signals delays!")
223
194
  end
224
195
 
225
196
  # Warn when bluepill can't find the process's pid
@@ -235,42 +206,15 @@ module Bluepill
235
206
  #
236
207
  # @api private
237
208
  def warn_unless_pid_known(process, default_pid_file)
209
+ attributes = process.attributes
238
210
  return if attributes[:daemonize] || attributes[:pid_command] || !attributes.fetch(:pid_file).eql?(default_pid_file)
239
211
 
240
212
  warning = "Config Warning: '#{process.name}' sets no daemonize, pid_file or pid_command, " \
241
213
  "so bluepill cannot find its pid unless start_command writes it to #{default_pid_file}. " \
242
- 'Set daemonize = true if start_command runs in the foreground.'
214
+ "Set daemonize = true if start_command runs in the foreground."
243
215
  $stderr.puts warning
244
216
  end
245
217
 
246
- # Warn about what bluepill 0.2.0 does differently with the process
247
- #
248
- # @return [void]
249
- #
250
- # @api private
251
- def warn_of_0_2_changes
252
- Deprecation.warn(:pre_start_command, PRE_START_COMMAND) if attributes[:pre_start_command]
253
- return unless attributes[:uid] || attributes[:gid] || attributes[:supplementary_groups]
254
- # Bluepill only switches users and groups when it runs as root
255
- return unless ::Process.euid.zero?
256
-
257
- warn_of_0_2_groups if attributes[:uid]
258
- Deprecation.warn(:monitor_children_user, CHILDREN_USER) if attributes[:monitor_children]
259
- end
260
-
261
- # Warn that bluepill 0.2.0 runs a process with a uid in the user's groups
262
- #
263
- # @return [void]
264
- #
265
- # @api private
266
- def warn_of_0_2_groups
267
- if attributes[:gid]
268
- Deprecation.warn(:uid_with_gid, UID_WITH_GID)
269
- else
270
- Deprecation.warn(:uid_without_gid, UID_WITHOUT_GID)
271
- end
272
- end
273
-
274
218
  # Print a config error and exit with status 6
275
219
  #
276
220
  # @param [String] message the error
@@ -282,20 +226,5 @@ module Bluepill
282
226
  $stderr.puts message
283
227
  exit(6)
284
228
  end
285
-
286
- # Exit unless a monitored child has a stop_command
287
- #
288
- # @param [ProcessProxy] child the child's proxy
289
- #
290
- # @return [void]
291
- #
292
- # @api private
293
- def validate_child_process!(child)
294
- return if child.attributes.key?(:stop_command)
295
-
296
- $stderr.puts "Config Error: Invalid child process monitor for #{child.name}"
297
- $stderr.puts 'You must specify a stop command to monitor child processes.'
298
- exit(6)
299
- end
300
229
  end
301
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
  #
@@ -14,12 +16,6 @@ module Bluepill
14
16
  #
15
17
  # @api public
16
18
  class ProcessProxy
17
- # The attributes that bluepill 0.2.0 adds, which it knows along with
18
- # the ones bluepill 0.1 has
19
- #
20
- # @api private
21
- ATTRIBUTES_ADDED_IN_0_2 = [:post_start_command, :pre_stop_command, :post_stop_command, :notify_command, :hook_timeout].freeze
22
-
23
19
  # Return the attributes that the block set, by name
24
20
  #
25
21
  # @return [Hash{Symbol => Object}] the attributes
@@ -64,7 +60,8 @@ module Bluepill
64
60
  #
65
61
  # A name ending in `=`, or one given a value, sets the attribute. A name
66
62
  # ending in `!` sets it to true. A name alone reads an attribute that's
67
- # 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.
68
65
  #
69
66
  # @example
70
67
  # process.start_command = '/usr/bin/my_server'
@@ -77,27 +74,21 @@ module Bluepill
77
74
  #
78
75
  # @return [Object] the value that was set or read
79
76
  #
80
- # @raise [NoMethodError] for an attribute that isn't set, or more than one
81
- # value
77
+ # @raise [NoMethodError] for an attribute a process doesn't have, one
78
+ # that isn't set yet, or more than one value
82
79
  #
83
80
  # @api public
84
81
  def method_missing(name, *args)
85
- attribute = name.to_s
86
- case args.size
87
- when 1
88
- value, = args
89
- set_attribute(attribute.chomp('=').to_sym, value)
90
- when 0
91
- if attribute.end_with?('!')
92
- set_attribute(attribute.chomp('!').to_sym, true)
93
- elsif @attributes.key?(name)
94
- @attributes.fetch(name)
95
- else
96
- super
97
- end
98
- else
99
- 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
100
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
101
92
  end
102
93
 
103
94
  # Return whether the proxy answers a name through method_missing
@@ -107,12 +98,38 @@ module Bluepill
107
98
  # @param [Symbol] name the method's name
108
99
  # @param [Boolean, nil] _include_private ignored
109
100
  #
110
- # @return [Boolean] true for a setter, a `name!`, or an attribute that's
111
- # 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
112
103
  #
113
104
  # @api private
114
105
  def respond_to_missing?(name, _include_private)
115
- ['=', '!'].include?(name[-1]) || @attributes.key?(name)
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]
116
133
  end
117
134
 
118
135
  # Add a check of the process, or a trigger such as :flapping
@@ -155,46 +172,78 @@ module Bluepill
155
172
  @attributes[:child_process_block] = child_process_block
156
173
  end
157
174
 
158
- # Return whether bluepill 0.2.0 knows a process attribute
175
+ # Create the process that the block described
159
176
  #
160
- # It knows a process's configurable attributes, the group, and the
161
- # hooks it adds. It fails the config on any other.
177
+ # @return [Process] the process
162
178
  #
163
- # @param [Symbol] name the attribute's name
179
+ # @api private
180
+ def to_process
181
+ Process.new(@name, @watches, @attributes)
182
+ end
183
+
184
+ private
185
+
186
+ # Return the attribute a call names, and the value it sets
164
187
  #
165
- # @return [Boolean]
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
166
198
  #
167
199
  # @api private
168
- def self.known_in_0_2?(name)
169
- Process::CONFIGURABLE_ATTRIBUTES.include?(name) || name.eql?(:group) || ATTRIBUTES_ADDED_IN_0_2.include?(name)
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)
170
208
  end
171
209
 
172
- # Create the process that the block described
210
+ # Fail on an attribute a process doesn't have
173
211
  #
174
- # @return [Process] the process
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
175
220
  #
176
221
  # @api private
177
- def to_process
178
- Process.new(@name, @watches, @attributes)
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
179
227
  end
180
228
 
181
- private
182
-
183
- # Set an attribute, and warn about one that bluepill 0.2.0 doesn't know
229
+ # Return the attribute a process has that's closest to a misspelled one
184
230
  #
185
- # @param [Symbol] key the attribute's name
186
- # @param [Object] value its value
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.
187
235
  #
188
- # @return [Object] the value
236
+ # @param [Symbol] key the misspelled attribute
237
+ #
238
+ # @return [String, nil] the closest attribute, or nil
189
239
  #
190
240
  # @api private
191
- def set_attribute(key, value)
192
- unless self.class.known_in_0_2?(key)
193
- message = "bluepill 0.2.0 fails the config on the process attribute '#{key}', which bluepill doesn't know, " \
194
- 'where 0.1 ignores it. Remove it, or fix its name.'
195
- Deprecation.warn(message, message)
196
- end
197
- @attributes[key] = value
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
198
247
  end
199
248
  end
200
249
  end
data/lib/bluepill/dsl.rb CHANGED
@@ -26,31 +26,8 @@ module Bluepill
26
26
  # @api public
27
27
  def self.application(app_name, options = {}, &block)
28
28
  app_proxy = AppProxy.new(app_name, options)
29
- warn_unsupported_ruby
30
29
  # instance_eval also passes the proxy to a block that takes an argument
31
30
  app_proxy.instance_eval(&block)
32
31
  app_proxy.app.load
33
32
  end
34
-
35
- # Warn on a Ruby that bluepill 0.2 doesn't run on
36
- #
37
- # Bluepill 0.2.0 needs Ruby 2.0 or JRuby 9.0, and later 0.2 releases need
38
- # Ruby 2.7 or JRuby 9.4. JRuby 1.7 can run Ruby 2.0 code, so it's told
39
- # apart by its own version.
40
- #
41
- # @param [String] ruby_version the Ruby version, such as "1.9.3"
42
- # @param [String, nil] jruby_version the JRuby version, such as "1.7.27",
43
- # or nil on another Ruby
44
- #
45
- # @return [void]
46
- #
47
- # @api private
48
- def self.warn_unsupported_ruby(ruby_version = RUBY_VERSION, jruby_version = defined?(JRUBY_VERSION) && JRUBY_VERSION)
49
- # Only Ruby 1.9 and JRuby 1.7 have a major version of 1
50
- return unless ruby_version.start_with?('1.') || (jruby_version && jruby_version.start_with?('1.'))
51
-
52
- message = "bluepill 0.2.0 needs Ruby 2.0 or JRuby 9.0, and later 0.2 releases need Ruby 2.7 or JRuby 9.4, so it won't run on Ruby #{ruby_version}" \
53
- "#{" (JRuby #{jruby_version})" if jruby_version}. Stay on bluepill 0.1 until you upgrade Ruby."
54
- Deprecation.warn(message, message)
55
- end
56
33
  end