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
@@ -0,0 +1,166 @@
1
+ module Bluepill
2
+ class Application
3
+ # Start the daemon, run it until a signal stops it, and clean up after
4
+ #
5
+ # Application is the only class that includes it.
6
+ #
7
+ # @api private
8
+ module Lifecycle
9
+ protected
10
+
11
+ # Start the daemon
12
+ #
13
+ # It stops a previous daemon for the application, clears journal locks
14
+ # it left behind, daemonizes unless it runs in the foreground, opens the
15
+ # socket, and runs until a signal stops it. Processes that are already
16
+ # running, from their pid files, are monitored again, not restarted, so
17
+ # bluepill can be upgraded without restarting them.
18
+ #
19
+ # @return [void]
20
+ #
21
+ # @api private
22
+ def start_server
23
+ kill_previous_bluepill
24
+ ProcessJournal.clear_all_atomic_fs_locks
25
+ become_process_group_leader
26
+ daemonize_and_trap_signals unless foreground?
27
+ logger.reopen
28
+
29
+ $0 = "bluepilld: #{name}"
30
+
31
+ groups.each_value(&:determine_initial_state)
32
+ serve
33
+ end
34
+
35
+ # Make the daemon its own process group's leader, if it may
36
+ #
37
+ # @return [void]
38
+ #
39
+ # @api private
40
+ def become_process_group_leader
41
+ ::Process.setpgid(0, 0)
42
+ rescue Errno::EPERM
43
+ # A session leader can't, and it already leads its process group
44
+ end
45
+
46
+ # Daemonize, and trap the signals again
47
+ #
48
+ # @return [void]
49
+ #
50
+ # @api private
51
+ def daemonize_and_trap_signals
52
+ Daemonize.daemonize
53
+ # Daemonizing ignores HUP, so trap it again to reopen the log on HUP
54
+ setup_signal_traps
55
+ end
56
+
57
+ # Write the pid file, open the socket, and run the daemon
58
+ #
59
+ # @return [void]
60
+ #
61
+ # @api private
62
+ def serve
63
+ write_pid_file
64
+ self.socket = Socket.server(base_dir, name)
65
+ start_listener
66
+
67
+ run
68
+ end
69
+
70
+ # Tick every group once a second, until a signal stops the daemon
71
+ #
72
+ # @return [void]
73
+ #
74
+ # @api private
75
+ def run
76
+ @running = true # set to false by signal trap
77
+ while @running
78
+ synchronize do
79
+ System.reset_data
80
+ groups.each_value(&:tick)
81
+ end
82
+ # Kernel.sleep, not sleep, so the specs can stand in for the clock
83
+ Kernel.sleep(1)
84
+ end
85
+ end
86
+
87
+ # Clear the app's journal locks, and remove the socket and pid file
88
+ #
89
+ # The processes keep running, so the next daemon monitors them again.
90
+ #
91
+ # @return [void]
92
+ #
93
+ # @api private
94
+ def cleanup
95
+ ProcessJournal.clear_all_atomic_fs_locks
96
+ begin
97
+ System.delete_if_exists(socket.path) if socket
98
+ rescue IOError
99
+ # A closed socket can't tell its path
100
+ end
101
+ System.delete_if_exists(pid_file)
102
+ end
103
+
104
+ # Trap TERM and INT to clean up and stop, and HUP to reopen the log
105
+ #
106
+ # @return [void]
107
+ #
108
+ # @api private
109
+ def setup_signal_traps
110
+ terminator = proc do
111
+ puts "Terminating..."
112
+ cleanup
113
+ @running = false
114
+ end #: ^(Integer signal) -> void
115
+
116
+ Signal.trap("TERM", &terminator)
117
+ Signal.trap("INT", &terminator)
118
+
119
+ Signal.trap("HUP") do
120
+ logger.reopen if logger
121
+ end
122
+ end
123
+
124
+ # Create the logger and the pids directory
125
+ #
126
+ # Bluepill opens its log file and creates its pids dir as the user that
127
+ # runs it. The uid and gid in the config only apply to its processes.
128
+ # It exits with status 5 if it can't.
129
+ #
130
+ # @return [void]
131
+ #
132
+ # @api private
133
+ def setup_logger_and_pids_dir
134
+ self.logger = ProcessJournal.logger = Logger.new(log_file: log_file, log_level: @log_level, stdout: foreground?).prefix_with(name)
135
+ setup_pids_dir
136
+ rescue SystemCallError => e
137
+ $stderr.puts("Failed to start bluepill: #{e}")
138
+ message = "The user running bluepill must be able to write to its log_file and base_dir. " \
139
+ "The uid and gid in the config only apply to the processes it monitors."
140
+ $stderr.puts(message)
141
+ exit(5)
142
+ end
143
+
144
+ # Create the pids directory, and let every user write to it
145
+ #
146
+ # @return [void]
147
+ #
148
+ # @api private
149
+ def setup_pids_dir
150
+ FileUtils.mkdir_p(pids_dir)
151
+ # we need everybody to be able to write to the pids_dir as processes managed by
152
+ # bluepill will be writing to this dir after they've dropped privileges
153
+ FileUtils.chmod(0o777, pids_dir)
154
+ end
155
+
156
+ # Write the daemon's pid to its pid file
157
+ #
158
+ # @return [void]
159
+ #
160
+ # @api private
161
+ def write_pid_file
162
+ File.write(pid_file, ::Process.pid.to_s)
163
+ end
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,124 @@
1
+ module Bluepill
2
+ class Application
3
+ # Answer the commands that the CLI sends over the daemon's socket
4
+ #
5
+ # Application is the only class that includes it.
6
+ #
7
+ # @api private
8
+ module Listener
9
+ protected
10
+
11
+ # Start the thread that answers commands on the socket
12
+ #
13
+ # It kills the thread that a previous call started. Each command is a
14
+ # line, such as start:group:process, and the answer is the command's
15
+ # result, or the error it raised, marshaled.
16
+ #
17
+ # @return [Thread] the listener thread
18
+ #
19
+ # @api private
20
+ def start_listener
21
+ previous_thread = @listener_thread
22
+ previous_thread.kill if previous_thread
23
+ @listener_thread = Thread.new do
24
+ loop do
25
+ answer_next_client
26
+ end
27
+ end
28
+ end
29
+
30
+ # Accept a client on the socket, answer its command, and close it
31
+ #
32
+ # An error is logged, so the listener keeps going.
33
+ #
34
+ # @return [void]
35
+ #
36
+ # @api private
37
+ def answer_next_client
38
+ client = nil #: UNIXSocket?
39
+ begin
40
+ server = socket #: UNIXServer
41
+ client = server.accept
42
+ answer_command(client)
43
+ rescue => e
44
+ log_listener_error(e)
45
+ ensure
46
+ close_client(client)
47
+ end
48
+ end
49
+
50
+ # Read a client's command, and write it the marshaled answer
51
+ #
52
+ # @param [UNIXSocket] client the client
53
+ #
54
+ # @return [void]
55
+ #
56
+ # @api private
57
+ def answer_command(client)
58
+ client.close_on_exec = true
59
+ # The target is group:process and the process name may contain ':'
60
+ command, *args = client.readline.strip.split(":", 3).reject(&:empty?)
61
+ client.write(Marshal.dump(command_result(command, args)))
62
+ end
63
+
64
+ # Run a command while holding the daemon's lock, and return its result
65
+ #
66
+ # @param [String, nil] command the command
67
+ # @param [Array<String>] args the group and process, for example
68
+ #
69
+ # @return [Object] what the command returned, or the error it raised
70
+ #
71
+ # @api private
72
+ def command_result(command, args)
73
+ method = daemon_command(command)
74
+ synchronize { public_send(method, *args) }
75
+ rescue => e
76
+ e
77
+ end
78
+
79
+ # Return the one of the DAEMON_COMMANDS that a client's command names
80
+ #
81
+ # Anyone who can write to the socket can send any line, so only these
82
+ # commands may run, and not any other of the daemon's public methods.
83
+ #
84
+ # @param [String, nil] command the command, or nil for an empty line
85
+ #
86
+ # @raise [NoMethodError] if it names none of them
87
+ #
88
+ # @return [Symbol]
89
+ #
90
+ # @api private
91
+ def daemon_command(command)
92
+ # The backtrace is caller's lines, with no locations, so Ruby 3.1's
93
+ # error_highlight doesn't add a snippet of this file to the message
94
+ DAEMON_COMMANDS.detect { |name| name.to_s.eql?(command) } ||
95
+ raise(NoMethodError, "Unknown command `#{command}`", caller)
96
+ end
97
+
98
+ # Log an error that answering a client raised, with its backtrace
99
+ #
100
+ # @param [StandardError] error the error
101
+ #
102
+ # @return [void]
103
+ #
104
+ # @api private
105
+ def log_listener_error(error)
106
+ logger.err(format("Got exception in cmd listener: %s `%s`", error.class.name, error.message))
107
+ Array(error.backtrace).each { |l| logger.err(l) }
108
+ end
109
+
110
+ # Close a client, unless accepting it failed
111
+ #
112
+ # @param [UNIXSocket, nil] client the client, or nil
113
+ #
114
+ # @return [void]
115
+ #
116
+ # @api private
117
+ def close_client(client)
118
+ client.close if client
119
+ rescue IOError
120
+ # closed stream
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,81 @@
1
+ module Bluepill
2
+ class Application
3
+ # Stop a daemon for the application that's still running, before
4
+ # starting a new one
5
+ #
6
+ # Application is the only class that includes it.
7
+ #
8
+ # @api private
9
+ module PreviousDaemon
10
+ protected
11
+
12
+ # Interrupt a daemon for this application that's still running
13
+ #
14
+ # It waits up to kill_timeout half seconds for the daemon to exit, and
15
+ # exits with status 4 if it doesn't, or if signaling it fails.
16
+ #
17
+ # @return [void]
18
+ #
19
+ # @api private
20
+ def kill_previous_bluepill
21
+ previous_pid = interrupt_previous_bluepill
22
+ wait_for_previous_bluepill(previous_pid) if previous_pid
23
+ end
24
+
25
+ # Interrupt the previous daemon, if its pid file names a live one
26
+ #
27
+ # It exits with status 4 if signaling it fails, unless it's gone.
28
+ #
29
+ # @return [Integer, nil] the previous daemon's pid, or nil if it
30
+ # interrupted none
31
+ #
32
+ # @api private
33
+ def interrupt_previous_bluepill
34
+ previous_pid = previous_bluepill_pid
35
+ return unless previous_pid
36
+
37
+ ::Process.kill(0, previous_pid)
38
+ puts "Killing previous bluepilld[#{previous_pid}]"
39
+ ::Process.kill(2, previous_pid)
40
+ previous_pid
41
+ rescue => e
42
+ $stderr.puts "Encountered error trying to kill previous bluepill:"
43
+ $stderr.puts "#{e.class}: #{e}"
44
+ exit(4) unless e.instance_of?(Errno::ESRCH)
45
+ end
46
+
47
+ # Return the pid of a previous daemon that's still running
48
+ #
49
+ # @return [Integer, nil] the pid in the pid file, or nil without a
50
+ # pid file or a live pid
51
+ #
52
+ # @api private
53
+ def previous_bluepill_pid
54
+ previous_pid = PidFile.read(pid_file)
55
+ previous_pid if previous_pid && System.pid_alive?(previous_pid)
56
+ end
57
+
58
+ # Wait up to kill_timeout half seconds for the previous daemon to exit
59
+ #
60
+ # It exits with status 4 if the daemon is still running after.
61
+ #
62
+ # @param [Integer] previous_pid the previous daemon's pid
63
+ #
64
+ # @return [void]
65
+ #
66
+ # @api private
67
+ def wait_for_previous_bluepill(previous_pid)
68
+ kill_timeout.times do |_i|
69
+ # Kernel.sleep, not sleep, so the specs can stand in for the clock
70
+ Kernel.sleep(0.5)
71
+ break unless System.pid_alive?(previous_pid)
72
+ end
73
+
74
+ return unless System.pid_alive?(previous_pid)
75
+
76
+ $stderr.puts "Previous bluepilld[#{previous_pid}] didn't die"
77
+ exit(4)
78
+ end
79
+ end
80
+ end
81
+ end