bluepill 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -21
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +100 -20
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +59 -259
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +129 -65
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -118
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -152
  20. data/lib/bluepill/dsl/process_proxy.rb +100 -51
  21. data/lib/bluepill/dsl.rb +0 -23
  22. data/lib/bluepill/group.rb +187 -78
  23. data/lib/bluepill/logger.rb +139 -48
  24. data/lib/bluepill/pid_file.rb +23 -0
  25. data/lib/bluepill/process/attributes.rb +379 -0
  26. data/lib/bluepill/process/children.rb +70 -0
  27. data/lib/bluepill/process/pid.rb +154 -0
  28. data/lib/bluepill/process/state_machine.rb +196 -0
  29. data/lib/bluepill/process/transition.rb +45 -0
  30. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  31. data/lib/bluepill/process/user_commands.rb +71 -0
  32. data/lib/bluepill/process/watches.rb +141 -0
  33. data/lib/bluepill/process.rb +64 -667
  34. data/lib/bluepill/process_commands/execution.rb +89 -0
  35. data/lib/bluepill/process_commands/hooks.rb +186 -0
  36. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  37. data/lib/bluepill/process_commands.rb +106 -174
  38. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  39. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  40. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  41. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  42. data/lib/bluepill/process_conditions/http.rb +48 -45
  43. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  44. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  45. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  46. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  47. data/lib/bluepill/process_conditions.rb +3 -2
  48. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  49. data/lib/bluepill/process_journal/lock.rb +106 -0
  50. data/lib/bluepill/process_journal.rb +126 -233
  51. data/lib/bluepill/process_statistics.rb +2 -2
  52. data/lib/bluepill/socket.rb +60 -20
  53. data/lib/bluepill/system/command_setup.rb +98 -0
  54. data/lib/bluepill/system/commands.rb +219 -248
  55. data/lib/bluepill/system/daemon.rb +161 -0
  56. data/lib/bluepill/system/ps.rb +85 -0
  57. data/lib/bluepill/system/ps_table.rb +163 -0
  58. data/lib/bluepill/system.rb +45 -126
  59. data/lib/bluepill/trigger.rb +52 -25
  60. data/lib/bluepill/triggers/flapping.rb +8 -16
  61. data/lib/bluepill/units.rb +126 -0
  62. data/lib/bluepill/util/inflections.rb +34 -0
  63. data/lib/bluepill/util/levenshtein.rb +53 -0
  64. data/lib/bluepill/util/rotational_array.rb +1 -1
  65. data/lib/bluepill/version.rb +13 -12
  66. data/lib/bluepill.rb +27 -35
  67. data/sig/bluepill/application.rbs +257 -0
  68. data/sig/bluepill/checks.rbs +203 -0
  69. data/sig/bluepill/dsl.rbs +126 -0
  70. data/sig/bluepill/logger.rbs +104 -0
  71. data/sig/bluepill/process.rbs +542 -0
  72. data/sig/bluepill/system.rbs +316 -0
  73. data/sig/bluepill.rbs +47 -994
  74. metadata +39 -49
  75. data/lib/bluepill/clock.rb +0 -48
  76. data/lib/bluepill/deprecation.rb +0 -74
  77. data/lib/bluepill/trigger/custom.rb +0 -147
  78. data/lib/bluepill/util/log_error.rb +0 -29
@@ -1,6 +1,11 @@
1
- require 'thread'
2
- require 'bluepill/system'
3
- require 'bluepill/process_journal'
1
+ require "thread"
2
+ require "bluepill/system"
3
+ require "bluepill/process_journal"
4
+ require "bluepill/base_directory"
5
+ require "bluepill/pid_file"
6
+ require "bluepill/application/listener"
7
+ require "bluepill/application/lifecycle"
8
+ require "bluepill/application/previous_daemon"
4
9
 
5
10
  module Bluepill
6
11
  # The bluepill daemon for one application
@@ -10,16 +15,20 @@ module Bluepill
10
15
  #
11
16
  # @api private
12
17
  class Application
18
+ include Listener
19
+ include Lifecycle
20
+ include PreviousDaemon
21
+
13
22
  # The commands that act on a process or group, and that the CLI sends
14
- PROCESS_COMMANDS = [:start, :stop, :restart, :unmonitor, :status].freeze
23
+ PROCESS_COMMANDS = %i[start stop restart unmonitor status].freeze
15
24
 
16
- # The commands that the daemon answers over its socket: the
17
- # PROCESS_COMMANDS, and the version and log file that the CLI asks for
25
+ # The commands that the daemon answers on its socket, and no others
18
26
  #
19
- # @api private
20
- DAEMON_COMMANDS = (PROCESS_COMMANDS + [:version, :log_file]).freeze
27
+ # The CLI sends the PROCESS_COMMANDS, version to check the daemon's
28
+ # version, and log_file to find the log that it tails.
29
+ DAEMON_COMMANDS = (PROCESS_COMMANDS + %i[version log_file]).freeze
21
30
 
22
- attr_accessor :name, :logger, :base_dir, :socket, :pid_file, :kill_timeout, :groups, :work_queue, :pids_dir, :log_file
31
+ attr_accessor :name, :logger, :base_dir, :socket, :pid_file, :kill_timeout, :groups, :pids_dir, :log_file
23
32
 
24
33
  # @!attribute [rw] name
25
34
  # The application's name, which names its socket and pid file
@@ -70,13 +79,6 @@ module Bluepill
70
79
  #
71
80
  # @api private
72
81
 
73
- # @!attribute [rw] work_queue
74
- # An attribute that bluepill doesn't use
75
- #
76
- # @return [Object]
77
- #
78
- # @api private
79
-
80
82
  # @!attribute [rw] pids_dir
81
83
  # The directory with the pid files of the application's processes
82
84
  #
@@ -98,7 +100,7 @@ module Bluepill
98
100
  #
99
101
  # @param [String] name the application's name
100
102
  # @param [Hash{Symbol => Object}] options :foreground, :log_file,
101
- # :base_dir and :kill_timeout
103
+ # :log_level, :base_dir and :kill_timeout
102
104
  #
103
105
  # @return [void]
104
106
  #
@@ -106,16 +108,10 @@ module Bluepill
106
108
  def initialize(name, options = {})
107
109
  self.name = name
108
110
 
109
- @foreground = options[:foreground]
111
+ @foreground = options[:foreground]
110
112
  self.log_file = options[:log_file]
111
- # Only used for a non-root user, who has a home
112
- home = ENV['HOME'] #: String
113
- self.base_dir = options[:base_dir] ||
114
- ENV['BLUEPILL_BASE_DIR'] ||
115
- (::Process.euid.zero? ? '/var/run/bluepill' : File.join(home, '.bluepill'))
116
- ProcessJournal.setup(base_dir, name)
117
- self.pid_file = File.join(base_dir, 'pids', "#{name}.pid")
118
- self.pids_dir = File.join(base_dir, 'pids', name)
113
+ @log_level = options[:log_level]
114
+ setup_base_dir(options[:base_dir])
119
115
  self.kill_timeout = options[:kill_timeout] || 10
120
116
 
121
117
  self.groups = {}
@@ -145,7 +141,7 @@ module Bluepill
145
141
  # @return [Object] what the block returns
146
142
  #
147
143
  # @api private
148
- def mutex(&block)
144
+ def synchronize(&block)
149
145
  @mutex.synchronize(&block)
150
146
  end
151
147
 
@@ -159,9 +155,9 @@ module Bluepill
159
155
  # @api private
160
156
  def load
161
157
  start_server
162
- rescue StandardError => e
163
- $stderr.puts('Failed to start bluepill:')
164
- $stderr.puts(format('%s `%s`', e.class.name, e.message))
158
+ rescue => e
159
+ $stderr.puts("Failed to start bluepill:")
160
+ $stderr.puts(format("%s `%s`", e.class.name, e.message))
165
161
  $stderr.puts(e.backtrace)
166
162
  exit(5)
167
163
  end
@@ -223,11 +219,10 @@ module Bluepill
223
219
 
224
220
  # Define a method for each of the PROCESS_COMMANDS
225
221
  PROCESS_COMMANDS.each do |command|
226
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
227
- def #{command}(group_name = nil, process_name = nil)
228
- self.send_to_process_or_group(:#{command}, group_name, process_name)
229
- end
230
- RUBY
222
+ define_method(command) do |group_name = nil, process_name = nil|
223
+ # @type self: Application
224
+ send_to_process_or_group(command, group_name, process_name)
225
+ end
231
226
  end
232
227
 
233
228
  # Add a process to a group, which is created if it's new
@@ -240,6 +235,8 @@ module Bluepill
240
235
  # @api private
241
236
  def add_process(process, group_name = nil)
242
237
  group_name = group_name.to_s if group_name
238
+ process.app_name = name
239
+ process.group_name = group_name
243
240
 
244
241
  groups[group_name] ||= Group.new(group_name, logger: logger.prefix_with(group_name))
245
242
  groups.fetch(group_name).add_process(process)
@@ -247,17 +244,14 @@ module Bluepill
247
244
 
248
245
  # Return the daemon's version, which the CLI checks against its own
249
246
  #
250
- # It's a String, since Marshal sends a module as only its name, which
251
- # the CLI would load as its own Version.
252
- #
253
- # @return [String] the version, such as "0.1.7"
247
+ # @return [Module] the Version module
254
248
  #
255
249
  # @api private
256
250
  def version
257
- Version.to_s
251
+ Version
258
252
  end
259
253
 
260
- protected
254
+ protected
261
255
 
262
256
  # Send a command to a process, a group, or every process
263
257
  #
@@ -271,247 +265,53 @@ module Bluepill
271
265
  # @api private
272
266
  def send_to_process_or_group(method, group_name, process_name)
273
267
  if group_name.nil? && process_name.nil?
274
- groups.values.collect do |group|
275
- group.public_send(method)
276
- end.flatten
268
+ send_to_groups(method)
277
269
  elsif groups.key?(group_name)
278
270
  groups.fetch(group_name).public_send(method, process_name)
279
271
  else
280
272
  # they must be targeting just by process name, which may contain ':'
281
- process_name = [group_name, process_name].compact.join(':')
282
- groups.values.collect do |group|
283
- group.public_send(method, process_name)
284
- end.flatten
273
+ send_to_groups(method, [group_name, process_name].compact.join(":"))
285
274
  end
286
275
  end
287
276
 
288
- # Start the thread that answers commands on the socket
289
- #
290
- # It kills the thread that a previous call started. Each command is a
291
- # line, such as start:group:process, and the answer is the command's
292
- # result, or the error it raised, marshaled.
293
- #
294
- # @return [Thread] the listener thread
295
- #
296
- # @api private
297
- def start_listener
298
- previous_thread = @listener_thread
299
- previous_thread.kill if previous_thread
300
- @listener_thread = Thread.new do
301
- loop do
302
- client = nil #: UNIXSocket?
303
- begin
304
- server = socket #: UNIXServer
305
- client = server.accept
306
- client.close_on_exec = true if client.respond_to?(:close_on_exec=)
307
- # The target is group:process and the process name may contain ':'
308
- command, *args = client.readline.strip.split(':', 3).reject(&:empty?)
309
- response = begin
310
- method = daemon_command(command)
311
- mutex { public_send(method, *args) }
312
- rescue StandardError => e
313
- e
314
- end
315
- client.write(Marshal.dump(response))
316
- rescue StandardError => e
317
- logger.err(format('Got exception in cmd listener: %s `%s`', e.class.name, e.message))
318
- Array(e.backtrace).each { |l| logger.err(l) }
319
- ensure
320
- begin
321
- # Ruby before 3.0 never counts branches in ensure as covered
322
- client && client.close
323
- rescue IOError
324
- # closed stream
325
- end
326
- end
327
- end
328
- end
329
- end
330
-
331
- # Return the one of the DAEMON_COMMANDS that a client's command names
332
- #
333
- # Anyone who can write to the socket can send any line, so only these
334
- # commands may run, and not any other of the daemon's public methods.
335
- #
336
- # @param [String, nil] command the command, or nil for an empty line
337
- #
338
- # @raise [NoMethodError] if it names none of them
339
- #
340
- # @return [Symbol]
341
- #
342
- # @api private
343
- def daemon_command(command)
344
- # The backtrace is caller's lines, with no locations, so Ruby 3.1's
345
- # error_highlight doesn't add a snippet of this file to the message
346
- DAEMON_COMMANDS.detect { |name| name.to_s.eql?(command) } ||
347
- raise(NoMethodError, "Unknown command `#{command}`", caller)
348
- end
349
-
350
- # Start the daemon
277
+ # Send a command to every group
351
278
  #
352
- # It stops a previous daemon for the application, clears journal locks
353
- # it left behind, daemonizes unless it runs in the foreground, opens the
354
- # socket, and runs until a signal stops it. Processes that are already
355
- # running, from their pid files, are monitored again, not restarted, so
356
- # bluepill can be upgraded without restarting them.
357
- #
358
- # @return [void]
359
- #
360
- # @api private
361
- def start_server
362
- kill_previous_bluepill
363
- ProcessJournal.clear_all_atomic_fs_locks
364
-
365
- begin
366
- ::Process.setpgid(0, 0)
367
- rescue Errno::EPERM
368
- end
369
-
370
- unless foreground?
371
- Daemonize.daemonize
372
- # Daemonizing ignores HUP, so trap it again to reopen the log on HUP
373
- setup_signal_traps
374
- end
375
-
376
- logger.reopen
377
-
378
- $0 = "bluepilld: #{name}"
379
-
380
- groups.each_value(&:determine_initial_state)
381
-
382
- write_pid_file
383
- self.socket = Socket.server(base_dir, name)
384
- start_listener
385
-
386
- run
387
- end
388
-
389
- # Tick every group once a second, until a signal stops the daemon
390
- #
391
- # @return [void]
392
- #
393
- # @api private
394
- def run
395
- @running = true # set to false by signal trap
396
- while @running
397
- mutex do
398
- System.reset_data
399
- groups.each_value(&:tick)
400
- end
401
- sleep 1
402
- end
403
- end
404
-
405
- # Clear the app's journal locks, and remove the socket and pid file
406
- #
407
- # The processes keep running, so the next daemon monitors them again.
408
- #
409
- # @return [void]
410
- #
411
- # @api private
412
- def cleanup
413
- ProcessJournal.clear_all_atomic_fs_locks
414
- begin
415
- System.delete_if_exists(socket.path) if socket
416
- rescue IOError
417
- end
418
- System.delete_if_exists(pid_file)
419
- end
420
-
421
- # Trap TERM and INT to clean up and stop, and HUP to reopen the log
422
- #
423
- # @return [void]
424
- #
425
- # @api private
426
- def setup_signal_traps
427
- terminator = proc do
428
- puts 'Terminating...'
429
- cleanup
430
- @running = false
431
- end #: ^(Integer signal) -> void
432
-
433
- Signal.trap('TERM', &terminator)
434
- Signal.trap('INT', &terminator)
435
-
436
- Signal.trap('HUP') do
437
- logger.reopen if logger
438
- end
439
- end
440
-
441
- # Create the logger and the pids directory
442
- #
443
- # Bluepill opens its log file and creates its pids dir as the user that
444
- # runs it. The uid and gid in the config only apply to its processes.
445
- # It exits with status 5 if it can't.
446
- #
447
- # @return [void]
448
- #
449
- # @api private
450
- def setup_logger_and_pids_dir
451
- self.logger = ProcessJournal.logger = Deprecation.logger = Logger.new(log_file: log_file, stdout: foreground?).prefix_with(name)
452
- setup_pids_dir
453
- rescue SystemCallError => e
454
- $stderr.puts("Failed to start bluepill: #{e}")
455
- message = 'The user running bluepill must be able to write to its log_file and base_dir. ' \
456
- 'The uid and gid in the config only apply to the processes it monitors.'
457
- $stderr.puts(message)
458
- exit(5)
459
- end
460
-
461
- # Create the pids directory, and let every user write to it
279
+ # @param [Symbol] method the command
280
+ # @param [Array<String>] args the process, or none for every process
462
281
  #
463
- # @return [void]
282
+ # @return [Array<String>] what the groups returned, flattened
464
283
  #
465
284
  # @api private
466
- def setup_pids_dir
467
- FileUtils.mkdir_p(pids_dir) unless File.exist?(pids_dir)
468
- # we need everybody to be able to write to the pids_dir as processes managed by
469
- # bluepill will be writing to this dir after they've dropped privileges
470
- FileUtils.chmod(0o777, pids_dir)
285
+ def send_to_groups(method, *args)
286
+ groups.values.collect do |group|
287
+ group.public_send(method, *args)
288
+ end.flatten
471
289
  end
472
290
 
473
- # Interrupt a daemon for this application that's still running
291
+ # Set the base directory, and the journal and pid paths in it
474
292
  #
475
- # It waits up to kill_timeout half seconds for the daemon to exit, and
476
- # exits with status 4 if it doesn't, or if signaling it fails.
293
+ # @param [String, nil] dir the base_dir option
477
294
  #
478
295
  # @return [void]
479
296
  #
480
297
  # @api private
481
- def kill_previous_bluepill
482
- return unless File.exist?(pid_file)
483
-
484
- # An empty or garbled pid file reads as 0, and signaling pid 0 would
485
- # interrupt bluepill's own process group
486
- previous_pid = File.read(pid_file).to_i
487
- return unless previous_pid > 0 && System.pid_alive?(previous_pid)
488
-
489
- ::Process.kill(0, previous_pid)
490
- puts "Killing previous bluepilld[#{previous_pid}]"
491
- ::Process.kill(2, previous_pid)
492
- rescue StandardError => e
493
- $stderr.puts 'Encountered error trying to kill previous bluepill:'
494
- $stderr.puts "#{e.class}: #{e}"
495
- exit(4) unless e.instance_of?(Errno::ESRCH)
496
- else
497
- kill_timeout.times do |_i|
498
- sleep 0.5
499
- break unless System.pid_alive?(previous_pid)
500
- end
501
-
502
- if System.pid_alive?(previous_pid)
503
- $stderr.puts "Previous bluepilld[#{previous_pid}] didn't die"
504
- exit(4)
505
- end
298
+ def setup_base_dir(dir)
299
+ self.base_dir = dir || ENV["BLUEPILL_BASE_DIR"] || default_base_dir
300
+ ProcessJournal.setup(base_dir, name)
301
+ paths = BaseDirectory.new(base_dir)
302
+ self.pid_file = paths.pid_file(name)
303
+ self.pids_dir = paths.process_pids_dir(name)
506
304
  end
507
305
 
508
- # Write the daemon's pid to its pid file
306
+ # Return the base directory without an option or BLUEPILL_BASE_DIR
509
307
  #
510
- # @return [void]
308
+ # @return [String] /var/run/bluepill for root, or ~/.bluepill
511
309
  #
512
310
  # @api private
513
- def write_pid_file
514
- File.open(pid_file, 'w') { |x| x.write(::Process.pid) }
311
+ def default_base_dir
312
+ # Only used for a non-root user, who has a home
313
+ home = ENV.fetch("HOME", nil) #: String
314
+ ::Process.euid.zero? ? "/var/run/bluepill" : File.join(home, ".bluepill")
515
315
  end
516
316
  end
517
317
  end
@@ -0,0 +1,93 @@
1
+ module Bluepill
2
+ # The paths in bluepill's base directory
3
+ #
4
+ # The daemons' sockets are in socks, and their pid files are in pids,
5
+ # next to a directory for each application's processes. The CLI and the
6
+ # daemon both find them here.
7
+ #
8
+ # @api private
9
+ class BaseDirectory
10
+ # The extension of a daemon's socket
11
+ SOCKET_EXTENSION = ".sock"
12
+
13
+ attr_reader :path
14
+
15
+ # @!attribute [r] path
16
+ # The base directory
17
+ #
18
+ # @return [String]
19
+ #
20
+ # @api private
21
+
22
+ # Create the paths for a base directory
23
+ #
24
+ # @param [String] path the base directory
25
+ #
26
+ # @return [void]
27
+ #
28
+ # @api private
29
+ def initialize(path)
30
+ @path = path
31
+ end
32
+
33
+ # Return the directory with the daemons' sockets
34
+ #
35
+ # @return [String]
36
+ #
37
+ # @api private
38
+ def sockets_dir
39
+ File.join(path, "socks")
40
+ end
41
+
42
+ # Return the directory with the daemons' pid files
43
+ #
44
+ # @return [String]
45
+ #
46
+ # @api private
47
+ def pids_dir
48
+ File.join(path, "pids")
49
+ end
50
+
51
+ # Return the socket that an application's daemon listens on
52
+ #
53
+ # @param [String] app the application's name
54
+ #
55
+ # @return [String]
56
+ #
57
+ # @api private
58
+ def socket_file(app)
59
+ File.join(sockets_dir, "#{app}#{SOCKET_EXTENSION}")
60
+ end
61
+
62
+ # Return an application's daemon's pid file
63
+ #
64
+ # @param [String] app the application's name
65
+ #
66
+ # @return [String]
67
+ #
68
+ # @api private
69
+ def pid_file(app)
70
+ File.join(pids_dir, "#{app}.pid")
71
+ end
72
+
73
+ # Return the directory with the pid files of an application's processes
74
+ #
75
+ # @param [String] app the application's name
76
+ #
77
+ # @return [String]
78
+ #
79
+ # @api private
80
+ def process_pids_dir(app)
81
+ File.join(pids_dir, app)
82
+ end
83
+
84
+ # Return the names of the applications with a socket
85
+ #
86
+ # @return [Array<String>]
87
+ #
88
+ # @api private
89
+ def socket_names
90
+ Dir[socket_file("*")].collect { |socket| File.basename(socket, SOCKET_EXTENSION) }
91
+ end
92
+ end
93
+ end