bluepill 0.2.9 → 0.3.2

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 (95) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -2
  3. data/README.md +16 -13
  4. data/bin/bluepill +21 -8
  5. data/bluepill.gemspec +7 -2
  6. data/lib/bluepill/application/lifecycle.rb +48 -35
  7. data/lib/bluepill/application/listener.rb +2 -4
  8. data/lib/bluepill/application/previous_daemon.rb +12 -14
  9. data/lib/bluepill/application.rb +36 -30
  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 +46 -54
  14. data/lib/bluepill/controller/directory.rb +1 -3
  15. data/lib/bluepill/controller.rb +35 -37
  16. data/lib/bluepill/daemonize.rb +52 -0
  17. data/lib/bluepill/dsl/app_proxy.rb +68 -64
  18. data/lib/bluepill/dsl/attributes.rb +135 -0
  19. data/lib/bluepill/dsl/process_factory/child_factory.rb +83 -0
  20. data/lib/bluepill/dsl/process_factory/registry.rb +4 -26
  21. data/lib/bluepill/dsl/process_factory.rb +85 -84
  22. data/lib/bluepill/dsl/process_proxy.rb +80 -160
  23. data/lib/bluepill/dsl.rb +16 -15
  24. data/lib/bluepill/errors.rb +190 -0
  25. data/lib/bluepill/group.rb +14 -37
  26. data/lib/bluepill/logger.rb +65 -99
  27. data/lib/bluepill/process/attributes.rb +66 -32
  28. data/lib/bluepill/process/children.rb +6 -7
  29. data/lib/bluepill/process/pid.rb +43 -35
  30. data/lib/bluepill/process/state_machine.rb +15 -59
  31. data/lib/bluepill/process/stop_signals.rb +123 -0
  32. data/lib/bluepill/process/transition.rb +1 -3
  33. data/lib/bluepill/process/transition_callbacks.rb +2 -16
  34. data/lib/bluepill/process/triggers.rb +90 -0
  35. data/lib/bluepill/process/user_commands.rb +1 -1
  36. data/lib/bluepill/process/watches.rb +9 -23
  37. data/lib/bluepill/process.rb +55 -52
  38. data/lib/bluepill/process_commands/execution.rb +16 -24
  39. data/lib/bluepill/process_commands/hooks.rb +1 -31
  40. data/lib/bluepill/process_commands/stop_signals.rb +12 -17
  41. data/lib/bluepill/process_commands.rb +10 -8
  42. data/lib/bluepill/process_conditions/always_true.rb +2 -6
  43. data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
  44. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
  45. data/lib/bluepill/process_conditions/file_time.rb +8 -9
  46. data/lib/bluepill/process_conditions/http.rb +29 -67
  47. data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
  48. data/lib/bluepill/process_conditions/process_condition.rb +29 -47
  49. data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
  50. data/lib/bluepill/process_conditions.rb +5 -14
  51. data/lib/bluepill/process_journal/cleanup.rb +5 -53
  52. data/lib/bluepill/process_journal/config.rb +73 -0
  53. data/lib/bluepill/process_journal/entries.rb +128 -0
  54. data/lib/bluepill/process_journal/journal.rb +69 -0
  55. data/lib/bluepill/process_journal/kind.rb +60 -0
  56. data/lib/bluepill/process_journal/lock.rb +9 -4
  57. data/lib/bluepill/process_journal.rb +13 -324
  58. data/lib/bluepill/process_statistics.rb +3 -7
  59. data/lib/bluepill/socket.rb +19 -17
  60. data/lib/bluepill/system/command_options.rb +136 -0
  61. data/lib/bluepill/system/command_result.rb +73 -0
  62. data/lib/bluepill/system/command_setup.rb +3 -4
  63. data/lib/bluepill/system/command_spawn.rb +244 -0
  64. data/lib/bluepill/system/commands.rb +27 -40
  65. data/lib/bluepill/system/daemon.rb +97 -15
  66. data/lib/bluepill/system/files.rb +35 -0
  67. data/lib/bluepill/system/ps.rb +20 -17
  68. data/lib/bluepill/system/ps_table.rb +7 -9
  69. data/lib/bluepill/system/usage.rb +175 -0
  70. data/lib/bluepill/system.rb +7 -192
  71. data/lib/bluepill/trigger.rb +18 -69
  72. data/lib/bluepill/triggers/flapping.rb +12 -8
  73. data/lib/bluepill/units.rb +7 -21
  74. data/lib/bluepill/version.rb +16 -26
  75. data/lib/bluepill.rb +1 -1
  76. data/sig/bluepill/application.rbs +11 -12
  77. data/sig/bluepill/attributes.rbs +48 -0
  78. data/sig/bluepill/checks.rbs +40 -40
  79. data/sig/bluepill/dsl.rbs +100 -59
  80. data/sig/bluepill/errors.rbs +48 -0
  81. data/sig/bluepill/logger.rbs +15 -31
  82. data/sig/bluepill/process.rbs +39 -305
  83. data/sig/bluepill/process_commands.rbs +235 -0
  84. data/sig/bluepill/stop_signals.rbs +21 -0
  85. data/sig/bluepill/system.rbs +191 -101
  86. data/sig/bluepill/version.rbs +17 -0
  87. data/sig/bluepill.rbs +2 -17
  88. data/sig/manifest.yaml +1 -0
  89. metadata +22 -8
  90. data/lib/bluepill/deprecation/custom_checks.rb +0 -193
  91. data/lib/bluepill/deprecation/dsl.rb +0 -141
  92. data/lib/bluepill/deprecation/options.rb +0 -140
  93. data/lib/bluepill/deprecation.rb +0 -92
  94. data/lib/bluepill/dsl/process_factory/children.rb +0 -99
  95. 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,11 @@ 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)
112
+ process.validate_privileges!
116
113
  end
117
114
 
118
- # Exit if another process in the group has the same name
115
+ # Fail if another process in the group has the same name
119
116
  #
120
117
  # @param [ProcessProxy] process the process's proxy
121
118
  #
@@ -131,7 +128,7 @@ module Bluepill
131
128
  config_error!(message)
132
129
  end
133
130
 
134
- # Exit unless the process has a start_command
131
+ # Fail unless the process has a start_command
135
132
  #
136
133
  # @param [ProcessProxy] process the process's proxy
137
134
  #
@@ -144,7 +141,7 @@ module Bluepill
144
141
  config_error!("Config Error: You must specify a start_command for '#{process.name}'")
145
142
  end
146
143
 
147
- # Exit if another process has the same pid file
144
+ # Fail if another process has the same pid file
148
145
  #
149
146
  # @param [ProcessProxy] process the process's proxy
150
147
  #
@@ -158,58 +155,62 @@ module Bluepill
158
155
  config_error!("Config Error: You have two entries with the pid file: #{pid_file}")
159
156
  end
160
157
 
161
- # Exit on a mistake in stop_signals
158
+ # Parse a process's stop_signals, and fail on a mistake
162
159
  #
163
160
  # The signals and delays must alternate, starting and ending with a
164
- # signal, and stop_grace_time must be longer than the delays.
161
+ # signal, and stop_grace_time must be longer than the delays. The
162
+ # proxy keeps the parsed {Process::StopSignals}, for the process.
165
163
  #
166
164
  # @param [ProcessProxy] process the process's proxy
167
165
  #
168
166
  # @return [void]
169
167
  #
168
+ # @raise [ConfigError] for a mistake in stop_signals
169
+ #
170
170
  # @api private
171
171
  def validate_stop_signals!(process)
172
- stop_signals = process.attributes[:stop_signals]
173
- return if stop_signals.nil?
172
+ attributes = process.attributes
173
+ signals = attributes[:stop_signals]
174
+ return if signals.nil?
174
175
 
175
- validate_stop_signal_order!(stop_signals)
176
- validate_stop_grace_time!(stop_signals, process.attributes[:stop_grace_time])
176
+ stop_signals = Process::StopSignals.new(signals)
177
+ validate_stop_grace_time!(stop_signals, attributes[:stop_grace_time])
178
+ attributes[:stop_signals] = stop_signals
177
179
  end
178
180
 
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.
181
+ # Fail unless stop_grace_time is longer than the stop_signals delays
184
182
  #
185
- # @param [Array<Symbol, Integer>] stop_signals the signals and delays
183
+ # @param [Process::StopSignals] stop_signals the signals and delays
184
+ # @param [Integer, Float, nil] stop_grace_time the process's
185
+ # stop_grace_time
186
186
  #
187
187
  # @return [void]
188
188
  #
189
+ # @raise [ConfigError] unless stop_grace_time is longer
190
+ #
189
191
  # @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?
192
+ def validate_stop_grace_time!(stop_signals, stop_grace_time)
193
+ return if stop_grace_time && stop_grace_time > stop_signals.total_delay
194
+
195
+ config_error!("Config Error: Stop_grace_time should be greater than the sum of stop_signals delays!")
198
196
  end
199
197
 
200
- # Exit unless stop_grace_time is longer than the stop_signals delays
198
+ # Fail unless on_start_timeout is an event of the state machine
201
199
  #
202
- # @param [Array<Symbol, Integer>] stop_signals the signals and delays
203
- # @param [Integer, nil] stop_grace_time the process's stop_grace_time
200
+ # @param [ProcessProxy] process the process's proxy
204
201
  #
205
202
  # @return [void]
206
203
  #
207
204
  # @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
205
+ def validate_on_start_timeout!(process)
206
+ return unless process.attributes.key?(:on_start_timeout)
211
207
 
212
- config_error!("Config Error: Stop_grace_time should be greater than the sum of stop_signals delays!")
208
+ events = Process::StateMachine::TRANSITIONS.keys
209
+ event = process.on_start_timeout
210
+ return if events.include?(event)
211
+
212
+ config_error!("Config Error: on_start_timeout for '#{process.name}' must be one of " \
213
+ "#{events.collect(&:inspect).join(", ")}, not #{event.inspect}")
213
214
  end
214
215
 
215
216
  # Warn when bluepill can't find the process's pid
@@ -218,15 +219,14 @@ module Bluepill
218
219
  # so it cannot find the pid of any other process that uses it.
219
220
  #
220
221
  # @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
222
+ # @param [String] default_pid_file the default pid file
223
223
  #
224
224
  # @return [void]
225
225
  #
226
226
  # @api private
227
227
  def warn_unless_pid_known(process, default_pid_file)
228
228
  attributes = process.attributes
229
- return if attributes[:daemonize] || attributes[:pid_command] || !attributes.fetch(:pid_file).eql?(default_pid_file)
229
+ return if attributes[:daemonize] || attributes[:pid_command] || !process.pid_file.eql?(default_pid_file)
230
230
 
231
231
  warning = "Config Warning: '#{process.name}' sets no daemonize, pid_file or pid_command, " \
232
232
  "so bluepill cannot find its pid unless start_command writes it to #{default_pid_file}. " \
@@ -234,16 +234,17 @@ module Bluepill
234
234
  $stderr.puts warning
235
235
  end
236
236
 
237
- # Print a config error and exit with status 6
237
+ # Fail with a config error
238
+ #
239
+ # Bluepill.application prints its message and exits with status 6.
238
240
  #
239
241
  # @param [String] message the error
240
242
  #
241
243
  # @return [void] it never returns
242
244
  #
245
+ # @raise [ConfigError] always
246
+ #
243
247
  # @api private
244
- def config_error!(message)
245
- $stderr.puts message
246
- exit(6)
247
- end
248
+ def config_error!(message) = raise(ConfigError, message)
248
249
  end
249
250
  end
@@ -1,14 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "did_you_mean"
3
+ require "bluepill/dsl/attributes"
4
+ require "bluepill/process/attributes"
5
+ require "bluepill/daemonize"
4
6
 
5
7
  module Bluepill
6
8
  # The self of a process block in a config file
7
9
  #
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.
10
+ # Each of the process's attributes is set with `name = value`, and read
11
+ # back with `name`, which is nil until it's set. See
12
+ # {Process::CONFIGURABLE_ATTRIBUTES} for the attributes a process has, and
13
+ # `group` sets the group it's in.
12
14
  #
13
15
  # @example
14
16
  # app.process('web') do |process|
@@ -19,19 +21,13 @@ module Bluepill
19
21
  #
20
22
  # @api public
21
23
  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
24
+ include DSL::Attributes
28
25
 
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
26
+ # monitor_children sets the two attributes it leaves out
27
+ attribute(*(Process::Attributes::CONFIGURABLE_ATTRIBUTES - %i[monitor_children child_process_factory]), :group)
28
+
29
+ # The attributes that switch the user or groups that commands run as
30
+ PRIVILEGE_ATTRIBUTES = %i[uid gid supplementary_groups].freeze
35
31
 
36
32
  # Return the process's name
37
33
  #
@@ -40,130 +36,76 @@ module Bluepill
40
36
  # @api private
41
37
  attr_reader :name
42
38
 
43
- # Create the proxy of a process, and run its block
39
+ # Return the block that monitor_children gave, to configure each child
44
40
  #
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]
41
+ # @return [Proc, nil] the block
51
42
  #
52
43
  # @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
44
+ attr_reader :child_process_block
61
45
 
62
- # Set or read an attribute of the process
46
+ # Return the pid of the child that a monitor_children block configures
63
47
  #
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.
48
+ # It's nil when bluepill checks the block as the config loads, before
49
+ # it has found any children.
69
50
  #
70
51
  # @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
52
+ # process.monitor_children do |child|
53
+ # child.stdout = "/var/log/child.#{child.actual_pid}.log"
54
+ # end
80
55
  #
81
- # @raise [NoMethodError] for an attribute a process doesn't have, one
82
- # that isn't set yet, or more than one value
56
+ # @return [Integer, nil] the child's pid, or nil for a process
83
57
  #
84
58
  # @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
98
-
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
59
+ attr_reader :actual_pid
117
60
 
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.
61
+ # Create the proxy of a process, and run its block
121
62
  #
122
- # @param [Symbol] name the attribute's name
63
+ # @param [String] process_name the process's name
64
+ # @param [Hash{Symbol => Object}] attributes the attributes the process
65
+ # starts with, which the block can override
66
+ # @param [Proc] process_block the block that configures the process
67
+ # @param [Integer, nil] actual_pid the pid of the child that the block
68
+ # configures, or nil for a process
123
69
  #
124
- # @return [Boolean] true for an attribute a config can set
70
+ # @return [void]
125
71
  #
126
72
  # @api private
127
- def self.attribute?(name)
128
- attribute_names.include?(name)
129
- end
73
+ def initialize(process_name, attributes, process_block, actual_pid: nil)
74
+ @name = process_name
75
+ # A copy, so the block leaves the attributes it was given as they were
76
+ @attributes = attributes.dup
77
+ @actual_pid = actual_pid
78
+ @checks = []
130
79
 
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]
80
+ evaluate(&process_block)
138
81
  end
139
82
 
140
83
  # Add a check of the process, or a trigger such as :flapping
141
84
  #
85
+ # A process can have more than one check of a kind, such as two :http
86
+ # checks of different URLs.
87
+ #
142
88
  # @example
143
89
  # process.checks :cpu_usage, every: 10.seconds, below: 50, times: [3, 5]
144
90
  # process.checks :flapping, times: 2, within: 30.seconds, retry_in: 7.seconds
145
91
  #
146
92
  # @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
93
+ # @param [Hash{Symbol => Object}] options the check's options, such as
150
94
  # :every, :below and :times
151
95
  #
152
- # @return [Hash{Symbol => Object}] the options
153
- #
154
- # @raise [ArgumentError] for options as a Hash and as keywords
96
+ # @return [void]
155
97
  #
156
98
  # @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
99
+ def checks(name, **options)
100
+ @checks << [name, options]
101
+ nil
161
102
  end
162
103
 
163
104
  # Monitor the process's children too
164
105
  #
165
106
  # The block configures each child the way a process block configures a
166
- # process. It must set a stop_command.
107
+ # process. It must set a stop_command. Bluepill runs it once as the
108
+ # config loads, to check it, and then again for each child it finds.
167
109
  #
168
110
  # @example
169
111
  # process.monitor_children do |child|
@@ -175,82 +117,60 @@ module Bluepill
175
117
  # @yieldparam [ProcessProxy] child the child's proxy, which is also self
176
118
  # @yieldreturn [void]
177
119
  #
178
- # @return [Proc, nil] the block
120
+ # @return [Proc] the block
121
+ #
122
+ # @raise [ConfigError] without a block
179
123
  #
180
124
  # @api public
181
125
  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
126
+ unless child_process_block
127
+ raise ConfigError, "monitor_children for process '#{@name}' needs a block that configures the children"
128
+ end
129
+
130
+ @child_process_block = child_process_block
185
131
  end
186
132
 
187
133
  # Create the process that the block described
188
134
  #
135
+ # The group isn't an attribute of the process: the application adds
136
+ # the process to it.
137
+ #
138
+ # @param [Hash{Symbol => Object}] options more of the process's options,
139
+ # such as its logger, or the factory of its children
140
+ #
189
141
  # @return [Process] the process
190
142
  #
191
143
  # @api private
192
- def to_process
193
- Process.new(@name, @watches, @attributes)
144
+ def to_process(**options)
145
+ Process.new(@name, @checks, actual_pid: @actual_pid, **attributes.except(:group), **options)
194
146
  end
195
147
 
196
- private
197
-
198
- # Return the attribute a call names, and the value it sets
148
+ # Fail if the process sets a user or groups that bluepill can't switch to
199
149
  #
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.
150
+ # A Ruby that can't fork, such as JRuby, spawns commands, which can't
151
+ # switch users or groups. Bluepill only switches when it runs as root,
152
+ # so they fail the config only then.
203
153
  #
204
- # @param [Symbol] name the method's name
205
- # @param [Array<Object>] args the call's arguments
154
+ # @return [void]
206
155
  #
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
156
+ # @raise [ConfigError] if it sets uid, gid or supplementary_groups
210
157
  #
211
158
  # @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)
159
+ def validate_privileges!
160
+ return if Daemonize.can_fork? || !::Process::Sys.geteuid.zero?
216
161
 
217
- bang = attribute.end_with?("!")
218
- value, = args
219
- [key, false, bang || value] if args.size.equal?(bang ? 0 : 1)
220
- end
162
+ set = PRIVILEGE_ATTRIBUTES.select { |name| attributes[name] }
163
+ return if set.empty?
221
164
 
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.
226
- #
227
- # @param [Symbol] key the attribute's name
228
- #
229
- # @return [void] it never returns
230
- #
231
- # @raise [NoMethodError] always
232
- #
233
- # @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
165
+ raise ConfigError, "Config Error: #{attribute_owner} sets #{set.join(", ")}, but bluepill " \
166
+ "can't run commands as another user or group on #{RUBY_ENGINE}, which can't fork"
239
167
  end
240
168
 
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.
169
+ # Name the process, for an unknown attribute's error
245
170
  #
246
- # @param [Symbol] key the misspelled attribute
247
- #
248
- # @return [String, nil] the closest attribute, or nil
171
+ # @return [String] the process's name
249
172
  #
250
173
  # @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
174
+ def attribute_owner = "process '#{@name}'"
255
175
  end
256
176
  end