bluepill 0.2.9 → 0.3.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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +100 -2
  3. data/README.md +12 -7
  4. data/bin/bluepill +8 -2
  5. data/bluepill.gemspec +1 -1
  6. data/lib/bluepill/application/lifecycle.rb +44 -34
  7. data/lib/bluepill/application/listener.rb +1 -1
  8. data/lib/bluepill/application/previous_daemon.rb +12 -14
  9. data/lib/bluepill/application.rb +33 -29
  10. data/lib/bluepill/base_directory.rb +18 -20
  11. data/lib/bluepill/check_registry.rb +104 -0
  12. data/lib/bluepill/clock.rb +1 -3
  13. data/lib/bluepill/condition_watch.rb +45 -54
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +32 -37
  16. data/lib/bluepill/dsl/app_proxy.rb +68 -64
  17. data/lib/bluepill/dsl/attributes.rb +135 -0
  18. data/lib/bluepill/dsl/process_factory/child_factory.rb +80 -0
  19. data/lib/bluepill/dsl/process_factory/registry.rb +6 -26
  20. data/lib/bluepill/dsl/process_factory.rb +83 -84
  21. data/lib/bluepill/dsl/process_proxy.rb +61 -168
  22. data/lib/bluepill/dsl.rb +16 -15
  23. data/lib/bluepill/errors.rb +190 -0
  24. data/lib/bluepill/group.rb +14 -37
  25. data/lib/bluepill/logger.rb +55 -97
  26. data/lib/bluepill/process/attributes.rb +66 -32
  27. data/lib/bluepill/process/children.rb +4 -6
  28. data/lib/bluepill/process/pid.rb +41 -35
  29. data/lib/bluepill/process/state_machine.rb +15 -59
  30. data/lib/bluepill/process/stop_signals.rb +120 -0
  31. data/lib/bluepill/process/transition.rb +1 -3
  32. data/lib/bluepill/process/transition_callbacks.rb +2 -16
  33. data/lib/bluepill/process/triggers.rb +90 -0
  34. data/lib/bluepill/process/user_commands.rb +1 -1
  35. data/lib/bluepill/process/watches.rb +9 -23
  36. data/lib/bluepill/process.rb +55 -52
  37. data/lib/bluepill/process_commands/execution.rb +14 -22
  38. data/lib/bluepill/process_commands/hooks.rb +1 -31
  39. data/lib/bluepill/process_commands/stop_signals.rb +12 -17
  40. data/lib/bluepill/process_commands.rb +10 -8
  41. data/lib/bluepill/process_conditions/always_true.rb +2 -6
  42. data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
  43. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
  44. data/lib/bluepill/process_conditions/file_time.rb +8 -9
  45. data/lib/bluepill/process_conditions/http.rb +28 -67
  46. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  47. data/lib/bluepill/process_conditions/process_condition.rb +29 -47
  48. data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
  49. data/lib/bluepill/process_conditions.rb +5 -14
  50. data/lib/bluepill/process_journal/cleanup.rb +5 -53
  51. data/lib/bluepill/process_journal/config.rb +73 -0
  52. data/lib/bluepill/process_journal/entries.rb +128 -0
  53. data/lib/bluepill/process_journal/journal.rb +69 -0
  54. data/lib/bluepill/process_journal/kind.rb +60 -0
  55. data/lib/bluepill/process_journal/lock.rb +9 -4
  56. data/lib/bluepill/process_journal.rb +13 -324
  57. data/lib/bluepill/process_statistics.rb +3 -7
  58. data/lib/bluepill/socket.rb +19 -17
  59. data/lib/bluepill/system/command_options.rb +135 -0
  60. data/lib/bluepill/system/command_result.rb +73 -0
  61. data/lib/bluepill/system/command_setup.rb +3 -4
  62. data/lib/bluepill/system/commands.rb +21 -18
  63. data/lib/bluepill/system/daemon.rb +19 -14
  64. data/lib/bluepill/system/files.rb +35 -0
  65. data/lib/bluepill/system/ps.rb +20 -17
  66. data/lib/bluepill/system/ps_table.rb +7 -9
  67. data/lib/bluepill/system/usage.rb +155 -0
  68. data/lib/bluepill/system.rb +7 -192
  69. data/lib/bluepill/trigger.rb +18 -69
  70. data/lib/bluepill/triggers/flapping.rb +12 -8
  71. data/lib/bluepill/units.rb +7 -21
  72. data/lib/bluepill/version.rb +12 -22
  73. data/lib/bluepill.rb +1 -1
  74. data/sig/bluepill/application.rbs +11 -12
  75. data/sig/bluepill/attributes.rbs +48 -0
  76. data/sig/bluepill/checks.rbs +40 -40
  77. data/sig/bluepill/dsl.rbs +97 -60
  78. data/sig/bluepill/errors.rbs +48 -0
  79. data/sig/bluepill/logger.rbs +15 -31
  80. data/sig/bluepill/process.rbs +39 -305
  81. data/sig/bluepill/process_commands.rbs +235 -0
  82. data/sig/bluepill/stop_signals.rbs +21 -0
  83. data/sig/bluepill/system.rbs +154 -99
  84. data/sig/bluepill/version.rbs +17 -0
  85. data/sig/bluepill.rbs +1 -17
  86. data/sig/manifest.yaml +1 -0
  87. metadata +21 -8
  88. data/lib/bluepill/deprecation/custom_checks.rb +0 -193
  89. data/lib/bluepill/deprecation/dsl.rb +0 -141
  90. data/lib/bluepill/deprecation/options.rb +0 -140
  91. data/lib/bluepill/deprecation.rb +0 -92
  92. data/lib/bluepill/dsl/process_factory/children.rb +0 -99
  93. data/sig/bluepill/deprecation.rbs +0 -87
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bluepill/dsl/process_factory/children"
3
+ require "bluepill/dsl/process_factory/child_factory"
4
4
  require "bluepill/dsl/process_factory/registry"
5
5
 
6
6
  module Bluepill
@@ -9,19 +9,10 @@ module Bluepill
9
9
  # A factory turns a process block into a {Process}, after it checks the
10
10
  # config for mistakes, such as two processes with the same name or pid
11
11
  # file, which it looks up in the application's {Registry}. A process that
12
- # monitors its children gets a factory of its own for them.
12
+ # monitors its children gets a {ChildFactory} for them.
13
13
  #
14
14
  # @api private
15
15
  class ProcessFactory
16
- include Children
17
-
18
- # Return the attributes that each process starts with
19
- #
20
- # @return [Hash{Symbol => Object}] the attributes
21
- #
22
- # @api private
23
- attr_reader :attributes
24
-
25
16
  # Create a factory of processes
26
17
  #
27
18
  # @param [Hash{Symbol => Object}] attributes the attributes that each
@@ -41,67 +32,71 @@ module Bluepill
41
32
 
42
33
  # Create a process from the factory's block, and check its config
43
34
  #
44
- # The process starts with a copy of the factory's attributes, so the
45
- # factory's own stay as they were. A process without a pid_file gets a
46
- # default one in pids_dir.
35
+ # The process starts with the factory's attributes, and a default
36
+ # pid_file in pids_dir unless they set one, and its block can override
37
+ # them. It gets the application's logger, prefixed with its group's
38
+ # name and its own, as its group's logger is with the group's name.
47
39
  #
48
40
  # @param [String] name the process's name
49
41
  # @param [String] pids_dir the directory of the default pid file
42
+ # @param [Logger] logger the application's logger
50
43
  #
51
44
  # @return [Array(Process, Object)] the process, and the group that its
52
45
  # block set, or nil for none
53
46
  #
47
+ # @raise [ConfigError] for a mistake in the process's config
48
+ #
54
49
  # @api private
55
- def create_process(name, pids_dir)
56
- attributes = @attributes.dup
57
- default_pid_file = assign_default_pid_file(attributes, name, pids_dir)
50
+ def create_process(name, pids_dir, logger:)
51
+ default_pid_file = default_pid_file(name, pids_dir)
52
+ process = ProcessProxy.new(name, {pid_file: default_pid_file}.merge(@attributes), @process_block)
58
53
 
59
- process = ProcessProxy.new(name, attributes, @process_block)
60
- Deprecation.check_on_start_timeout(name, attributes)
61
- child_process_block = attributes.delete(:child_process_block)
62
- attributes[:child_process_factory] = child_factory(name, attributes, child_process_block) if attributes[:monitor_children]
63
-
64
- validate_process! process
54
+ validate_process!(process)
65
55
  warn_unless_pid_known(process, default_pid_file)
66
- [process.to_process, attributes[:group]]
56
+ group = process.group
57
+ process_logger = logger.prefix_with(group&.to_s).prefix_with(name)
58
+ [process.to_process(logger: process_logger, **children_attributes(process)), group]
67
59
  end
68
60
 
69
61
  protected
70
62
 
71
- # Create the factory of a process's children, and check their block
63
+ # Return the pid file of a process that sets none
72
64
  #
73
- # @param [String] name the process's name
74
- # @param [Hash{Symbol => Object}] attributes the process's attributes
75
- # @param [Proc, nil] child_process_block the block that configures each
76
- # child
65
+ # It's in pids_dir, named after the process and its group, if the
66
+ # factory's attributes give one.
77
67
  #
78
- # @return [ProcessFactory] the children's factory
68
+ # @param [String] process_name the process's name
69
+ # @param [String] pids_dir the directory of the pid file
70
+ #
71
+ # @return [String] the pid file
79
72
  #
80
73
  # @api private
81
- def child_factory(name, attributes, child_process_block)
82
- factory = ProcessFactory.new(attributes, child_process_block)
83
- factory.check_children(name)
84
- factory
74
+ def default_pid_file(process_name, pids_dir)
75
+ default_pid_name = [@attributes[:group], process_name].compact.join("_").gsub(/[^A-Za-z0-9_-]/, "_")
76
+ File.join(pids_dir, "#{default_pid_name}.pid")
85
77
  end
86
78
 
87
- # Set the default pid file, unless the process sets its own
79
+ # Return the attributes that say whether a process monitors its children
88
80
  #
89
- # @param [Hash{Symbol => Object}] attributes the process's attributes
90
- # @param [String] process_name the process's name
91
- # @param [String] pids_dir the directory of the pid file
81
+ # A process whose block calls monitor_children gets a factory of its
82
+ # children.
83
+ #
84
+ # @param [ProcessProxy] process the process's proxy
85
+ #
86
+ # @return [Hash{Symbol => Object}] monitor_children and the
87
+ # child_process_factory, or nil for a process that doesn't monitor
88
+ # its children
92
89
  #
93
- # @return [String, nil] the default pid file, or nil when the process
94
- # sets its own
90
+ # @raise [ConfigError] for a mistake in the children's config
95
91
  #
96
92
  # @api private
97
- def assign_default_pid_file(attributes, process_name, pids_dir)
98
- return if attributes.key?(:pid_file)
99
-
100
- default_pid_name = [attributes[:group], process_name].compact.join("_").gsub(/[^A-Za-z0-9_-]/, "_")
101
- attributes[:pid_file] = File.join(pids_dir, "#{default_pid_name}.pid")
93
+ def children_attributes(process)
94
+ child_process_block = process.child_process_block
95
+ factory = ChildFactory.new(process.name, process.attributes, child_process_block) if child_process_block
96
+ {monitor_children: !factory.nil?, child_process_factory: factory}
102
97
  end
103
98
 
104
- # Check a process's config, and exit on a mistake
99
+ # Check a process's config, and fail on a mistake
105
100
  #
106
101
  # @param [ProcessProxy] process the process's proxy
107
102
  #
@@ -113,9 +108,10 @@ module Bluepill
113
108
  validate_required_attributes!(process)
114
109
  validate_unique_pid_file!(process)
115
110
  validate_stop_signals!(process)
111
+ validate_on_start_timeout!(process)
116
112
  end
117
113
 
118
- # Exit if another process in the group has the same name
114
+ # Fail if another process in the group has the same name
119
115
  #
120
116
  # @param [ProcessProxy] process the process's proxy
121
117
  #
@@ -131,7 +127,7 @@ module Bluepill
131
127
  config_error!(message)
132
128
  end
133
129
 
134
- # Exit unless the process has a start_command
130
+ # Fail unless the process has a start_command
135
131
  #
136
132
  # @param [ProcessProxy] process the process's proxy
137
133
  #
@@ -144,7 +140,7 @@ module Bluepill
144
140
  config_error!("Config Error: You must specify a start_command for '#{process.name}'")
145
141
  end
146
142
 
147
- # Exit if another process has the same pid file
143
+ # Fail if another process has the same pid file
148
144
  #
149
145
  # @param [ProcessProxy] process the process's proxy
150
146
  #
@@ -158,58 +154,61 @@ module Bluepill
158
154
  config_error!("Config Error: You have two entries with the pid file: #{pid_file}")
159
155
  end
160
156
 
161
- # Exit on a mistake in stop_signals
157
+ # Parse a process's stop_signals, and fail on a mistake
162
158
  #
163
159
  # The signals and delays must alternate, starting and ending with a
164
- # signal, and stop_grace_time must be longer than the delays.
160
+ # signal, and stop_grace_time must be longer than the delays. The
161
+ # proxy keeps the parsed {Process::StopSignals}, for the process.
165
162
  #
166
163
  # @param [ProcessProxy] process the process's proxy
167
164
  #
168
165
  # @return [void]
169
166
  #
167
+ # @raise [ConfigError] for a mistake in stop_signals
168
+ #
170
169
  # @api private
171
170
  def validate_stop_signals!(process)
172
- stop_signals = process.attributes[:stop_signals]
173
- return if stop_signals.nil?
171
+ attributes = process.attributes
172
+ signals = attributes[:stop_signals]
173
+ return if signals.nil?
174
174
 
175
- validate_stop_signal_order!(stop_signals)
176
- validate_stop_grace_time!(stop_signals, process.attributes[:stop_grace_time])
175
+ stop_signals = Process::StopSignals.new(signals)
176
+ validate_stop_grace_time!(stop_signals, attributes[:stop_grace_time])
177
+ attributes[:stop_signals] = stop_signals
177
178
  end
178
179
 
179
- # Exit unless stop_signals alternates signals and delays
180
- #
181
- # The signals and delays must alternate, starting and ending with a
182
- # signal. Checks the more specific problems before the 'odd number'
183
- # message.
180
+ # Fail unless stop_grace_time is longer than the stop_signals delays
184
181
  #
185
- # @param [Array<Symbol, Integer>] stop_signals the signals and delays
182
+ # @param [Process::StopSignals] stop_signals the signals and delays
183
+ # @param [Integer, Float, nil] stop_grace_time the process's
184
+ # stop_grace_time
186
185
  #
187
186
  # @return [void]
188
187
  #
188
+ # @raise [ConfigError] unless stop_grace_time is longer
189
+ #
189
190
  # @api private
190
- def validate_stop_signal_order!(stop_signals)
191
- stop_signals.each_with_index do |value, i|
192
- expected, label = i.even? ? [Symbol, "a symbol (signal)"] : [Integer, "a number (delay)"]
193
- next if value.is_a?(expected)
194
-
195
- config_error!("Config Error: Invalid stop_signals! Expected #{label} at position #{i} instead of '#{value}'.")
196
- end
197
- config_error!("Config Error: Invalid stop_signals! Expected an odd number of elements.") unless stop_signals.size.odd?
191
+ def validate_stop_grace_time!(stop_signals, stop_grace_time)
192
+ return if stop_grace_time && stop_grace_time > stop_signals.total_delay
193
+
194
+ config_error!("Config Error: Stop_grace_time should be greater than the sum of stop_signals delays!")
198
195
  end
199
196
 
200
- # Exit unless stop_grace_time is longer than the stop_signals delays
197
+ # Fail unless on_start_timeout is an event of the state machine
201
198
  #
202
- # @param [Array<Symbol, Integer>] stop_signals the signals and delays
203
- # @param [Integer, nil] stop_grace_time the process's stop_grace_time
199
+ # @param [ProcessProxy] process the process's proxy
204
200
  #
205
201
  # @return [void]
206
202
  #
207
203
  # @api private
208
- def validate_stop_grace_time!(stop_signals, stop_grace_time)
209
- delays = stop_signals.select.with_index { |_, i| i.odd? }
210
- return unless stop_grace_time.nil? || stop_grace_time <= delays.sum
204
+ def validate_on_start_timeout!(process)
205
+ return unless process.attributes.key?(:on_start_timeout)
211
206
 
212
- config_error!("Config Error: Stop_grace_time should be greater than the sum of stop_signals delays!")
207
+ events = Process::StateMachine::TRANSITIONS.keys
208
+ event = process.on_start_timeout
209
+ return if events.include?(event)
210
+
211
+ config_error!("Config Error: on_start_timeout for '#{process.name}' must be one of #{events.collect(&:inspect).join(", ")}, not #{event.inspect}")
213
212
  end
214
213
 
215
214
  # Warn when bluepill can't find the process's pid
@@ -218,15 +217,14 @@ module Bluepill
218
217
  # so it cannot find the pid of any other process that uses it.
219
218
  #
220
219
  # @param [ProcessProxy] process the process's proxy
221
- # @param [String, nil] default_pid_file the default pid file, or nil when
222
- # the process sets its own
220
+ # @param [String] default_pid_file the default pid file
223
221
  #
224
222
  # @return [void]
225
223
  #
226
224
  # @api private
227
225
  def warn_unless_pid_known(process, default_pid_file)
228
226
  attributes = process.attributes
229
- return if attributes[:daemonize] || attributes[:pid_command] || !attributes.fetch(:pid_file).eql?(default_pid_file)
227
+ return if attributes[:daemonize] || attributes[:pid_command] || !process.pid_file.eql?(default_pid_file)
230
228
 
231
229
  warning = "Config Warning: '#{process.name}' sets no daemonize, pid_file or pid_command, " \
232
230
  "so bluepill cannot find its pid unless start_command writes it to #{default_pid_file}. " \
@@ -234,16 +232,17 @@ module Bluepill
234
232
  $stderr.puts warning
235
233
  end
236
234
 
237
- # Print a config error and exit with status 6
235
+ # Fail with a config error
236
+ #
237
+ # Bluepill.application prints its message and exits with status 6.
238
238
  #
239
239
  # @param [String] message the error
240
240
  #
241
241
  # @return [void] it never returns
242
242
  #
243
+ # @raise [ConfigError] always
244
+ #
243
245
  # @api private
244
- def config_error!(message)
245
- $stderr.puts message
246
- exit(6)
247
- end
246
+ def config_error!(message) = raise(ConfigError, message)
248
247
  end
249
248
  end
@@ -1,14 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "did_you_mean"
3
+ require "bluepill/dsl/attributes"
4
+ require "bluepill/process/attributes"
4
5
 
5
6
  module Bluepill
6
7
  # The self of a process block in a config file
7
8
  #
8
- # Any of the process's attributes can be set with `name = value`,
9
- # `name value`, or `name!` for true, and read back with `name`. Bluepill
10
- # 0.3.0 takes only `name = value`, so the others warn. See
11
- # {Process::CONFIGURABLE_ATTRIBUTES} for the attributes a process has.
9
+ # Each of the process's attributes is set with `name = value`, and read
10
+ # back with `name`, which is nil until it's set. See
11
+ # {Process::CONFIGURABLE_ATTRIBUTES} for the attributes a process has, and
12
+ # `group` sets the group it's in.
12
13
  #
13
14
  # @example
14
15
  # app.process('web') do |process|
@@ -19,19 +20,10 @@ module Bluepill
19
20
  #
20
21
  # @api public
21
22
  class ProcessProxy
22
- # Return the attributes that the block set, by name
23
- #
24
- # @return [Hash{Symbol => Object}] the attributes
25
- #
26
- # @api private
27
- attr_reader :attributes
23
+ include DSL::Attributes
28
24
 
29
- # Return the checks that the block added, with their options, by name
30
- #
31
- # @return [Hash{Symbol => Hash{Symbol => Object}}] the checks
32
- #
33
- # @api private
34
- attr_reader :watches
25
+ # monitor_children sets the two attributes it leaves out
26
+ attribute(*(Process::Attributes::CONFIGURABLE_ATTRIBUTES - %i[monitor_children child_process_factory]), :group)
35
27
 
36
28
  # Return the process's name
37
29
  #
@@ -40,130 +32,76 @@ module Bluepill
40
32
  # @api private
41
33
  attr_reader :name
42
34
 
43
- # Create the proxy of a process, and run its block
35
+ # Return the block that monitor_children gave, to configure each child
44
36
  #
45
- # @param [String] process_name the process's name
46
- # @param [Hash{Symbol => Object}] attributes the attributes the process
47
- # starts with, which the block adds to
48
- # @param [Proc] process_block the block that configures the process
49
- #
50
- # @return [void]
37
+ # @return [Proc, nil] the block
51
38
  #
52
39
  # @api private
53
- def initialize(process_name, attributes, process_block)
54
- @name = process_name
55
- @attributes = attributes
56
- @watches = {}
57
-
58
- # instance_eval also passes the proxy to a block that takes an argument
59
- instance_eval(&process_block)
60
- end
40
+ attr_reader :child_process_block
61
41
 
62
- # Set or read an attribute of the process
42
+ # Return the pid of the child that a monitor_children block configures
63
43
  #
64
- # A name ending in `=`, or one given a value, sets the attribute. A name
65
- # ending in `!` sets it to true. A name alone reads an attribute that's
66
- # set. Only the attributes a process has can be set, so a misspelled one
67
- # fails when the config loads instead of being ignored. Bluepill 0.3.0
68
- # removes the `name value` and `name!` forms, so they warn.
44
+ # It's nil when bluepill checks the block as the config loads, before
45
+ # it has found any children.
69
46
  #
70
47
  # @example
71
- # process.start_command = '/usr/bin/my_server'
72
- # process.stop_grace_time 10.seconds # warns
73
- # process.daemonize! # warns
74
- # process.start_command # => "/usr/bin/my_server"
75
- #
76
- # @param [Symbol] name the attribute's name
77
- # @param [Array<Object>] args the value, if it's set
78
- #
79
- # @return [Object] the value that was set or read
48
+ # process.monitor_children do |child|
49
+ # child.stdout = "/var/log/child.#{child.actual_pid}.log"
50
+ # end
80
51
  #
81
- # @raise [NoMethodError] for an attribute a process doesn't have, one
82
- # that isn't set yet, or more than one value
52
+ # @return [Integer, nil] the child's pid, or nil for a process
83
53
  #
84
54
  # @api public
85
- def method_missing(name, *args)
86
- key, reader, value = attribute_call(name, args)
87
- if key
88
- return @attributes.fetch(key) if reader && @attributes.key?(key)
89
-
90
- unknown_attribute!(key) unless self.class.attribute?(key)
91
- Deprecation.check_attribute(name, key) unless reader
92
- return @attributes[key] = value unless reader
93
- end
94
- # A call with the wrong number of values, or the reader of a known
95
- # attribute that isn't set yet
96
- super
97
- end
55
+ attr_reader :actual_pid
98
56
 
99
- # Return whether the proxy answers a name through method_missing
100
- #
101
- # Ruby turns a String name into a Symbol before calling this.
102
- #
103
- # @param [Symbol] name the method's name
104
- # @param [Boolean, nil] _include_private ignored
105
- #
106
- # @return [Boolean] true for the setter or `name!` of an attribute a
107
- # process has, or the reader of one that's set
108
- #
109
- # @api private
110
- def respond_to_missing?(name, _include_private)
111
- attribute = name.to_s
112
- key = attribute.sub(/[=!]\z/, "")
113
- return @attributes.key?(name) if key.eql?(attribute)
114
-
115
- self.class.attribute?(key.to_sym)
116
- end
117
-
118
- # Return whether a process has an attribute that a config can set
119
- #
120
- # These are a process's configurable attributes, and the group it's in.
57
+ # Create the proxy of a process, and run its block
121
58
  #
122
- # @param [Symbol] name the attribute's name
59
+ # @param [String] process_name the process's name
60
+ # @param [Hash{Symbol => Object}] attributes the attributes the process
61
+ # starts with, which the block can override
62
+ # @param [Proc] process_block the block that configures the process
63
+ # @param [Integer, nil] actual_pid the pid of the child that the block
64
+ # configures, or nil for a process
123
65
  #
124
- # @return [Boolean] true for an attribute a config can set
66
+ # @return [void]
125
67
  #
126
68
  # @api private
127
- def self.attribute?(name)
128
- attribute_names.include?(name)
129
- end
69
+ def initialize(process_name, attributes, process_block, actual_pid: nil)
70
+ @name = process_name
71
+ # A copy, so the block leaves the attributes it was given as they were
72
+ @attributes = attributes.dup
73
+ @actual_pid = actual_pid
74
+ @checks = []
130
75
 
131
- # Return the attributes a config can set on a process
132
- #
133
- # @return [Array<Symbol>] a process's configurable attributes, and :group
134
- #
135
- # @api private
136
- def self.attribute_names
137
- Process::CONFIGURABLE_ATTRIBUTES + [:group]
76
+ evaluate(&process_block)
138
77
  end
139
78
 
140
79
  # Add a check of the process, or a trigger such as :flapping
141
80
  #
81
+ # A process can have more than one check of a kind, such as two :http
82
+ # checks of different URLs.
83
+ #
142
84
  # @example
143
85
  # process.checks :cpu_usage, every: 10.seconds, below: 50, times: [3, 5]
144
86
  # process.checks :flapping, times: 2, within: 30.seconds, retry_in: 7.seconds
145
87
  #
146
88
  # @param [Symbol] name the check's name, such as :mem_usage
147
- # @param [Hash{Symbol => Object}, nil] options the check's options, as
148
- # a Hash, which bluepill 0.3.0 doesn't take
149
- # @param [Hash{Symbol => Object}] keywords the check's options, such as
89
+ # @param [Hash{Symbol => Object}] options the check's options, such as
150
90
  # :every, :below and :times
151
91
  #
152
- # @return [Hash{Symbol => Object}] the options
153
- #
154
- # @raise [ArgumentError] for options as a Hash and as keywords
92
+ # @return [void]
155
93
  #
156
94
  # @api public
157
- def checks(name, options = nil, **keywords)
158
- Deprecation.check_keywords(options, keywords, "process.checks #{name.inspect}")
159
- Deprecation.check_duplicate_check(name) if @watches.key?(name)
160
- @watches[name] = options || keywords
95
+ def checks(name, **options)
96
+ @checks << [name, options]
97
+ nil
161
98
  end
162
99
 
163
100
  # Monitor the process's children too
164
101
  #
165
102
  # The block configures each child the way a process block configures a
166
- # process. It must set a stop_command.
103
+ # process. It must set a stop_command. Bluepill runs it once as the
104
+ # config loads, to check it, and then again for each child it finds.
167
105
  #
168
106
  # @example
169
107
  # process.monitor_children do |child|
@@ -175,82 +113,37 @@ module Bluepill
175
113
  # @yieldparam [ProcessProxy] child the child's proxy, which is also self
176
114
  # @yieldreturn [void]
177
115
  #
178
- # @return [Proc, nil] the block
116
+ # @return [Proc] the block
117
+ #
118
+ # @raise [ConfigError] without a block
179
119
  #
180
120
  # @api public
181
121
  def monitor_children(&child_process_block)
182
- Deprecation.check_monitor_children(@name, child_process_block)
183
- @attributes[:monitor_children] = true
184
- @attributes[:child_process_block] = child_process_block
185
- end
122
+ raise ConfigError, "monitor_children for process '#{@name}' needs a block that configures the children" unless child_process_block
186
123
 
187
- # Create the process that the block described
188
- #
189
- # @return [Process] the process
190
- #
191
- # @api private
192
- def to_process
193
- Process.new(@name, @watches, @attributes)
194
- end
195
-
196
- private
197
-
198
- # Return the attribute a call names, and the value it sets
199
- #
200
- # A name alone with no value reads the attribute. A name ending in `=`,
201
- # or one alone, sets it to one value, and a name ending in `!` sets it
202
- # to true with none.
203
- #
204
- # @param [Symbol] name the method's name
205
- # @param [Array<Object>] args the call's arguments
206
- #
207
- # @return [Array(Symbol, Boolean), Array(Symbol, Boolean, Object), nil]
208
- # the attribute's name, whether the call reads it, and the value a
209
- # setter sets, or nil for a call with the wrong number of values
210
- #
211
- # @api private
212
- def attribute_call(name, args)
213
- attribute = name.to_s
214
- key = attribute.sub(/[=!]\z/, "").to_sym
215
- return [key, true] if args.empty? && key.to_s.eql?(attribute)
216
-
217
- bang = attribute.end_with?("!")
218
- value, = args
219
- [key, false, bang || value] if args.size.equal?(bang ? 0 : 1)
124
+ @child_process_block = child_process_block
220
125
  end
221
126
 
222
- # Fail on an attribute a process doesn't have
223
- #
224
- # The message names the process and the attribute, and the closest
225
- # attribute a process has, when there's one close enough.
127
+ # Create the process that the block described
226
128
  #
227
- # @param [Symbol] key the attribute's name
129
+ # The group isn't an attribute of the process: the application adds
130
+ # the process to it.
228
131
  #
229
- # @return [void] it never returns
132
+ # @param [Hash{Symbol => Object}] options more of the process's options,
133
+ # such as its logger, or the factory of its children
230
134
  #
231
- # @raise [NoMethodError] always
135
+ # @return [Process] the process
232
136
  #
233
137
  # @api private
234
- def unknown_attribute!(key)
235
- message = "Unknown attribute '#{key}' for process '#{@name}'"
236
- suggestion = suggest(key)
237
- message += ". Did you mean '#{suggestion}'?" if suggestion
238
- raise NoMethodError.new(message, key) # steep:ignore ArgumentTypeMismatch
138
+ def to_process(**options)
139
+ Process.new(@name, @checks, actual_pid: @actual_pid, **attributes.except(:group), **options)
239
140
  end
240
141
 
241
- # Return the attribute a process has that's closest to a misspelled one
242
- #
243
- # Ruby's did_you_mean picks it, as it picks a method for a misspelled
244
- # one.
142
+ # Name the process, for an unknown attribute's error
245
143
  #
246
- # @param [Symbol] key the misspelled attribute
247
- #
248
- # @return [String, nil] the closest attribute, or nil
144
+ # @return [String] the process's name
249
145
  #
250
146
  # @api private
251
- def suggest(key)
252
- names = self.class.attribute_names.collect(&:to_s)
253
- DidYouMean::SpellChecker.new(dictionary: names).correct(key).first
254
- end
147
+ def attribute_owner = "process '#{@name}'"
255
148
  end
256
149
  end
data/lib/bluepill/dsl.rb CHANGED
@@ -5,7 +5,11 @@ module Bluepill
5
5
  #
6
6
  # The block describes the application's processes. It runs with self set
7
7
  # to an {AppProxy}, and also gets the proxy as an argument. Bluepill then
8
- # loads the application, which starts its daemon.
8
+ # loads the application, which starts its daemon. A {Bluepill::Error},
9
+ # such as a {ConfigError} for a mistake in the config, prints its message
10
+ # and exits with its exit_status, which is 6 for a ConfigError. A
11
+ # ConfigError's message starts with the pill file's line, as in
12
+ # "app.pill.rb:5: Unknown attribute ...".
9
13
  #
10
14
  # @example
11
15
  # Bluepill.application(:my_app, log_file: '/var/log/bluepill.log') do |app|
@@ -16,10 +20,8 @@ module Bluepill
16
20
  # end
17
21
  #
18
22
  # @param [#to_s] app_name the application's name
19
- # @param [Hash{Symbol => Object}, nil] options the application's options,
20
- # as a Hash, which bluepill 0.3.0 doesn't take
21
- # @param [Hash{Symbol => Object}] keywords the application's options,
22
- # such as :log_file, :foreground and :base_dir
23
+ # @param [Hash{Symbol => Object}] options the application's options, such
24
+ # as log_file:, foreground: and base_dir:
23
25
  #
24
26
  # @yield [app] describes the application's processes
25
27
  # @yieldparam [AppProxy] app the application's proxy, which is also self
@@ -27,17 +29,16 @@ module Bluepill
27
29
  #
28
30
  # @return [void]
29
31
  #
30
- # @raise [ArgumentError] for options as a Hash and as keywords
31
- #
32
32
  # @api public
33
- def self.application(app_name, options = nil, **keywords, &block)
34
- Deprecation.check_keywords(options, keywords, "Bluepill.application #{app_name.to_s.inspect}")
35
- options ||= keywords
36
- app_proxy = AppProxy.new(app_name, options)
37
- Deprecation.check_ruby
38
- Deprecation.check_application_options(options)
39
- # instance_eval also passes the proxy to a block that takes an argument
40
- app_proxy.instance_eval(&block)
33
+ def self.application(app_name, **options, &block)
34
+ app_proxy = AppProxy.new(app_name, **options)
35
+ # evaluate also passes the proxy to a block that takes an argument, and
36
+ # turns a misspelled attribute into a ConfigError
37
+ app_proxy.evaluate(&block)
41
38
  app_proxy.app.load
39
+ rescue Error => e
40
+ location = e.location if e.is_a?(ConfigError)
41
+ $stderr.puts(location ? "#{location}: #{e}" : e)
42
+ exit(e.exit_status)
42
43
  end
43
44
  end