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