bluepill 0.1.5 → 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 +57 -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 +59 -231
  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 -869
  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,10 +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
24
+
25
+ # The commands that the daemon answers on its socket, and no others
26
+ #
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
15
30
 
16
- 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
17
32
 
18
33
  # @!attribute [rw] name
19
34
  # The application's name, which names its socket and pid file
@@ -64,13 +79,6 @@ module Bluepill
64
79
  #
65
80
  # @api private
66
81
 
67
- # @!attribute [rw] work_queue
68
- # An attribute that bluepill doesn't use
69
- #
70
- # @return [Object]
71
- #
72
- # @api private
73
-
74
82
  # @!attribute [rw] pids_dir
75
83
  # The directory with the pid files of the application's processes
76
84
  #
@@ -92,7 +100,7 @@ module Bluepill
92
100
  #
93
101
  # @param [String] name the application's name
94
102
  # @param [Hash{Symbol => Object}] options :foreground, :log_file,
95
- # :base_dir and :kill_timeout
103
+ # :log_level, :base_dir and :kill_timeout
96
104
  #
97
105
  # @return [void]
98
106
  #
@@ -100,16 +108,10 @@ module Bluepill
100
108
  def initialize(name, options = {})
101
109
  self.name = name
102
110
 
103
- @foreground = options[:foreground]
111
+ @foreground = options[:foreground]
104
112
  self.log_file = options[:log_file]
105
- # Only used for a non-root user, who has a home
106
- home = ENV['HOME'] #: String
107
- self.base_dir = options[:base_dir] ||
108
- ENV['BLUEPILL_BASE_DIR'] ||
109
- (::Process.euid.zero? ? '/var/run/bluepill' : File.join(home, '.bluepill'))
110
- ProcessJournal.setup(base_dir, name)
111
- self.pid_file = File.join(base_dir, 'pids', "#{name}.pid")
112
- self.pids_dir = File.join(base_dir, 'pids', name)
113
+ @log_level = options[:log_level]
114
+ setup_base_dir(options[:base_dir])
113
115
  self.kill_timeout = options[:kill_timeout] || 10
114
116
 
115
117
  self.groups = {}
@@ -139,7 +141,7 @@ module Bluepill
139
141
  # @return [Object] what the block returns
140
142
  #
141
143
  # @api private
142
- def mutex(&block)
144
+ def synchronize(&block)
143
145
  @mutex.synchronize(&block)
144
146
  end
145
147
 
@@ -153,9 +155,9 @@ module Bluepill
153
155
  # @api private
154
156
  def load
155
157
  start_server
156
- rescue StandardError => e
157
- $stderr.puts('Failed to start bluepill:')
158
- $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))
159
161
  $stderr.puts(e.backtrace)
160
162
  exit(5)
161
163
  end
@@ -217,11 +219,10 @@ module Bluepill
217
219
 
218
220
  # Define a method for each of the PROCESS_COMMANDS
219
221
  PROCESS_COMMANDS.each do |command|
220
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
221
- def #{command}(group_name = nil, process_name = nil)
222
- self.send_to_process_or_group(:#{command}, group_name, process_name)
223
- end
224
- 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
225
226
  end
226
227
 
227
228
  # Add a process to a group, which is created if it's new
@@ -234,6 +235,8 @@ module Bluepill
234
235
  # @api private
235
236
  def add_process(process, group_name = nil)
236
237
  group_name = group_name.to_s if group_name
238
+ process.app_name = name
239
+ process.group_name = group_name
237
240
 
238
241
  groups[group_name] ||= Group.new(group_name, logger: logger.prefix_with(group_name))
239
242
  groups.fetch(group_name).add_process(process)
@@ -248,7 +251,7 @@ module Bluepill
248
251
  Version
249
252
  end
250
253
 
251
- protected
254
+ protected
252
255
 
253
256
  # Send a command to a process, a group, or every process
254
257
  #
@@ -262,228 +265,53 @@ module Bluepill
262
265
  # @api private
263
266
  def send_to_process_or_group(method, group_name, process_name)
264
267
  if group_name.nil? && process_name.nil?
265
- groups.values.collect do |group|
266
- group.public_send(method)
267
- end.flatten
268
+ send_to_groups(method)
268
269
  elsif groups.key?(group_name)
269
270
  groups.fetch(group_name).public_send(method, process_name)
270
271
  else
271
272
  # they must be targeting just by process name, which may contain ':'
272
- process_name = [group_name, process_name].compact.join(':')
273
- groups.values.collect do |group|
274
- group.public_send(method, process_name)
275
- end.flatten
276
- end
277
- end
278
-
279
- # Start the thread that answers commands on the socket
280
- #
281
- # It kills the thread that a previous call started. Each command is a
282
- # line, such as start:group:process, and the answer is the command's
283
- # result, or the error it raised, marshaled.
284
- #
285
- # @return [Thread] the listener thread
286
- #
287
- # @api private
288
- def start_listener
289
- previous_thread = @listener_thread
290
- previous_thread.kill if previous_thread
291
- @listener_thread = Thread.new do
292
- loop do
293
- client = nil #: UNIXSocket?
294
- begin
295
- server = socket #: UNIXServer
296
- client = server.accept
297
- client.close_on_exec = true if client.respond_to?(:close_on_exec=)
298
- # The target is group:process and the process name may contain ':'
299
- command, *args = client.readline.strip.split(':', 3).reject(&:empty?)
300
- response = begin
301
- # An empty line leaves command nil, so public_send raises an error for the client
302
- mutex { public_send(command, *args) } # steep:ignore ArgumentTypeMismatch
303
- rescue StandardError => e
304
- e
305
- end
306
- client.write(Marshal.dump(response))
307
- rescue StandardError => e
308
- logger.err(format('Got exception in cmd listener: %s `%s`', e.class.name, e.message))
309
- Array(e.backtrace).each { |l| logger.err(l) }
310
- ensure
311
- begin
312
- # Ruby before 3.0 never counts branches in ensure as covered
313
- client && client.close
314
- rescue IOError
315
- # closed stream
316
- end
317
- end
318
- end
319
- end
320
- end
321
-
322
- # Start the daemon
323
- #
324
- # It stops a previous daemon for the application, clears journal locks
325
- # it left behind, daemonizes unless it runs in the foreground, opens the
326
- # socket, and runs until a signal stops it. Processes that are already
327
- # running, from their pid files, are monitored again, not restarted, so
328
- # bluepill can be upgraded without restarting them.
329
- #
330
- # @return [void]
331
- #
332
- # @api private
333
- def start_server
334
- kill_previous_bluepill
335
- ProcessJournal.clear_all_atomic_fs_locks
336
-
337
- begin
338
- ::Process.setpgid(0, 0)
339
- rescue Errno::EPERM
340
- end
341
-
342
- unless foreground?
343
- Daemonize.daemonize
344
- # Daemonizing ignores HUP, so trap it again to reopen the log on HUP
345
- setup_signal_traps
273
+ send_to_groups(method, [group_name, process_name].compact.join(":"))
346
274
  end
347
-
348
- logger.reopen
349
-
350
- $0 = "bluepilld: #{name}"
351
-
352
- groups.each_value(&:determine_initial_state)
353
-
354
- write_pid_file
355
- self.socket = Socket.server(base_dir, name)
356
- start_listener
357
-
358
- run
359
275
  end
360
276
 
361
- # Tick every group once a second, until a signal stops the daemon
362
- #
363
- # @return [void]
277
+ # Send a command to every group
364
278
  #
365
- # @api private
366
- def run
367
- @running = true # set to false by signal trap
368
- while @running
369
- mutex do
370
- System.reset_data
371
- groups.each_value(&:tick)
372
- end
373
- sleep 1
374
- end
375
- end
376
-
377
- # Clear the app's journal locks, and remove the socket and pid file
378
- #
379
- # The processes keep running, so the next daemon monitors them again.
380
- #
381
- # @return [void]
382
- #
383
- # @api private
384
- def cleanup
385
- ProcessJournal.clear_all_atomic_fs_locks
386
- begin
387
- System.delete_if_exists(socket.path) if socket
388
- rescue IOError
389
- end
390
- System.delete_if_exists(pid_file)
391
- end
392
-
393
- # Trap TERM and INT to clean up and stop, and HUP to reopen the log
394
- #
395
- # @return [void]
396
- #
397
- # @api private
398
- def setup_signal_traps
399
- terminator = proc do
400
- puts 'Terminating...'
401
- cleanup
402
- @running = false
403
- end #: ^(Integer signal) -> void
404
-
405
- Signal.trap('TERM', &terminator)
406
- Signal.trap('INT', &terminator)
407
-
408
- Signal.trap('HUP') do
409
- logger.reopen if logger
410
- end
411
- end
412
-
413
- # Create the logger and the pids directory
414
- #
415
- # Bluepill opens its log file and creates its pids dir as the user that
416
- # runs it. The uid and gid in the config only apply to its processes.
417
- # It exits with status 5 if it can't.
418
- #
419
- # @return [void]
420
- #
421
- # @api private
422
- def setup_logger_and_pids_dir
423
- self.logger = ProcessJournal.logger = Logger.new(log_file: log_file, stdout: foreground?).prefix_with(name)
424
- setup_pids_dir
425
- rescue SystemCallError => e
426
- $stderr.puts("Failed to start bluepill: #{e}")
427
- message = 'The user running bluepill must be able to write to its log_file and base_dir. ' \
428
- 'The uid and gid in the config only apply to the processes it monitors.'
429
- $stderr.puts(message)
430
- exit(5)
431
- end
432
-
433
- # 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
434
281
  #
435
- # @return [void]
282
+ # @return [Array<String>] what the groups returned, flattened
436
283
  #
437
284
  # @api private
438
- def setup_pids_dir
439
- FileUtils.mkdir_p(pids_dir) unless File.exist?(pids_dir)
440
- # we need everybody to be able to write to the pids_dir as processes managed by
441
- # bluepill will be writing to this dir after they've dropped privileges
442
- 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
443
289
  end
444
290
 
445
- # Interrupt a daemon for this application that's still running
291
+ # Set the base directory, and the journal and pid paths in it
446
292
  #
447
- # It waits up to kill_timeout half seconds for the daemon to exit, and
448
- # exits with status 4 if it doesn't, or if signaling it fails.
293
+ # @param [String, nil] dir the base_dir option
449
294
  #
450
295
  # @return [void]
451
296
  #
452
297
  # @api private
453
- def kill_previous_bluepill
454
- return unless File.exist?(pid_file)
455
-
456
- # An empty or garbled pid file reads as 0, and signaling pid 0 would
457
- # interrupt bluepill's own process group
458
- previous_pid = File.read(pid_file).to_i
459
- return unless previous_pid > 0 && System.pid_alive?(previous_pid)
460
-
461
- ::Process.kill(0, previous_pid)
462
- puts "Killing previous bluepilld[#{previous_pid}]"
463
- ::Process.kill(2, previous_pid)
464
- rescue StandardError => e
465
- $stderr.puts 'Encountered error trying to kill previous bluepill:'
466
- $stderr.puts "#{e.class}: #{e}"
467
- exit(4) unless e.instance_of?(Errno::ESRCH)
468
- else
469
- kill_timeout.times do |_i|
470
- sleep 0.5
471
- break unless System.pid_alive?(previous_pid)
472
- end
473
-
474
- if System.pid_alive?(previous_pid)
475
- $stderr.puts "Previous bluepilld[#{previous_pid}] didn't die"
476
- exit(4)
477
- 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)
478
304
  end
479
305
 
480
- # Write the daemon's pid to its pid file
306
+ # Return the base directory without an option or BLUEPILL_BASE_DIR
481
307
  #
482
- # @return [void]
308
+ # @return [String] /var/run/bluepill for root, or ~/.bluepill
483
309
  #
484
310
  # @api private
485
- def write_pid_file
486
- 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")
487
315
  end
488
316
  end
489
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