bluepill 0.2.7 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +144 -1
- data/README.md +17 -10
- data/bin/bluepill +8 -2
- data/bluepill.gemspec +1 -1
- data/lib/bluepill/application/lifecycle.rb +43 -31
- data/lib/bluepill/application/listener.rb +1 -1
- data/lib/bluepill/application/previous_daemon.rb +14 -14
- data/lib/bluepill/application.rb +48 -28
- data/lib/bluepill/base_directory.rb +18 -20
- data/lib/bluepill/check_registry.rb +104 -0
- data/lib/bluepill/clock.rb +1 -3
- data/lib/bluepill/condition_watch.rb +61 -67
- data/lib/bluepill/controller/directory.rb +1 -3
- data/lib/bluepill/controller.rb +46 -38
- data/lib/bluepill/dsl/app_proxy.rb +69 -58
- data/lib/bluepill/dsl/attributes.rb +135 -0
- data/lib/bluepill/dsl/process_factory/child_factory.rb +80 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +6 -26
- data/lib/bluepill/dsl/process_factory.rb +86 -70
- data/lib/bluepill/dsl/process_proxy.rb +61 -158
- data/lib/bluepill/dsl.rb +15 -6
- data/lib/bluepill/errors.rb +190 -0
- data/lib/bluepill/group.rb +14 -37
- data/lib/bluepill/logger.rb +55 -97
- data/lib/bluepill/process/attributes.rb +66 -32
- data/lib/bluepill/process/children.rb +4 -6
- data/lib/bluepill/process/pid.rb +41 -35
- data/lib/bluepill/process/state_machine.rb +15 -59
- data/lib/bluepill/process/stop_signals.rb +120 -0
- data/lib/bluepill/process/transition.rb +1 -3
- data/lib/bluepill/process/transition_callbacks.rb +2 -19
- data/lib/bluepill/process/triggers.rb +90 -0
- data/lib/bluepill/process/user_commands.rb +1 -1
- data/lib/bluepill/process/watches.rb +9 -23
- data/lib/bluepill/process.rb +70 -55
- data/lib/bluepill/process_commands/execution.rb +14 -22
- data/lib/bluepill/process_commands/hooks.rb +1 -31
- data/lib/bluepill/process_commands/stop_signals.rb +12 -17
- data/lib/bluepill/process_commands.rb +10 -8
- data/lib/bluepill/process_conditions/always_true.rb +2 -6
- data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
- data/lib/bluepill/process_conditions/file_time.rb +8 -9
- data/lib/bluepill/process_conditions/http.rb +28 -43
- data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
- data/lib/bluepill/process_conditions/process_condition.rb +30 -32
- data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
- data/lib/bluepill/process_conditions.rb +5 -14
- data/lib/bluepill/process_journal/cleanup.rb +5 -53
- data/lib/bluepill/process_journal/config.rb +73 -0
- data/lib/bluepill/process_journal/entries.rb +128 -0
- data/lib/bluepill/process_journal/journal.rb +69 -0
- data/lib/bluepill/process_journal/kind.rb +60 -0
- data/lib/bluepill/process_journal/lock.rb +9 -4
- data/lib/bluepill/process_journal.rb +13 -324
- data/lib/bluepill/process_statistics.rb +3 -7
- data/lib/bluepill/socket.rb +19 -17
- data/lib/bluepill/system/command_options.rb +135 -0
- data/lib/bluepill/system/command_result.rb +73 -0
- data/lib/bluepill/system/command_setup.rb +3 -4
- data/lib/bluepill/system/commands.rb +21 -18
- data/lib/bluepill/system/daemon.rb +19 -14
- data/lib/bluepill/system/files.rb +35 -0
- data/lib/bluepill/system/ps.rb +20 -17
- data/lib/bluepill/system/ps_table.rb +7 -9
- data/lib/bluepill/system/usage.rb +155 -0
- data/lib/bluepill/system.rb +7 -192
- data/lib/bluepill/trigger.rb +22 -32
- data/lib/bluepill/triggers/flapping.rb +12 -8
- data/lib/bluepill/units.rb +7 -21
- data/lib/bluepill/util/log_error.rb +28 -0
- data/lib/bluepill/version.rb +12 -22
- data/lib/bluepill.rb +1 -0
- data/sig/bluepill/application.rbs +17 -12
- data/sig/bluepill/attributes.rbs +48 -0
- data/sig/bluepill/checks.rbs +43 -30
- data/sig/bluepill/dsl.rbs +98 -55
- data/sig/bluepill/errors.rbs +48 -0
- data/sig/bluepill/logger.rbs +15 -31
- data/sig/bluepill/process.rbs +42 -304
- data/sig/bluepill/process_commands.rbs +235 -0
- data/sig/bluepill/stop_signals.rbs +21 -0
- data/sig/bluepill/system.rbs +154 -99
- data/sig/bluepill/version.rbs +17 -0
- data/sig/bluepill.rbs +4 -17
- data/sig/manifest.yaml +1 -0
- metadata +22 -3
- data/lib/bluepill/dsl/process_factory/children.rb +0 -73
data/lib/bluepill/application.rb
CHANGED
|
@@ -96,29 +96,37 @@ module Bluepill
|
|
|
96
96
|
|
|
97
97
|
# Create the daemon for an application
|
|
98
98
|
#
|
|
99
|
-
# It
|
|
100
|
-
#
|
|
99
|
+
# It opens the log, so the pill file's processes can log through it.
|
|
100
|
+
# Starting the daemon traps signals and creates the pids directory.
|
|
101
101
|
#
|
|
102
102
|
# @param [String] name the application's name
|
|
103
|
-
# @param [
|
|
104
|
-
#
|
|
103
|
+
# @param [Boolean] foreground whether to run in the foreground, rather
|
|
104
|
+
# than as a daemon
|
|
105
|
+
# @param [String, nil] log_file the file to log to, or nil for syslog
|
|
106
|
+
# @param [Symbol, String] log_level the least severe priority to log
|
|
107
|
+
# (debug, so everything, by default)
|
|
108
|
+
# @param [String, nil] base_dir the directory for the socket and pid
|
|
109
|
+
# files, or nil for the default
|
|
110
|
+
# @param [Numeric] kill_timeout how long to wait for an earlier daemon
|
|
111
|
+
# to exit, in half seconds
|
|
105
112
|
#
|
|
106
113
|
# @return [void]
|
|
107
114
|
#
|
|
115
|
+
# @raise [StartupError] if it can't open its log file
|
|
116
|
+
#
|
|
108
117
|
# @api private
|
|
109
|
-
def initialize(name,
|
|
118
|
+
def initialize(name, foreground: false, log_file: nil, log_level: :debug, base_dir: nil, kill_timeout: 10)
|
|
110
119
|
self.name = name
|
|
111
120
|
|
|
112
|
-
@foreground =
|
|
113
|
-
self.log_file =
|
|
114
|
-
@log_level =
|
|
115
|
-
setup_base_dir(
|
|
116
|
-
self.kill_timeout =
|
|
121
|
+
@foreground = foreground
|
|
122
|
+
self.log_file = log_file
|
|
123
|
+
@log_level = log_level
|
|
124
|
+
setup_base_dir(base_dir)
|
|
125
|
+
self.kill_timeout = kill_timeout
|
|
117
126
|
|
|
118
127
|
self.groups = {}
|
|
119
128
|
|
|
120
|
-
|
|
121
|
-
setup_logger_and_pids_dir
|
|
129
|
+
setup_logger
|
|
122
130
|
|
|
123
131
|
@mutex = Mutex.new
|
|
124
132
|
end
|
|
@@ -128,9 +136,7 @@ module Bluepill
|
|
|
128
136
|
# @return [Boolean]
|
|
129
137
|
#
|
|
130
138
|
# @api private
|
|
131
|
-
def foreground?
|
|
132
|
-
!!@foreground
|
|
133
|
-
end
|
|
139
|
+
def foreground? = @foreground
|
|
134
140
|
|
|
135
141
|
# Run a block while holding the daemon's lock
|
|
136
142
|
#
|
|
@@ -142,25 +148,27 @@ module Bluepill
|
|
|
142
148
|
# @return [Object] what the block returns
|
|
143
149
|
#
|
|
144
150
|
# @api private
|
|
145
|
-
def synchronize(&block)
|
|
146
|
-
@mutex.synchronize(&block)
|
|
147
|
-
end
|
|
151
|
+
def synchronize(&block) = @mutex.synchronize(&block)
|
|
148
152
|
|
|
149
153
|
# Start the daemon and monitor the application's processes
|
|
150
154
|
#
|
|
151
|
-
# It
|
|
152
|
-
#
|
|
155
|
+
# It returns when a signal stops the daemon. Whatever stops it before
|
|
156
|
+
# then is logged at crit, with its backtrace, since a daemon's stderr
|
|
157
|
+
# is /dev/null, and raised as a {Bluepill::Error}.
|
|
153
158
|
#
|
|
154
159
|
# @return [void]
|
|
155
160
|
#
|
|
161
|
+
# @raise [Bluepill::Error] if the daemon fails to start, or stops
|
|
162
|
+
# without a signal, such as a {StartupError} for any other error
|
|
163
|
+
#
|
|
156
164
|
# @api private
|
|
157
165
|
def load
|
|
158
166
|
start_server
|
|
159
167
|
rescue => e
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
168
|
+
log_crash(e)
|
|
169
|
+
raise if e.is_a?(Error)
|
|
170
|
+
|
|
171
|
+
raise StartupError, ["Failed to start bluepill:", format("%s `%s`", e.class.name, e.message), e.backtrace].join("\n")
|
|
164
172
|
end
|
|
165
173
|
|
|
166
174
|
# @!method start(group_name = nil, process_name = nil)
|
|
@@ -245,15 +253,27 @@ module Bluepill
|
|
|
245
253
|
|
|
246
254
|
# Return the daemon's version, which the CLI checks against its own
|
|
247
255
|
#
|
|
248
|
-
#
|
|
256
|
+
# It's a String, since Marshal sends a module as only its name, which
|
|
257
|
+
# the CLI would load as its own Version.
|
|
258
|
+
#
|
|
259
|
+
# @return [String] the version, such as "0.3.0"
|
|
249
260
|
#
|
|
250
261
|
# @api private
|
|
251
|
-
def version
|
|
252
|
-
Version
|
|
253
|
-
end
|
|
262
|
+
def version = Version.to_s
|
|
254
263
|
|
|
255
264
|
protected
|
|
256
265
|
|
|
266
|
+
# Log an error that stopped the daemon at crit, with its backtrace
|
|
267
|
+
#
|
|
268
|
+
# @param [StandardError] error the error
|
|
269
|
+
#
|
|
270
|
+
# @return [void]
|
|
271
|
+
#
|
|
272
|
+
# @api private
|
|
273
|
+
def log_crash(error)
|
|
274
|
+
[format("Bluepill stopped: %s `%s`", error.class, error.message), *error.backtrace].each { |line| logger.crit(line) }
|
|
275
|
+
end
|
|
276
|
+
|
|
257
277
|
# Send a command to a process, a group, or every process
|
|
258
278
|
#
|
|
259
279
|
# @param [Symbol] method the command
|
|
@@ -4,8 +4,9 @@ module Bluepill
|
|
|
4
4
|
# The paths in bluepill's base directory
|
|
5
5
|
#
|
|
6
6
|
# The daemons' sockets are in socks, and their pid files are in pids,
|
|
7
|
-
# next to a directory for each application's processes. The
|
|
8
|
-
#
|
|
7
|
+
# next to a directory for each application's processes. The journals of
|
|
8
|
+
# the processes bluepill starts are in journals. The CLI and the daemon
|
|
9
|
+
# both find them here.
|
|
9
10
|
#
|
|
10
11
|
# @api private
|
|
11
12
|
class BaseDirectory
|
|
@@ -28,27 +29,30 @@ module Bluepill
|
|
|
28
29
|
# @return [void]
|
|
29
30
|
#
|
|
30
31
|
# @api private
|
|
31
|
-
def initialize(path)
|
|
32
|
-
@path = path
|
|
33
|
-
end
|
|
32
|
+
def initialize(path) = @path = path
|
|
34
33
|
|
|
35
34
|
# Return the directory with the daemons' sockets
|
|
36
35
|
#
|
|
37
36
|
# @return [String]
|
|
38
37
|
#
|
|
39
38
|
# @api private
|
|
40
|
-
def sockets_dir
|
|
41
|
-
File.join(path, "socks")
|
|
42
|
-
end
|
|
39
|
+
def sockets_dir = File.join(path, "socks")
|
|
43
40
|
|
|
44
41
|
# Return the directory with the daemons' pid files
|
|
45
42
|
#
|
|
46
43
|
# @return [String]
|
|
47
44
|
#
|
|
48
45
|
# @api private
|
|
49
|
-
def pids_dir
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
def pids_dir = File.join(path, "pids")
|
|
47
|
+
|
|
48
|
+
# Return the directory with the journals of the processes bluepill starts
|
|
49
|
+
#
|
|
50
|
+
# Each application's are in a directory of their own in it.
|
|
51
|
+
#
|
|
52
|
+
# @return [String]
|
|
53
|
+
#
|
|
54
|
+
# @api private
|
|
55
|
+
def journals_dir = File.join(path, "journals")
|
|
52
56
|
|
|
53
57
|
# Return the socket that an application's daemon listens on
|
|
54
58
|
#
|
|
@@ -57,9 +61,7 @@ module Bluepill
|
|
|
57
61
|
# @return [String]
|
|
58
62
|
#
|
|
59
63
|
# @api private
|
|
60
|
-
def socket_file(app)
|
|
61
|
-
File.join(sockets_dir, "#{app}#{SOCKET_EXTENSION}")
|
|
62
|
-
end
|
|
64
|
+
def socket_file(app) = File.join(sockets_dir, "#{app}#{SOCKET_EXTENSION}")
|
|
63
65
|
|
|
64
66
|
# Return an application's daemon's pid file
|
|
65
67
|
#
|
|
@@ -68,9 +70,7 @@ module Bluepill
|
|
|
68
70
|
# @return [String]
|
|
69
71
|
#
|
|
70
72
|
# @api private
|
|
71
|
-
def pid_file(app)
|
|
72
|
-
File.join(pids_dir, "#{app}.pid")
|
|
73
|
-
end
|
|
73
|
+
def pid_file(app) = File.join(pids_dir, "#{app}.pid")
|
|
74
74
|
|
|
75
75
|
# Return the directory with the pid files of an application's processes
|
|
76
76
|
#
|
|
@@ -79,9 +79,7 @@ module Bluepill
|
|
|
79
79
|
# @return [String]
|
|
80
80
|
#
|
|
81
81
|
# @api private
|
|
82
|
-
def process_pids_dir(app)
|
|
83
|
-
File.join(pids_dir, app)
|
|
84
|
-
end
|
|
82
|
+
def process_pids_dir(app) = File.join(pids_dir, app)
|
|
85
83
|
|
|
86
84
|
# Return the names of the applications with a socket
|
|
87
85
|
#
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "did_you_mean"
|
|
4
|
+
|
|
5
|
+
module Bluepill
|
|
6
|
+
# The checks and triggers a config can name, by their underscored class
|
|
7
|
+
# names
|
|
8
|
+
#
|
|
9
|
+
# Every subclass of ProcessConditions::ProcessCondition and of Trigger
|
|
10
|
+
# adds itself when it's defined, in whatever module, so `checks
|
|
11
|
+
# :log_size` finds a LogSize class anywhere. When two classes have the
|
|
12
|
+
# same name, the one defined last wins.
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
15
|
+
module CheckRegistry
|
|
16
|
+
# Each class, newest first, with the class it builds on and the methods
|
|
17
|
+
# it must define
|
|
18
|
+
@entries = [] #: Array[[Class, Class, Array[Symbol]]]
|
|
19
|
+
|
|
20
|
+
# Add a check or trigger class
|
|
21
|
+
#
|
|
22
|
+
# @param [Class] klass the class
|
|
23
|
+
# @param [Class] base ProcessConditions::ProcessCondition or Trigger
|
|
24
|
+
# @param [Array<Symbol>] required the methods of the base that the class
|
|
25
|
+
# must define
|
|
26
|
+
#
|
|
27
|
+
# @return [void]
|
|
28
|
+
#
|
|
29
|
+
# @api private
|
|
30
|
+
def self.add(klass, base, required) = @entries.unshift([klass, base, required])
|
|
31
|
+
|
|
32
|
+
# Return the check or trigger class a config names
|
|
33
|
+
#
|
|
34
|
+
# @param [Symbol, String] name the underscored name, such as :mem_usage
|
|
35
|
+
#
|
|
36
|
+
# @return [Class] the ProcessConditions::ProcessCondition or Trigger
|
|
37
|
+
# subclass
|
|
38
|
+
#
|
|
39
|
+
# @raise [ConfigError] when no class has the name, or the class doesn't
|
|
40
|
+
# define the methods a check or trigger needs
|
|
41
|
+
#
|
|
42
|
+
# @api private
|
|
43
|
+
def self.fetch(name)
|
|
44
|
+
key = name.to_s
|
|
45
|
+
entry = @entries.detect { |klass, _, _| check_name(klass).eql?(key) }
|
|
46
|
+
raise ConfigError, unknown_message(key) unless entry
|
|
47
|
+
|
|
48
|
+
klass, base, required = entry
|
|
49
|
+
missing = missing_methods(klass, base, required)
|
|
50
|
+
return klass if missing.empty?
|
|
51
|
+
|
|
52
|
+
raise ConfigError, "#{klass}, the class of check '#{key}', must define #{missing.join(" and ")}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Return the names a config can use, for suggestions
|
|
56
|
+
#
|
|
57
|
+
# @return [Array<String>] the name of every class that defines the
|
|
58
|
+
# methods it needs
|
|
59
|
+
#
|
|
60
|
+
# @api private
|
|
61
|
+
def self.names
|
|
62
|
+
@entries.filter_map { |klass, base, required| check_name(klass) if missing_methods(klass, base, required).empty? }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Return the name a config uses for a class
|
|
66
|
+
#
|
|
67
|
+
# @param [Class] klass the class
|
|
68
|
+
#
|
|
69
|
+
# @return [String, nil] its underscored name without its modules, such
|
|
70
|
+
# as "mem_usage", or nil for a class without a name
|
|
71
|
+
#
|
|
72
|
+
# @api private
|
|
73
|
+
def self.check_name(klass)
|
|
74
|
+
class_name = klass.name
|
|
75
|
+
Util.underscore(class_name.rpartition("::").last) if class_name
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Return the methods a class must define and doesn't
|
|
79
|
+
#
|
|
80
|
+
# @param [Class] klass the class
|
|
81
|
+
# @param [Class] base the class it builds on
|
|
82
|
+
# @param [Array<Symbol>] required the methods it must define
|
|
83
|
+
#
|
|
84
|
+
# @return [Array<Symbol>] the methods it leaves to its base
|
|
85
|
+
#
|
|
86
|
+
# @api private
|
|
87
|
+
def self.missing_methods(klass, base, required)
|
|
88
|
+
required.select { |method| klass.instance_method(method).owner.equal?(base) }
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Return the message for a name no class has, with a suggestion
|
|
92
|
+
#
|
|
93
|
+
# @param [String] name the name
|
|
94
|
+
#
|
|
95
|
+
# @return [String]
|
|
96
|
+
#
|
|
97
|
+
# @api private
|
|
98
|
+
def self.unknown_message(name)
|
|
99
|
+
suggestion = DidYouMean::SpellChecker.new(dictionary: names).correct(name).first
|
|
100
|
+
message = "Unknown check '#{name}'"
|
|
101
|
+
suggestion ? "#{message}. Did you mean '#{suggestion}'?" : message
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
data/lib/bluepill/clock.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "bluepill/check_registry"
|
|
4
|
+
require "bluepill/util/log_error"
|
|
5
|
+
|
|
3
6
|
module Bluepill
|
|
4
7
|
# Runs a process's check every so often, and fires its events when the
|
|
5
8
|
# check fails often enough
|
|
@@ -20,18 +23,17 @@ module Bluepill
|
|
|
20
23
|
#
|
|
21
24
|
# @api private
|
|
22
25
|
#
|
|
23
|
-
# @!attribute
|
|
24
|
-
# Return whether the value
|
|
26
|
+
# @!attribute passed
|
|
27
|
+
# Return whether the value passed the check
|
|
25
28
|
#
|
|
26
|
-
# It's
|
|
27
|
-
# counts toward the watch firing.
|
|
29
|
+
# It's false for a value that counts toward the watch firing.
|
|
28
30
|
#
|
|
29
31
|
# @return [Boolean] whether the value passed the check
|
|
30
32
|
#
|
|
31
33
|
# @api private
|
|
32
34
|
#
|
|
33
35
|
# @api private
|
|
34
|
-
HistoryValue = Struct.new(:value, :
|
|
36
|
+
HistoryValue = Struct.new(:value, :passed)
|
|
35
37
|
|
|
36
38
|
# Return the logger the watch writes its history to
|
|
37
39
|
#
|
|
@@ -54,69 +56,70 @@ module Bluepill
|
|
|
54
56
|
# @api private
|
|
55
57
|
EMPTY_ARRAY = no_events.freeze
|
|
56
58
|
|
|
57
|
-
# The options the watch takes for itself, which the check doesn't get
|
|
58
|
-
#
|
|
59
|
-
# @api private
|
|
60
|
-
WATCH_KEYS = %i[logger fires every times include_children].freeze
|
|
61
|
-
|
|
62
59
|
# Create a watch of a check
|
|
63
60
|
#
|
|
64
61
|
# @param [Symbol] name the check's name, such as :mem_usage
|
|
65
|
-
# @param [
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
62
|
+
# @param [Logger, nil] logger the logger to write to
|
|
63
|
+
# @param [Symbol, Array<Symbol>, nil] fires the events to fire when the
|
|
64
|
+
# check fails
|
|
65
|
+
# @param [Integer, nil] every the seconds between runs
|
|
66
|
+
# @param [Integer, Array<Integer>] times how many of the last values must
|
|
67
|
+
# fail, as a count or as [count, of]
|
|
68
|
+
# @param [Boolean] include_children whether to count the process's
|
|
69
|
+
# children too
|
|
70
|
+
# @param [Hash{Symbol => Object}] condition_options the check's own
|
|
71
|
+
# options, such as below: 100.megabytes
|
|
70
72
|
#
|
|
71
73
|
# @return [void]
|
|
72
74
|
#
|
|
75
|
+
# @raise [ConfigError] for an event a check can't fire
|
|
76
|
+
#
|
|
73
77
|
# @api private
|
|
74
|
-
def initialize(name,
|
|
78
|
+
def initialize(name, logger: nil, fires: :restart, every: nil, times: 1, include_children: false, **condition_options)
|
|
75
79
|
@name = name
|
|
76
|
-
@logger =
|
|
77
|
-
@fires = Array(
|
|
78
|
-
@every =
|
|
80
|
+
@logger = logger
|
|
81
|
+
@fires = events_to_fire(Array(fires))
|
|
82
|
+
@every = every
|
|
79
83
|
# times: 5 means 5 of the last 5, and times: [3, 5] 3 of the last 5
|
|
80
|
-
times = Array(
|
|
84
|
+
times = Array(times) #: Array[Integer]
|
|
81
85
|
@required = times.first #: Integer
|
|
82
86
|
@window = times.last #: Integer
|
|
83
|
-
@include_children =
|
|
87
|
+
@include_children = include_children
|
|
84
88
|
clear_history!
|
|
85
|
-
create_condition(
|
|
89
|
+
create_condition(condition_options)
|
|
86
90
|
end
|
|
87
91
|
|
|
88
|
-
#
|
|
92
|
+
# Return the events a check fires, if each is one a process takes
|
|
89
93
|
#
|
|
90
|
-
#
|
|
91
|
-
# initialize can log, and again after, in case that initialize doesn't
|
|
92
|
-
# call super.
|
|
94
|
+
# @param [Array<Object>] events what fires: names
|
|
93
95
|
#
|
|
94
|
-
# @
|
|
95
|
-
# created with, which stay as they are
|
|
96
|
+
# @return [Array<Symbol>] the events
|
|
96
97
|
#
|
|
97
|
-
# @
|
|
98
|
+
# @raise [ConfigError] for an event that isn't one of
|
|
99
|
+
# Process::StateMachine::TRANSITIONS
|
|
98
100
|
#
|
|
99
101
|
# @api private
|
|
100
|
-
def
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
def events_to_fire(events)
|
|
103
|
+
known = Process::StateMachine::TRANSITIONS.keys
|
|
104
|
+
unknown = events - known
|
|
105
|
+
return events if unknown.empty?
|
|
106
|
+
|
|
107
|
+
raise ConfigError, "#{@name} can't fire #{unknown.collect(&:inspect).join(", ")}: " \
|
|
108
|
+
"a check fires #{known.collect(&:inspect).join(", ")}"
|
|
105
109
|
end
|
|
106
110
|
|
|
107
|
-
#
|
|
111
|
+
# Create the check, with the options the watch doesn't take for itself
|
|
108
112
|
#
|
|
109
|
-
# The
|
|
110
|
-
#
|
|
113
|
+
# The condition gets the watch's logger as its logger: option, so its
|
|
114
|
+
# own initialize can log once it has called super.
|
|
111
115
|
#
|
|
112
|
-
# @param [
|
|
116
|
+
# @param [Hash{Symbol => Object}] options the check's own options
|
|
113
117
|
#
|
|
114
118
|
# @return [void]
|
|
115
119
|
#
|
|
116
120
|
# @api private
|
|
117
|
-
def
|
|
118
|
-
@
|
|
119
|
-
@process_condition.logger = logger
|
|
121
|
+
def create_condition(options)
|
|
122
|
+
@process_condition = CheckRegistry.fetch(@name).new(**options, logger: @logger)
|
|
120
123
|
end
|
|
121
124
|
|
|
122
125
|
# Return whether the check is due
|
|
@@ -144,9 +147,6 @@ module Bluepill
|
|
|
144
147
|
# @return [Array<Symbol>] the events to fire, empty unless the check
|
|
145
148
|
# ran and failed often enough
|
|
146
149
|
#
|
|
147
|
-
# @raise [StandardError] whatever the check raised, other than a
|
|
148
|
-
# System::PsError, after logging it
|
|
149
|
-
#
|
|
150
150
|
# @api private
|
|
151
151
|
def run(pid, tick_number = Clock.now)
|
|
152
152
|
# The same test as due?, written out to save a call on every check.
|
|
@@ -162,49 +162,45 @@ module Bluepill
|
|
|
162
162
|
|
|
163
163
|
# Run the check, add its value to the history, and log the history
|
|
164
164
|
#
|
|
165
|
+
# A check that raises is skipped: it adds nothing to the history and
|
|
166
|
+
# fires nothing, and runs again at its next interval.
|
|
167
|
+
#
|
|
165
168
|
# @param [Integer, nil] pid the process's pid
|
|
166
169
|
#
|
|
167
170
|
# @return [Array<Symbol>] the events to fire, empty unless enough of
|
|
168
|
-
# the last values failed for the watch to fire
|
|
169
|
-
#
|
|
170
|
-
# @raise [StandardError] whatever the check raised, other than a
|
|
171
|
-
# System::PsError, after logging it
|
|
171
|
+
# the last values failed for the watch to fire
|
|
172
172
|
#
|
|
173
173
|
# @api private
|
|
174
174
|
def run_check(pid)
|
|
175
|
-
|
|
176
|
-
value = @process_condition.run(pid, @include_children)
|
|
177
|
-
rescue => e
|
|
178
|
-
return check_failed(e)
|
|
179
|
-
end
|
|
175
|
+
value = @process_condition.run(pid, @include_children)
|
|
180
176
|
@history << HistoryValue.new(@process_condition.format_value(value), @process_condition.check?(value))
|
|
181
177
|
logger.info(to_s)
|
|
182
178
|
# Fire when enough of the values in the window failed
|
|
183
|
-
return @fires if @history.count { |v| !v.
|
|
179
|
+
return @fires if @history.count { |v| !v.passed } >= @required
|
|
184
180
|
|
|
185
181
|
EMPTY_ARRAY
|
|
182
|
+
rescue => e
|
|
183
|
+
check_failed(e)
|
|
186
184
|
end
|
|
187
185
|
|
|
188
|
-
# Log an error the check raised
|
|
186
|
+
# Log an error the check raised
|
|
189
187
|
#
|
|
190
|
-
#
|
|
188
|
+
# A failed ps is a warning, since it says nothing about the check. Any
|
|
189
|
+
# other error is logged with its backtrace. Either way, the check reads
|
|
190
|
+
# nothing this time.
|
|
191
191
|
#
|
|
192
192
|
# @param [StandardError] error what the check raised
|
|
193
193
|
#
|
|
194
|
-
# @return [Array<Symbol>] no events
|
|
195
|
-
#
|
|
196
|
-
# @raise [StandardError] the error, unless it's a System::PsError
|
|
194
|
+
# @return [Array<Symbol>] no events
|
|
197
195
|
#
|
|
198
196
|
# @api private
|
|
199
197
|
def check_failed(error)
|
|
200
198
|
if error.is_a?(System::PsError)
|
|
201
|
-
# No data this time, so leave the history alone and check again next interval
|
|
202
199
|
logger.warning("#{@name}: #{error}")
|
|
203
|
-
EMPTY_ARRAY
|
|
204
200
|
else
|
|
205
|
-
|
|
206
|
-
raise
|
|
201
|
+
Util.log_error(logger, "#{@name} check failed", error)
|
|
207
202
|
end
|
|
203
|
+
EMPTY_ARRAY
|
|
208
204
|
end
|
|
209
205
|
|
|
210
206
|
# Forget the values the check read, as when the process changes state
|
|
@@ -212,9 +208,7 @@ module Bluepill
|
|
|
212
208
|
# @return [Util::RotationalArray<HistoryValue>] the new, empty history
|
|
213
209
|
#
|
|
214
210
|
# @api private
|
|
215
|
-
def clear_history!
|
|
216
|
-
@history = Util::RotationalArray.new(@window)
|
|
217
|
-
end
|
|
211
|
+
def clear_history! = @history = Util::RotationalArray.new(@window)
|
|
218
212
|
|
|
219
213
|
# Return the check's name and history, as the log shows it
|
|
220
214
|
#
|
|
@@ -223,7 +217,7 @@ module Bluepill
|
|
|
223
217
|
#
|
|
224
218
|
# @api private
|
|
225
219
|
def to_s
|
|
226
|
-
data = @history.collect { |v| "#{v.value}#{"*" unless v.
|
|
220
|
+
data = @history.collect { |v| "#{v.value}#{"*" unless v.passed}" }.join(", ")
|
|
227
221
|
"#{@name}: [#{data}]\n"
|
|
228
222
|
end
|
|
229
223
|
end
|