bluepill 0.1.6 → 0.2.1

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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +106 -9
  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 +56 -254
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/clock.rb +25 -0
  13. data/lib/bluepill/condition_watch.rb +136 -58
  14. data/lib/bluepill/controller/directory.rb +59 -0
  15. data/lib/bluepill/controller.rb +99 -94
  16. data/lib/bluepill/daemonize.rb +234 -0
  17. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  18. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  19. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  20. data/lib/bluepill/dsl/process_factory.rb +81 -92
  21. data/lib/bluepill/dsl/process_proxy.rb +109 -21
  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 +60 -657
  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 +52 -24
  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 -26
  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 +33 -36
  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 +50 -871
  74. metadata +40 -45
@@ -1,6 +1,10 @@
1
- require 'thread'
2
- require 'bluepill/system'
3
- require 'bluepill/process_journal'
1
+ require "bluepill/system"
2
+ require "bluepill/process_journal"
3
+ require "bluepill/base_directory"
4
+ require "bluepill/pid_file"
5
+ require "bluepill/application/listener"
6
+ require "bluepill/application/lifecycle"
7
+ require "bluepill/application/previous_daemon"
4
8
 
5
9
  module Bluepill
6
10
  # The bluepill daemon for one application
@@ -10,16 +14,20 @@ module Bluepill
10
14
  #
11
15
  # @api private
12
16
  class Application
17
+ include Listener
18
+ include Lifecycle
19
+ include PreviousDaemon
20
+
13
21
  # The commands that act on a process or group, and that the CLI sends
14
- PROCESS_COMMANDS = [:start, :stop, :restart, :unmonitor, :status].freeze
22
+ PROCESS_COMMANDS = %i[start stop restart unmonitor status].freeze
15
23
 
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
24
+ # The commands that the daemon answers on its socket, and no others
18
25
  #
19
- # @api private
20
- DAEMON_COMMANDS = (PROCESS_COMMANDS + [:version, :log_file]).freeze
26
+ # The CLI sends the PROCESS_COMMANDS, version to check the daemon's
27
+ # version, and log_file to find the log that it tails.
28
+ DAEMON_COMMANDS = (PROCESS_COMMANDS + %i[version log_file]).freeze
21
29
 
22
- attr_accessor :name, :logger, :base_dir, :socket, :pid_file, :kill_timeout, :groups, :work_queue, :pids_dir, :log_file
30
+ attr_accessor :name, :logger, :base_dir, :socket, :pid_file, :kill_timeout, :groups, :pids_dir, :log_file
23
31
 
24
32
  # @!attribute [rw] name
25
33
  # The application's name, which names its socket and pid file
@@ -70,13 +78,6 @@ module Bluepill
70
78
  #
71
79
  # @api private
72
80
 
73
- # @!attribute [rw] work_queue
74
- # An attribute that bluepill doesn't use
75
- #
76
- # @return [Object]
77
- #
78
- # @api private
79
-
80
81
  # @!attribute [rw] pids_dir
81
82
  # The directory with the pid files of the application's processes
82
83
  #
@@ -98,7 +99,7 @@ module Bluepill
98
99
  #
99
100
  # @param [String] name the application's name
100
101
  # @param [Hash{Symbol => Object}] options :foreground, :log_file,
101
- # :base_dir and :kill_timeout
102
+ # :log_level, :base_dir and :kill_timeout
102
103
  #
103
104
  # @return [void]
104
105
  #
@@ -106,16 +107,10 @@ module Bluepill
106
107
  def initialize(name, options = {})
107
108
  self.name = name
108
109
 
109
- @foreground = options[:foreground]
110
+ @foreground = options[:foreground]
110
111
  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)
112
+ @log_level = options[:log_level]
113
+ setup_base_dir(options[:base_dir])
119
114
  self.kill_timeout = options[:kill_timeout] || 10
120
115
 
121
116
  self.groups = {}
@@ -145,7 +140,7 @@ module Bluepill
145
140
  # @return [Object] what the block returns
146
141
  #
147
142
  # @api private
148
- def mutex(&block)
143
+ def synchronize(&block)
149
144
  @mutex.synchronize(&block)
150
145
  end
151
146
 
@@ -159,9 +154,9 @@ module Bluepill
159
154
  # @api private
160
155
  def load
161
156
  start_server
162
- rescue StandardError => e
163
- $stderr.puts('Failed to start bluepill:')
164
- $stderr.puts(format('%s `%s`', e.class.name, e.message))
157
+ rescue => e
158
+ $stderr.puts("Failed to start bluepill:")
159
+ $stderr.puts(format("%s `%s`", e.class.name, e.message))
165
160
  $stderr.puts(e.backtrace)
166
161
  exit(5)
167
162
  end
@@ -223,11 +218,10 @@ module Bluepill
223
218
 
224
219
  # Define a method for each of the PROCESS_COMMANDS
225
220
  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
221
+ define_method(command) do |group_name = nil, process_name = nil|
222
+ # @type self: Application
223
+ send_to_process_or_group(command, group_name, process_name)
224
+ end
231
225
  end
232
226
 
233
227
  # Add a process to a group, which is created if it's new
@@ -240,6 +234,8 @@ module Bluepill
240
234
  # @api private
241
235
  def add_process(process, group_name = nil)
242
236
  group_name = group_name.to_s if group_name
237
+ process.app_name = name
238
+ process.group_name = group_name
243
239
 
244
240
  groups[group_name] ||= Group.new(group_name, logger: logger.prefix_with(group_name))
245
241
  groups.fetch(group_name).add_process(process)
@@ -254,7 +250,7 @@ module Bluepill
254
250
  Version
255
251
  end
256
252
 
257
- protected
253
+ protected
258
254
 
259
255
  # Send a command to a process, a group, or every process
260
256
  #
@@ -268,247 +264,53 @@ module Bluepill
268
264
  # @api private
269
265
  def send_to_process_or_group(method, group_name, process_name)
270
266
  if group_name.nil? && process_name.nil?
271
- groups.values.collect do |group|
272
- group.public_send(method)
273
- end.flatten
267
+ send_to_groups(method)
274
268
  elsif groups.key?(group_name)
275
269
  groups.fetch(group_name).public_send(method, process_name)
276
270
  else
277
271
  # 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
272
+ send_to_groups(method, [group_name, process_name].compact.join(":"))
414
273
  end
415
- System.delete_if_exists(pid_file)
416
274
  end
417
275
 
418
- # Trap TERM and INT to clean up and stop, and HUP to reopen the log
419
- #
420
- # @return [void]
276
+ # Send a command to every group
421
277
  #
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
278
+ # @param [Symbol] method the command
279
+ # @param [Array<String>] args the process, or none for every process
459
280
  #
460
- # @return [void]
281
+ # @return [Array<String>] what the groups returned, flattened
461
282
  #
462
283
  # @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)
284
+ def send_to_groups(method, *args)
285
+ groups.values.collect do |group|
286
+ group.public_send(method, *args)
287
+ end.flatten
468
288
  end
469
289
 
470
- # Interrupt a daemon for this application that's still running
290
+ # Set the base directory, and the journal and pid paths in it
471
291
  #
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.
292
+ # @param [String, nil] dir the base_dir option
474
293
  #
475
294
  # @return [void]
476
295
  #
477
296
  # @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
297
+ def setup_base_dir(dir)
298
+ self.base_dir = dir || ENV["BLUEPILL_BASE_DIR"] || default_base_dir
299
+ ProcessJournal.setup(base_dir, name)
300
+ paths = BaseDirectory.new(base_dir)
301
+ self.pid_file = paths.pid_file(name)
302
+ self.pids_dir = paths.process_pids_dir(name)
503
303
  end
504
304
 
505
- # Write the daemon's pid to its pid file
305
+ # Return the base directory without an option or BLUEPILL_BASE_DIR
506
306
  #
507
- # @return [void]
307
+ # @return [String] /var/run/bluepill for root, or ~/.bluepill
508
308
  #
509
309
  # @api private
510
- def write_pid_file
511
- File.open(pid_file, 'w') { |x| x.write(::Process.pid) }
310
+ def default_base_dir
311
+ # Only used for a non-root user, who has a home
312
+ home = ENV.fetch("HOME", nil) #: String
313
+ ::Process.euid.zero? ? "/var/run/bluepill" : File.join(home, ".bluepill")
512
314
  end
513
315
  end
514
316
  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
@@ -0,0 +1,25 @@
1
+ module Bluepill
2
+ # The clock that times ticks, checks and grace times
3
+ #
4
+ # The system clock can jump, when NTP corrects it or someone sets it, so
5
+ # a time between two readings of Time.now can come out too long, too
6
+ # short or below zero. The monotonic clock only counts up, one second a
7
+ # second.
8
+ #
9
+ # @api private
10
+ module Clock
11
+ # Return the monotonic clock's time, in whole seconds
12
+ #
13
+ # It counts from an arbitrary point, such as when the machine booted, so
14
+ # a reading only means something next to another one. JRuby 9.0 has no
15
+ # :second unit, so it reads the clock in seconds, as a Float, and drops
16
+ # the fraction.
17
+ #
18
+ # @return [Integer]
19
+ #
20
+ # @api private
21
+ def self.now
22
+ Integer(::Process.clock_gettime(::Process::CLOCK_MONOTONIC))
23
+ end
24
+ end
25
+ end