bluepill 0.1.6 → 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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +105 -8
  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 +57 -254
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +134 -56
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -94
  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 -92
  20. data/lib/bluepill/dsl/process_proxy.rb +109 -21
  21. data/lib/bluepill/group.rb +187 -78
  22. data/lib/bluepill/logger.rb +139 -48
  23. data/lib/bluepill/pid_file.rb +23 -0
  24. data/lib/bluepill/process/attributes.rb +379 -0
  25. data/lib/bluepill/process/children.rb +70 -0
  26. data/lib/bluepill/process/pid.rb +154 -0
  27. data/lib/bluepill/process/state_machine.rb +196 -0
  28. data/lib/bluepill/process/transition.rb +45 -0
  29. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  30. data/lib/bluepill/process/user_commands.rb +71 -0
  31. data/lib/bluepill/process/watches.rb +141 -0
  32. data/lib/bluepill/process.rb +56 -653
  33. data/lib/bluepill/process_commands/execution.rb +89 -0
  34. data/lib/bluepill/process_commands/hooks.rb +186 -0
  35. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  36. data/lib/bluepill/process_commands.rb +104 -172
  37. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  38. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  39. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  40. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  41. data/lib/bluepill/process_conditions/http.rb +52 -24
  42. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  43. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  44. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  45. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  46. data/lib/bluepill/process_conditions.rb +3 -2
  47. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  48. data/lib/bluepill/process_journal/lock.rb +106 -0
  49. data/lib/bluepill/process_journal.rb +126 -233
  50. data/lib/bluepill/process_statistics.rb +2 -2
  51. data/lib/bluepill/socket.rb +60 -20
  52. data/lib/bluepill/system/command_setup.rb +98 -0
  53. data/lib/bluepill/system/commands.rb +219 -248
  54. data/lib/bluepill/system/daemon.rb +161 -0
  55. data/lib/bluepill/system/ps.rb +85 -0
  56. data/lib/bluepill/system/ps_table.rb +163 -0
  57. data/lib/bluepill/system.rb +45 -126
  58. data/lib/bluepill/trigger.rb +52 -26
  59. data/lib/bluepill/triggers/flapping.rb +7 -15
  60. data/lib/bluepill/units.rb +126 -0
  61. data/lib/bluepill/util/inflections.rb +34 -0
  62. data/lib/bluepill/util/levenshtein.rb +53 -0
  63. data/lib/bluepill/util/rotational_array.rb +1 -1
  64. data/lib/bluepill/version.rb +13 -12
  65. data/lib/bluepill.rb +33 -36
  66. data/sig/bluepill/application.rbs +257 -0
  67. data/sig/bluepill/checks.rbs +203 -0
  68. data/sig/bluepill/dsl.rbs +126 -0
  69. data/sig/bluepill/logger.rbs +104 -0
  70. data/sig/bluepill/process.rbs +542 -0
  71. data/sig/bluepill/system.rbs +316 -0
  72. data/sig/bluepill.rbs +47 -873
  73. metadata +39 -45
@@ -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)
@@ -254,7 +251,7 @@ module Bluepill
254
251
  Version
255
252
  end
256
253
 
257
- protected
254
+ protected
258
255
 
259
256
  # Send a command to a process, a group, or every process
260
257
  #
@@ -268,247 +265,53 @@ module Bluepill
268
265
  # @api private
269
266
  def send_to_process_or_group(method, group_name, process_name)
270
267
  if group_name.nil? && process_name.nil?
271
- groups.values.collect do |group|
272
- group.public_send(method)
273
- end.flatten
268
+ send_to_groups(method)
274
269
  elsif groups.key?(group_name)
275
270
  groups.fetch(group_name).public_send(method, process_name)
276
271
  else
277
272
  # they must be targeting just by process name, which may contain ':'
278
- process_name = [group_name, process_name].compact.join(':')
279
- groups.values.collect do |group|
280
- group.public_send(method, process_name)
281
- end.flatten
282
- end
283
- end
284
-
285
- # Start the thread that answers commands on the socket
286
- #
287
- # It kills the thread that a previous call started. Each command is a
288
- # line, such as start:group:process, and the answer is the command's
289
- # result, or the error it raised, marshaled.
290
- #
291
- # @return [Thread] the listener thread
292
- #
293
- # @api private
294
- def start_listener
295
- previous_thread = @listener_thread
296
- previous_thread.kill if previous_thread
297
- @listener_thread = Thread.new do
298
- loop do
299
- client = nil #: UNIXSocket?
300
- begin
301
- server = socket #: UNIXServer
302
- client = server.accept
303
- client.close_on_exec = true if client.respond_to?(:close_on_exec=)
304
- # The target is group:process and the process name may contain ':'
305
- command, *args = client.readline.strip.split(':', 3).reject(&:empty?)
306
- response = begin
307
- method = daemon_command(command)
308
- mutex { public_send(method, *args) }
309
- rescue StandardError => e
310
- e
311
- end
312
- client.write(Marshal.dump(response))
313
- rescue StandardError => e
314
- logger.err(format('Got exception in cmd listener: %s `%s`', e.class.name, e.message))
315
- Array(e.backtrace).each { |l| logger.err(l) }
316
- ensure
317
- begin
318
- # Ruby before 3.0 never counts branches in ensure as covered
319
- client && client.close
320
- rescue IOError
321
- # closed stream
322
- end
323
- end
324
- end
325
- end
326
- end
327
-
328
- # Return the one of the DAEMON_COMMANDS that a client's command names
329
- #
330
- # Anyone who can write to the socket can send any line, so only these
331
- # commands may run, and not any other of the daemon's public methods.
332
- #
333
- # @param [String, nil] command the command, or nil for an empty line
334
- #
335
- # @raise [NoMethodError] if it names none of them
336
- #
337
- # @return [Symbol]
338
- #
339
- # @api private
340
- def daemon_command(command)
341
- # The backtrace is caller's lines, with no locations, so Ruby 3.1's
342
- # error_highlight doesn't add a snippet of this file to the message
343
- DAEMON_COMMANDS.detect { |name| name.to_s.eql?(command) } ||
344
- raise(NoMethodError, "Unknown command `#{command}`", caller)
345
- end
346
-
347
- # Start the daemon
348
- #
349
- # It stops a previous daemon for the application, clears journal locks
350
- # it left behind, daemonizes unless it runs in the foreground, opens the
351
- # socket, and runs until a signal stops it. Processes that are already
352
- # running, from their pid files, are monitored again, not restarted, so
353
- # bluepill can be upgraded without restarting them.
354
- #
355
- # @return [void]
356
- #
357
- # @api private
358
- def start_server
359
- kill_previous_bluepill
360
- ProcessJournal.clear_all_atomic_fs_locks
361
-
362
- begin
363
- ::Process.setpgid(0, 0)
364
- rescue Errno::EPERM
365
- end
366
-
367
- unless foreground?
368
- Daemonize.daemonize
369
- # Daemonizing ignores HUP, so trap it again to reopen the log on HUP
370
- setup_signal_traps
371
- end
372
-
373
- logger.reopen
374
-
375
- $0 = "bluepilld: #{name}"
376
-
377
- groups.each_value(&:determine_initial_state)
378
-
379
- write_pid_file
380
- self.socket = Socket.server(base_dir, name)
381
- start_listener
382
-
383
- run
384
- end
385
-
386
- # Tick every group once a second, until a signal stops the daemon
387
- #
388
- # @return [void]
389
- #
390
- # @api private
391
- def run
392
- @running = true # set to false by signal trap
393
- while @running
394
- mutex do
395
- System.reset_data
396
- groups.each_value(&:tick)
397
- end
398
- sleep 1
399
- end
400
- end
401
-
402
- # Clear the app's journal locks, and remove the socket and pid file
403
- #
404
- # The processes keep running, so the next daemon monitors them again.
405
- #
406
- # @return [void]
407
- #
408
- # @api private
409
- def cleanup
410
- ProcessJournal.clear_all_atomic_fs_locks
411
- begin
412
- System.delete_if_exists(socket.path) if socket
413
- rescue IOError
273
+ send_to_groups(method, [group_name, process_name].compact.join(":"))
414
274
  end
415
- System.delete_if_exists(pid_file)
416
275
  end
417
276
 
418
- # Trap TERM and INT to clean up and stop, and HUP to reopen the log
419
- #
420
- # @return [void]
277
+ # Send a command to every group
421
278
  #
422
- # @api private
423
- def setup_signal_traps
424
- terminator = proc do
425
- puts 'Terminating...'
426
- cleanup
427
- @running = false
428
- end #: ^(Integer signal) -> void
429
-
430
- Signal.trap('TERM', &terminator)
431
- Signal.trap('INT', &terminator)
432
-
433
- Signal.trap('HUP') do
434
- logger.reopen if logger
435
- end
436
- end
437
-
438
- # Create the logger and the pids directory
439
- #
440
- # Bluepill opens its log file and creates its pids dir as the user that
441
- # runs it. The uid and gid in the config only apply to its processes.
442
- # It exits with status 5 if it can't.
443
- #
444
- # @return [void]
445
- #
446
- # @api private
447
- def setup_logger_and_pids_dir
448
- self.logger = ProcessJournal.logger = Logger.new(log_file: log_file, stdout: foreground?).prefix_with(name)
449
- setup_pids_dir
450
- rescue SystemCallError => e
451
- $stderr.puts("Failed to start bluepill: #{e}")
452
- message = 'The user running bluepill must be able to write to its log_file and base_dir. ' \
453
- 'The uid and gid in the config only apply to the processes it monitors.'
454
- $stderr.puts(message)
455
- exit(5)
456
- end
457
-
458
- # 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
459
281
  #
460
- # @return [void]
282
+ # @return [Array<String>] what the groups returned, flattened
461
283
  #
462
284
  # @api private
463
- def setup_pids_dir
464
- FileUtils.mkdir_p(pids_dir) unless File.exist?(pids_dir)
465
- # we need everybody to be able to write to the pids_dir as processes managed by
466
- # bluepill will be writing to this dir after they've dropped privileges
467
- 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
468
289
  end
469
290
 
470
- # Interrupt a daemon for this application that's still running
291
+ # Set the base directory, and the journal and pid paths in it
471
292
  #
472
- # It waits up to kill_timeout half seconds for the daemon to exit, and
473
- # exits with status 4 if it doesn't, or if signaling it fails.
293
+ # @param [String, nil] dir the base_dir option
474
294
  #
475
295
  # @return [void]
476
296
  #
477
297
  # @api private
478
- def kill_previous_bluepill
479
- return unless File.exist?(pid_file)
480
-
481
- # An empty or garbled pid file reads as 0, and signaling pid 0 would
482
- # interrupt bluepill's own process group
483
- previous_pid = File.read(pid_file).to_i
484
- return unless previous_pid > 0 && System.pid_alive?(previous_pid)
485
-
486
- ::Process.kill(0, previous_pid)
487
- puts "Killing previous bluepilld[#{previous_pid}]"
488
- ::Process.kill(2, previous_pid)
489
- rescue StandardError => e
490
- $stderr.puts 'Encountered error trying to kill previous bluepill:'
491
- $stderr.puts "#{e.class}: #{e}"
492
- exit(4) unless e.instance_of?(Errno::ESRCH)
493
- else
494
- kill_timeout.times do |_i|
495
- sleep 0.5
496
- break unless System.pid_alive?(previous_pid)
497
- end
498
-
499
- if System.pid_alive?(previous_pid)
500
- $stderr.puts "Previous bluepilld[#{previous_pid}] didn't die"
501
- exit(4)
502
- 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)
503
304
  end
504
305
 
505
- # Write the daemon's pid to its pid file
306
+ # Return the base directory without an option or BLUEPILL_BASE_DIR
506
307
  #
507
- # @return [void]
308
+ # @return [String] /var/run/bluepill for root, or ~/.bluepill
508
309
  #
509
310
  # @api private
510
- def write_pid_file
511
- 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")
512
315
  end
513
316
  end
514
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