bluepill 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -21
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +100 -20
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +59 -259
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +129 -65
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -118
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -152
  20. data/lib/bluepill/dsl/process_proxy.rb +100 -51
  21. data/lib/bluepill/dsl.rb +0 -23
  22. data/lib/bluepill/group.rb +187 -78
  23. data/lib/bluepill/logger.rb +139 -48
  24. data/lib/bluepill/pid_file.rb +23 -0
  25. data/lib/bluepill/process/attributes.rb +379 -0
  26. data/lib/bluepill/process/children.rb +70 -0
  27. data/lib/bluepill/process/pid.rb +154 -0
  28. data/lib/bluepill/process/state_machine.rb +196 -0
  29. data/lib/bluepill/process/transition.rb +45 -0
  30. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  31. data/lib/bluepill/process/user_commands.rb +71 -0
  32. data/lib/bluepill/process/watches.rb +141 -0
  33. data/lib/bluepill/process.rb +64 -667
  34. data/lib/bluepill/process_commands/execution.rb +89 -0
  35. data/lib/bluepill/process_commands/hooks.rb +186 -0
  36. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  37. data/lib/bluepill/process_commands.rb +106 -174
  38. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  39. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  40. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  41. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  42. data/lib/bluepill/process_conditions/http.rb +48 -45
  43. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  44. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  45. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  46. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  47. data/lib/bluepill/process_conditions.rb +3 -2
  48. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  49. data/lib/bluepill/process_journal/lock.rb +106 -0
  50. data/lib/bluepill/process_journal.rb +126 -233
  51. data/lib/bluepill/process_statistics.rb +2 -2
  52. data/lib/bluepill/socket.rb +60 -20
  53. data/lib/bluepill/system/command_setup.rb +98 -0
  54. data/lib/bluepill/system/commands.rb +219 -248
  55. data/lib/bluepill/system/daemon.rb +161 -0
  56. data/lib/bluepill/system/ps.rb +85 -0
  57. data/lib/bluepill/system/ps_table.rb +163 -0
  58. data/lib/bluepill/system.rb +45 -126
  59. data/lib/bluepill/trigger.rb +52 -25
  60. data/lib/bluepill/triggers/flapping.rb +8 -16
  61. data/lib/bluepill/units.rb +126 -0
  62. data/lib/bluepill/util/inflections.rb +34 -0
  63. data/lib/bluepill/util/levenshtein.rb +53 -0
  64. data/lib/bluepill/util/rotational_array.rb +1 -1
  65. data/lib/bluepill/version.rb +13 -12
  66. data/lib/bluepill.rb +27 -35
  67. data/sig/bluepill/application.rbs +257 -0
  68. data/sig/bluepill/checks.rbs +203 -0
  69. data/sig/bluepill/dsl.rbs +126 -0
  70. data/sig/bluepill/logger.rbs +104 -0
  71. data/sig/bluepill/process.rbs +542 -0
  72. data/sig/bluepill/system.rbs +316 -0
  73. data/sig/bluepill.rbs +47 -994
  74. metadata +39 -49
  75. data/lib/bluepill/clock.rb +0 -48
  76. data/lib/bluepill/deprecation.rb +0 -74
  77. data/lib/bluepill/trigger/custom.rb +0 -147
  78. data/lib/bluepill/util/log_error.rb +0 -29
@@ -37,6 +37,15 @@ module Bluepill
37
37
  # @api private
38
38
  attr_accessor :process_logger
39
39
 
40
+ # The setting that says whether each command runs on a process without
41
+ # waiting for it
42
+ NOBLOCK = {
43
+ start: :group_start_noblock,
44
+ unmonitor: :group_unmonitor_noblock,
45
+ stop: :group_stop_noblock,
46
+ restart: :group_restart_noblock
47
+ }.freeze
48
+
40
49
  # Create an empty group
41
50
  #
42
51
  # @param [String, nil] name the group's name, or nil for the nameless
@@ -86,84 +95,68 @@ module Bluepill
86
95
  processes.each(&:determine_initial_state)
87
96
  end
88
97
 
89
- # @!method start(process_name = nil)
90
- # Start the group's processes, or one of them
91
- #
92
- # Each process runs the command on a thread of its own. Unless it's
93
- # set to run it without blocking (group_start_noblock), the group
94
- # waits for it before going on to the next process.
95
- #
96
- # @param [String, nil] process_name the process to start, or nil for
97
- # every process in the group
98
+ # Start the group's processes, or one of them
98
99
  #
99
- # @return [Array<String>] the processes it started, as group:process
100
+ # Each process runs the command on a thread of its own. Unless it's set
101
+ # to run it without blocking (group_start_noblock), the group waits for it
102
+ # before going on to the next process.
100
103
  #
101
- # @api private
102
- #
103
- # @!method unmonitor(process_name = nil)
104
- # Stop monitoring the group's processes, or one of them
104
+ # @param [String, nil] process_name the process to start, or nil for
105
+ # every process in the group
105
106
  #
106
- # Each process runs the command on a thread of its own. Unless it's
107
- # set to run it without blocking (group_unmonitor_noblock), the group
108
- # waits for it before going on to the next process.
107
+ # @return [Array<String>] the processes it started, as group:process
109
108
  #
110
- # @param [String, nil] process_name the process to unmonitor, or nil
111
- # for every process in the group
109
+ # @api private
110
+ def start(process_name = nil)
111
+ send_user_command(:start, process_name)
112
+ end
113
+
114
+ # Stop monitoring the group's processes, or one of them
112
115
  #
113
- # @return [Array<String>] the processes it unmonitored, as
114
- # group:process
116
+ # Each process runs the command on a thread of its own. Unless it's set
117
+ # to run it without blocking (group_unmonitor_noblock), the group waits for it
118
+ # before going on to the next process.
115
119
  #
116
- # @api private
120
+ # @param [String, nil] process_name the process to unmonitor, or nil
121
+ # for every process in the group
117
122
  #
118
- # @!method stop(process_name = nil)
119
- # Stop the group's processes, or one of them
123
+ # @return [Array<String>] the processes it unmonitored, as group:process
120
124
  #
121
- # Each process runs the command on a thread of its own. Unless it's
122
- # set to run it without blocking (group_stop_noblock), the group waits
123
- # for it before going on to the next process.
125
+ # @api private
126
+ def unmonitor(process_name = nil)
127
+ send_user_command(:unmonitor, process_name)
128
+ end
129
+
130
+ # Stop the group's processes, or one of them
124
131
  #
125
- # @param [String, nil] process_name the process to stop, or nil for
126
- # every process in the group
132
+ # Each process runs the command on a thread of its own. Unless it's set
133
+ # to run it without blocking (group_stop_noblock), the group waits for it
134
+ # before going on to the next process.
127
135
  #
128
- # @return [Array<String>] the processes it stopped, as group:process
136
+ # @param [String, nil] process_name the process to stop, or nil for
137
+ # every process in the group
129
138
  #
130
- # @api private
139
+ # @return [Array<String>] the processes it stopped, as group:process
131
140
  #
132
- # @!method restart(process_name = nil)
133
- # Restart the group's processes, or one of them
141
+ # @api private
142
+ def stop(process_name = nil)
143
+ send_user_command(:stop, process_name)
144
+ end
145
+
146
+ # Restart the group's processes, or one of them
134
147
  #
135
- # Each process runs the command on a thread of its own. Unless it's
136
- # set to run it without blocking (group_restart_noblock), the group
137
- # waits for it before going on to the next process.
148
+ # Each process runs the command on a thread of its own. Unless it's set
149
+ # to run it without blocking (group_restart_noblock), the group waits for it
150
+ # before going on to the next process.
138
151
  #
139
- # @param [String, nil] process_name the process to restart, or nil for
140
- # every process in the group
152
+ # @param [String, nil] process_name the process to restart, or nil for
153
+ # every process in the group
141
154
  #
142
- # @return [Array<String>] the processes it restarted, as group:process
155
+ # @return [Array<String>] the processes it restarted, as group:process
143
156
  #
144
- # @api private
145
- [:start, :unmonitor, :stop, :restart].each do |event|
146
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
147
- def #{event}(process_name = nil)
148
- threads = []
149
- affected = []
150
- self.processes.each do |process|
151
- next if process_name && process_name != process.name
152
- affected << [self.name, process.name].join(":")
153
- noblock = process.group_#{event}_noblock
154
- if noblock
155
- self.logger.debug("Command #{event} running in non-blocking mode.")
156
- threads << Thread.new { process.handle_user_command("#{event}") }
157
- else
158
- self.logger.debug("Command #{event} running in blocking mode.")
159
- thread = Thread.new { process.handle_user_command("#{event}") }
160
- thread.join
161
- end
162
- end
163
- threads.each { |t| t.join } unless threads.nil?
164
- affected
165
- end
166
- RUBY
157
+ # @api private
158
+ def restart(process_name = nil)
159
+ send_user_command(:restart, process_name)
167
160
  end
168
161
 
169
162
  # Return the status lines that bluepill status shows
@@ -181,27 +174,143 @@ module Bluepill
181
174
  def status(process_name = nil)
182
175
  lines = [] #: Array[String]
183
176
  if process_name.nil?
184
- prefix = (' ' if name)
185
- lines << "#{name}:" if name
177
+ add_group_status(lines)
178
+ else
179
+ add_process_status(process_name, lines)
180
+ end
181
+ lines << ""
182
+ end
186
183
 
187
- processes.each do |process|
188
- lines << format('%s%s(pid:%s): %s', prefix, process.name, process.actual_pid, process.state)
189
- next unless process.monitor_children?
184
+ private
190
185
 
191
- process.children.each do |child|
192
- lines << format(' %s%s: %s', prefix, child.name, child.state)
193
- end
194
- end
186
+ # Run a user command on the group's processes, or one of them
187
+ #
188
+ # Each process runs it on a thread of its own. The group waits for each
189
+ # one that blocks before going on to the next process, and for the rest
190
+ # at the end.
191
+ #
192
+ # @param [Symbol] event :start, :unmonitor, :stop or :restart
193
+ # @param [String, nil] process_name the process to run it on, or nil for
194
+ # every process in the group
195
+ #
196
+ # @return [Array<String>] the processes it ran on, as group:process
197
+ #
198
+ # @api private
199
+ def send_user_command(event, process_name)
200
+ threads = [] #: Array[Thread]
201
+ affected = targets(process_name).collect do |process|
202
+ noblock = noblock?(process, event)
203
+ thread = start_user_command(process, event, noblock)
204
+ noblock ? threads << thread : thread.join
205
+ [name, process.name].join(":")
206
+ end
207
+ threads.each(&:join)
208
+ affected
209
+ end
195
210
 
196
- else
197
- processes.each do |process|
198
- next unless process_name.eql?(process.name)
211
+ # Return the group's processes, or the one with a name
212
+ #
213
+ # @param [String, nil] process_name the process, or nil for every
214
+ # process in the group
215
+ #
216
+ # @return [Array<Process>]
217
+ #
218
+ # @api private
219
+ def targets(process_name)
220
+ return processes unless process_name
221
+
222
+ processes.select { |process| process_name.eql?(process.name) }
223
+ end
224
+
225
+ # Log how a process runs a user command, and start it on a thread
226
+ #
227
+ # @param [Process] process the process
228
+ # @param [Symbol] event the command
229
+ # @param [Boolean] noblock whether the group goes on without waiting
230
+ # for it
231
+ #
232
+ # @return [Thread] the thread that runs it
233
+ #
234
+ # @api private
235
+ def start_user_command(process, event, noblock)
236
+ logger.debug("Command #{event} running in #{noblock ? "non-blocking" : "blocking"} mode.")
237
+ Thread.new { process.handle_user_command(event) }
238
+ end
239
+
240
+ # Return whether a process runs a user command without the group waiting
241
+ #
242
+ # @param [Process] process the process
243
+ # @param [Symbol] event the command
244
+ #
245
+ # @return [Boolean]
246
+ #
247
+ # @api private
248
+ def noblock?(process, event)
249
+ process.public_send(NOBLOCK.fetch(event))
250
+ end
251
+
252
+ # Return a process's status line
253
+ #
254
+ # @param [Process] process the process
255
+ # @param [String, nil] prefix the indent, if any
256
+ #
257
+ # @return [String] its name, pid and state
258
+ #
259
+ # @api private
260
+ def status_line(process, prefix = nil)
261
+ "#{prefix}#{process.name}(pid:#{process.actual_pid}): #{process.state}"
262
+ end
263
+
264
+ # Add a status line for each process, under the group's name
265
+ #
266
+ # A process with monitor_children has a line for each of its children
267
+ # too.
268
+ #
269
+ # @param [Array<String>] lines the status lines to add to
270
+ #
271
+ # @return [void]
272
+ #
273
+ # @api private
274
+ def add_group_status(lines)
275
+ prefix = (" " if name)
276
+ lines << "#{name}:" if name
277
+
278
+ processes.each do |process|
279
+ lines << status_line(process, prefix)
280
+ add_children_status(process.children, prefix, lines) if process.monitor_children?
281
+ end
282
+ end
283
+
284
+ # Add a status line for each child of a process
285
+ #
286
+ # @param [Array<Process>] children the process's children
287
+ # @param [String, nil] prefix the indent of the process's line
288
+ # @param [Array<String>] lines the status lines to add to
289
+ #
290
+ # @return [void]
291
+ #
292
+ # @api private
293
+ def add_children_status(children, prefix, lines)
294
+ children.each do |child|
295
+ lines << " #{prefix}#{child.name}: #{child.state}"
296
+ end
297
+ end
298
+
299
+ # Add a process's status line and its event history
300
+ #
301
+ # @param [String] process_name the process to show
302
+ # @param [Array<String>] lines the status lines to add to
303
+ #
304
+ # @return [void]
305
+ #
306
+ # @api private
307
+ def add_process_status(process_name, lines)
308
+ processes.each do |process|
309
+ next unless process_name.eql?(process.name)
199
310
 
200
- lines << format('%s(pid:%s): %s', process.name, process.actual_pid, process.state)
201
- lines << process.statistics.to_s
202
- end
311
+ lines << status_line(process)
312
+ lines << process.statistics.to_s
203
313
  end
204
- lines << ''
205
314
  end
206
315
  end
207
316
  end
@@ -1,96 +1,103 @@
1
1
  module Bluepill
2
2
  # Write bluepill's log, to syslog or a file, with prefixes
3
3
  #
4
- # A logger made with prefix_with writes through its parent, which adds
5
- # its own prefix, so a process's messages are tagged with its app's name
6
- # and its own, as in "[app:web] Going from up => down".
4
+ # A logger made with prefix_with adds its prefix to its parent's, so a
5
+ # process's messages are tagged with its app's name and its own, as in
6
+ # "[app:web] Going from up => down". Every logger made from the same one
7
+ # writes to the same place, which reopen can change for all of them.
7
8
  #
8
9
  # @api private
9
10
  class Logger
10
- # The syslog priorities, each a method that logs a message
11
+ # The syslog priorities, most severe first, each a method that logs a
12
+ # message
11
13
  #
12
14
  # @api private
13
- LOG_METHODS = [:emerg, :alert, :crit, :err, :warning, :notice, :info, :debug].freeze
15
+ LOG_METHODS = %i[emerg alert crit err warning notice info debug].freeze
14
16
 
15
17
  # Make a logger
16
18
  #
17
19
  # @param [Hash{Symbol => Object}] options :logger, a parent logger to
18
- # write through, :prefix, :stdout to echo messages there too,
19
- # :log_file to write to instead of syslog, and :identity, the name
20
- # syslog shows
20
+ # write through, or an object to write to, :prefix, :stdout to echo
21
+ # messages there too, :log_file to write to instead of syslog,
22
+ # :identity, the name syslog shows, and :log_level, the least severe
23
+ # priority to log (debug, so everything, by default)
21
24
  #
22
25
  # @return [void]
23
26
  #
27
+ # @raise [ArgumentError] if :log_level isn't one of {LOG_METHODS}
28
+ #
24
29
  # @api private
25
30
  def initialize(options = {})
26
- @options = options
27
- @logger = options[:logger] || create_logger
28
- @prefix = options[:prefix]
29
- @stdout = options[:stdout]
31
+ @options = options
32
+ @logger = options[:logger] || create_logger
33
+ @prefix = options[:prefix]
30
34
  @prefixes = {}
35
+ # Set here, and not in settings_from, since Ruby makes room in a new
36
+ # Logger for the instance variables that initialize sets
37
+ @level, @names, @stdout, @sink = settings_from(@logger, priority(options[:log_level] || :debug))
38
+ @label = label(@names)
31
39
  end
32
40
 
33
- # @!method emerg(msg, prefix = [])
41
+ # @!method emerg(msg, prefix = nil)
34
42
  # Log a message at the emergency priority
35
43
  # @param [String] msg the message
36
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
44
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
37
45
  # @return [void]
38
46
  # @api private
39
- # @!method alert(msg, prefix = [])
47
+ # @!method alert(msg, prefix = nil)
40
48
  # Log a message at the alert priority
41
49
  # @param [String] msg the message
42
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
50
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
43
51
  # @return [void]
44
52
  # @api private
45
- # @!method crit(msg, prefix = [])
53
+ # @!method crit(msg, prefix = nil)
46
54
  # Log a message at the critical priority
47
55
  # @param [String] msg the message
48
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
56
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
49
57
  # @return [void]
50
58
  # @api private
51
- # @!method err(msg, prefix = [])
59
+ # @!method err(msg, prefix = nil)
52
60
  # Log a message at the error priority
53
61
  # @param [String] msg the message
54
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
62
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
55
63
  # @return [void]
56
64
  # @api private
57
- # @!method warning(msg, prefix = [])
65
+ # @!method warning(msg, prefix = nil)
58
66
  # Log a message at the warning priority
59
67
  # @param [String] msg the message
60
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
68
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
61
69
  # @return [void]
62
70
  # @api private
63
- # @!method notice(msg, prefix = [])
71
+ # @!method notice(msg, prefix = nil)
64
72
  # Log a message at the notice priority
65
73
  # @param [String] msg the message
66
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
74
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
67
75
  # @return [void]
68
76
  # @api private
69
- # @!method info(msg, prefix = [])
77
+ # @!method info(msg, prefix = nil)
70
78
  # Log a message at the info priority
71
79
  # @param [String] msg the message
72
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
80
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
73
81
  # @return [void]
74
82
  # @api private
75
- # @!method debug(msg, prefix = [])
83
+ # @!method debug(msg, prefix = nil)
76
84
  # Log a message at the debug priority
77
85
  # @param [String] msg the message
78
- # @param [Array<String, nil>] prefix the prefixes of the loggers below
86
+ # @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
79
87
  # @return [void]
80
88
  # @api private
81
- LOG_METHODS.each do |method|
89
+ LOG_METHODS.each_with_index do |method, priority|
82
90
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
83
- def #{method}(msg, prefix = [])
84
- if @logger.is_a?(self.class)
85
- @logger.#{method}(msg, [@prefix] + prefix)
86
- else
87
- s_prefix = prefix.size > 0 ? "[\#{prefix.compact.join(':')}] " : ""
88
- if @stdout
89
- $stdout.puts("[#{method}]: \#{s_prefix}\#{msg}")
90
- $stdout.flush
91
- end
92
- @logger.#{method}("\#{s_prefix}\#{msg}")
91
+ def #{method}(msg, prefix = nil) # def emerg(msg, prefix = nil)
92
+ # Below the log level, so dropped before anything is formatted
93
+ return if #{priority} > @level # return if 0 > @level
94
+
95
+ line = "\#{prefix ? label(@names + prefix) : @label}\#{msg}"
96
+ if @stdout
97
+ $stdout.puts("[#{method}]: \#{line}") # $stdout.puts("[emerg]: ...")
98
+ $stdout.flush
93
99
  end
100
+ @sink[0].#{method}(line) # @sink[0].emerg(line)
94
101
  end
95
102
  RUBY
96
103
  end
@@ -114,15 +121,99 @@ module Bluepill
114
121
  #
115
122
  # @api private
116
123
  def reopen
117
- logger = @logger
118
- if logger.instance_of?(self.class)
119
- logger.reopen
124
+ parent = @logger
125
+ if parent.instance_of?(Logger)
126
+ parent.reopen
127
+ else
128
+ @sink.replace([create_logger])
129
+ end
130
+ end
131
+
132
+ # The position in {LOG_METHODS} of the least severe priority to log
133
+ #
134
+ # @return [Integer]
135
+ #
136
+ # @api private
137
+ attr_reader :level
138
+
139
+ # Whether messages are echoed to stdout too
140
+ #
141
+ # @return [Boolean, nil]
142
+ #
143
+ # @api private
144
+ attr_reader :stdout
145
+
146
+ # Where the messages go, which every logger made from this one shares
147
+ #
148
+ # It holds one LoggerAdapter, Syslog, or given logger, and reopen
149
+ # replaces it.
150
+ #
151
+ # @return [Array<Object>]
152
+ #
153
+ # @api private
154
+ attr_reader :sink
155
+
156
+ # This logger's prefixes, its parents' first
157
+ #
158
+ # @return [Array<String, nil>]
159
+ #
160
+ # @api private
161
+ attr_reader :names
162
+
163
+ protected
164
+
165
+ # Return the level, prefixes, stdout and sink of a new logger
166
+ #
167
+ # A logger made with a parent logger writes through it: the parent's
168
+ # level still applies, and so do its prefixes. One made with anything
169
+ # else writes to that, without prefixes.
170
+ #
171
+ # @param [Logger, LoggerAdapter, Syslog, Object] parent the :logger
172
+ # option, or the log file or syslog without one
173
+ # @param [Integer] level the position in {LOG_METHODS} of the least
174
+ # severe priority the :log_level option allows
175
+ #
176
+ # @return [Array(Integer, Array<String, nil>, Boolean, Array<Object>)]
177
+ # the level, the prefixes, whether to echo to stdout, and the sink
178
+ #
179
+ # @api private
180
+ def settings_from(parent, level)
181
+ if parent.instance_of?(Logger)
182
+ [[level, parent.level].min, parent.names + [@prefix], parent.stdout, parent.sink]
120
183
  else
121
- @logger = create_logger
184
+ names = [] #: Array[String?]
185
+ [level, names, @options[:stdout], [parent]]
122
186
  end
123
187
  end
124
188
 
125
- protected
189
+ # Return the prefixes as they start a message, as in "[app:web] "
190
+ #
191
+ # @param [Array<String, nil>] names the prefixes, nil for none
192
+ #
193
+ # @return [String, nil] the prefixes, or nil without any, which reads as
194
+ # nothing in a message
195
+ #
196
+ # @api private
197
+ def label(names)
198
+ names = names.compact
199
+ "[#{names.join(":")}] " unless names.empty?
200
+ end
201
+
202
+ # Return the position of a log level in {LOG_METHODS}
203
+ #
204
+ # @param [Symbol, String] level a priority's name, such as :info
205
+ #
206
+ # @return [Integer] 0 for emerg, up to 7 for debug
207
+ #
208
+ # @raise [ArgumentError] if it isn't a priority's name
209
+ #
210
+ # @api private
211
+ def priority(level)
212
+ index = LOG_METHODS.index { |method| method.to_s.eql?(level.to_s) }
213
+ raise ArgumentError, "Unknown log_level #{level.inspect}. Use one of: #{LOG_METHODS.join(", ")}" unless index
214
+
215
+ index
216
+ end
126
217
 
127
218
  # Open the log file, or syslog if there isn't one
128
219
  #
@@ -135,7 +226,7 @@ module Bluepill
135
226
  else
136
227
  require_syslog
137
228
  Syslog.close if Syslog.opened? # need to explictly close it before reopening it
138
- Syslog.open(@options[:identity] || 'bluepilld', Syslog::LOG_PID, Syslog::LOG_LOCAL6)
229
+ Syslog.open(@options[:identity] || "bluepilld", Syslog::LOG_PID, Syslog::LOG_LOCAL6)
139
230
  end
140
231
  end
141
232
 
@@ -152,11 +243,11 @@ module Bluepill
152
243
  def require_syslog
153
244
  # A HUP reopens the log from a signal trap, where require raises
154
245
  # ThreadError, and syslog is loaded by then
155
- require 'syslog' unless defined?(::Syslog)
246
+ require "syslog" unless defined?(::Syslog)
156
247
  rescue LoadError
157
- message = 'Bluepill logs to syslog unless log_file is set, but the syslog library ' \
248
+ message = "Bluepill logs to syslog unless log_file is set, but the syslog library " \
158
249
  "could not be loaded. Since Ruby 3.4 it's a separate gem, so add gem 'syslog' " \
159
- 'to your Gemfile or set log_file.'
250
+ "to your Gemfile or set log_file."
160
251
  raise LoadError, message
161
252
  end
162
253
 
@@ -0,0 +1,23 @@
1
+ module Bluepill
2
+ # Read the pid file of a bluepill daemon
3
+ #
4
+ # @api private
5
+ module PidFile
6
+ # Return the pid in a pid file
7
+ #
8
+ # An empty or garbled pid file reads as 0, which is no pid, since
9
+ # signaling pid 0 would signal the reader's own process group.
10
+ #
11
+ # @param [String] path the pid file
12
+ #
13
+ # @return [Integer, nil] the pid, or nil without the file or a pid in it
14
+ #
15
+ # @api private
16
+ def self.read(path)
17
+ return unless File.exist?(path)
18
+
19
+ pid = File.read(path).to_i
20
+ pid if pid > 0
21
+ end
22
+ end
23
+ end