bluepill 0.1.3 → 0.1.5
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 +5 -5
- data/CHANGELOG.md +835 -0
- data/CONTRIBUTING.md +31 -0
- data/LICENSE +1 -1
- data/README.md +66 -29
- data/bin/bluepill +1 -1
- data/bluepill.gemspec +17 -6
- data/lib/bluepill/application.rb +321 -46
- data/lib/bluepill/condition_watch.rb +103 -8
- data/lib/bluepill/controller.rb +124 -13
- data/lib/bluepill/dsl/app_proxy.rb +102 -3
- data/lib/bluepill/dsl/process_factory.rb +183 -60
- data/lib/bluepill/dsl/process_proxy.rb +132 -14
- data/lib/bluepill/dsl.rb +27 -5
- data/lib/bluepill/group.rb +142 -9
- data/lib/bluepill/logger.rb +122 -5
- data/lib/bluepill/process.rb +575 -201
- data/lib/bluepill/process_commands.rb +264 -0
- data/lib/bluepill/process_conditions/always_true.rb +18 -4
- data/lib/bluepill/process_conditions/cpu_usage.rb +34 -0
- data/lib/bluepill/process_conditions/file_time.rb +40 -4
- data/lib/bluepill/process_conditions/http.rb +81 -23
- data/lib/bluepill/process_conditions/mem_usage.rb +61 -2
- data/lib/bluepill/process_conditions/process_condition.rb +43 -0
- data/lib/bluepill/process_conditions/running_time.rb +36 -1
- data/lib/bluepill/process_conditions/zombie_process.rb +26 -5
- data/lib/bluepill/process_conditions.rb +15 -1
- data/lib/bluepill/process_journal.rb +374 -114
- data/lib/bluepill/process_statistics.rb +33 -3
- data/lib/bluepill/socket.rb +63 -16
- data/lib/bluepill/system/commands.rb +286 -0
- data/lib/bluepill/system.rb +223 -228
- data/lib/bluepill/trigger.rb +110 -14
- data/lib/bluepill/triggers/flapping.rb +84 -3
- data/lib/bluepill/util/rotational_array.rb +24 -1
- data/lib/bluepill/version.rb +71 -19
- data/lib/bluepill.rb +25 -2
- data/sig/bluepill.rbs +941 -0
- data/sig/manifest.yaml +7 -0
- metadata +16 -34
- data/bin/sample_forking_server +0 -66
- data/lib/bluepill/application/client.rb +0 -6
- data/lib/bluepill/application/server.rb +0 -21
|
@@ -1,176 +1,426 @@
|
|
|
1
1
|
require 'bluepill/system'
|
|
2
2
|
|
|
3
3
|
module Bluepill
|
|
4
|
+
# Remember the pids and process groups of the processes bluepill starts
|
|
5
|
+
#
|
|
6
|
+
# Each process has a pid journal and a process group journal, files in the
|
|
7
|
+
# app's journal directory with one id a line. Before bluepill starts a
|
|
8
|
+
# process, it kills whatever an earlier run left in that process's
|
|
9
|
+
# journals.
|
|
10
|
+
#
|
|
11
|
+
# A pid can belong to another process by then: after a reboot, or once
|
|
12
|
+
# pids wrap around. So each line also has when the process started, and
|
|
13
|
+
# bluepill only kills a pid that still belongs to a process that started
|
|
14
|
+
# then. A process group whose leader has exited is only killed if it was
|
|
15
|
+
# journaled since the machine booted. Lines without a start time, from
|
|
16
|
+
# bluepill 0.1 and earlier, are only trusted if the journal was written
|
|
17
|
+
# since the machine booted.
|
|
18
|
+
#
|
|
19
|
+
# Each app has a journal directory of its own, so two apps with a process
|
|
20
|
+
# of the same name don't share a journal. Bluepill 0.1 and earlier kept
|
|
21
|
+
# every app's journals in one directory, and those are still read and
|
|
22
|
+
# cleared, so upgrading doesn't leave their processes behind.
|
|
23
|
+
#
|
|
24
|
+
# @api private
|
|
4
25
|
module ProcessJournal
|
|
5
|
-
module_function
|
|
6
|
-
|
|
7
26
|
class << self
|
|
27
|
+
# Return the logger the journal writes to
|
|
28
|
+
#
|
|
29
|
+
# @return [Logger] the first logger that was set
|
|
30
|
+
#
|
|
31
|
+
# @api private
|
|
8
32
|
attr_reader :logger
|
|
33
|
+
|
|
34
|
+
# Return the directory the app's journals are in
|
|
35
|
+
#
|
|
36
|
+
# @return [String] the app's directory in the journals directory
|
|
37
|
+
#
|
|
38
|
+
# @api private
|
|
9
39
|
attr_reader :journal_base_dir
|
|
10
40
|
|
|
41
|
+
# Return the directory every app's journals were in before 0.2
|
|
42
|
+
#
|
|
43
|
+
# @return [String] the journals directory under bluepill's base dir
|
|
44
|
+
#
|
|
45
|
+
# @api private
|
|
46
|
+
attr_reader :legacy_journal_dir
|
|
47
|
+
|
|
48
|
+
# Set the logger the journal writes to, unless it already has one
|
|
49
|
+
#
|
|
50
|
+
# Only the first logger is kept.
|
|
51
|
+
#
|
|
52
|
+
# @param [Logger] new_logger the logger to use
|
|
53
|
+
#
|
|
54
|
+
# @return [Logger] the logger
|
|
55
|
+
#
|
|
56
|
+
# @api private
|
|
11
57
|
def logger=(new_logger)
|
|
12
58
|
@logger ||= new_logger
|
|
13
59
|
end
|
|
14
60
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
61
|
+
# Set the app's journal directory from bluepill's base dir, and create it
|
|
62
|
+
#
|
|
63
|
+
# Only the first base dir and app are kept. The directories are made
|
|
64
|
+
# world-writable so processes that run as other users can lock their
|
|
65
|
+
# journals.
|
|
66
|
+
#
|
|
67
|
+
# @param [String] base_dir bluepill's base directory
|
|
68
|
+
# @param [#to_s] app_name the app's name
|
|
69
|
+
#
|
|
70
|
+
# @return [void]
|
|
71
|
+
#
|
|
72
|
+
# @api private
|
|
73
|
+
def setup(base_dir, app_name)
|
|
74
|
+
@legacy_journal_dir ||= File.join(base_dir, 'journals')
|
|
75
|
+
@journal_base_dir ||= File.join(@legacy_journal_dir, app_name.to_s)
|
|
76
|
+
[@legacy_journal_dir, @journal_base_dir].each do |dir|
|
|
77
|
+
FileUtils.mkdir_p(dir) unless File.exist?(dir)
|
|
78
|
+
FileUtils.chmod(0o777, dir)
|
|
79
|
+
end
|
|
19
80
|
end
|
|
20
81
|
end
|
|
21
82
|
|
|
22
|
-
|
|
83
|
+
# Return whether a pid can't be journaled or signaled
|
|
84
|
+
#
|
|
85
|
+
# A pid of 1 or less would reach init, or a whole process group.
|
|
86
|
+
#
|
|
87
|
+
# @param [Integer, nil] pid the pid to check
|
|
88
|
+
#
|
|
89
|
+
# @return [Boolean] true unless pid is an Integer above 1
|
|
90
|
+
#
|
|
91
|
+
# @api private
|
|
92
|
+
def self.skip_pid?(pid)
|
|
23
93
|
!pid.is_a?(Integer) || pid <= 1
|
|
24
94
|
end
|
|
25
95
|
|
|
26
|
-
|
|
96
|
+
# Return whether a process group id can't be journaled or signaled
|
|
97
|
+
#
|
|
98
|
+
# @param [Integer, nil] pgid the process group id to check
|
|
99
|
+
#
|
|
100
|
+
# @return [Boolean] true unless pgid is an Integer above 1
|
|
101
|
+
#
|
|
102
|
+
# @api private
|
|
103
|
+
def self.skip_pgid?(pgid)
|
|
27
104
|
!pgid.is_a?(Integer) || pgid <= 1
|
|
28
105
|
end
|
|
29
106
|
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
|
|
107
|
+
# Run a block while holding a lock on a journal
|
|
108
|
+
#
|
|
109
|
+
# The lock is a directory, since making one is atomic on POSIX
|
|
110
|
+
# filesystems and f.flock(File::LOCK_SH) is not available on all
|
|
111
|
+
# platforms. It waits up to 10 seconds for another process's lock.
|
|
112
|
+
#
|
|
113
|
+
# @param [String] name the journal's file name
|
|
114
|
+
#
|
|
115
|
+
# @yield while holding the lock
|
|
116
|
+
# @yieldreturn [Object] what the method returns
|
|
117
|
+
#
|
|
118
|
+
# @return [Object] what the block returns
|
|
119
|
+
#
|
|
120
|
+
# @raise [RuntimeError] if the lock is still held after 10 tries
|
|
121
|
+
#
|
|
122
|
+
# @api private
|
|
123
|
+
def self.acquire_atomic_fs_lock(name)
|
|
124
|
+
lock = "#{name}.lock"
|
|
33
125
|
times = 0
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
126
|
+
begin
|
|
127
|
+
Dir.mkdir lock, 0o700
|
|
128
|
+
rescue Errno::EEXIST
|
|
129
|
+
times += 1
|
|
130
|
+
if times < 10
|
|
131
|
+
logger.debug("Waiting for lock #{lock}")
|
|
132
|
+
sleep 1
|
|
133
|
+
retry
|
|
134
|
+
end
|
|
135
|
+
logger.info("Timeout waiting for lock #{lock}")
|
|
136
|
+
raise "Timeout waiting for lock #{lock}"
|
|
137
|
+
end
|
|
138
|
+
logger.debug("Acquired lock #{lock}")
|
|
139
|
+
begin
|
|
140
|
+
yield
|
|
141
|
+
ensure
|
|
142
|
+
clear_atomic_fs_lock(lock)
|
|
47
143
|
end
|
|
48
|
-
ensure
|
|
49
|
-
clear_atomic_fs_lock(name)
|
|
50
144
|
end
|
|
51
145
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
146
|
+
# Remove the app's journal locks, which a daemon that died left behind
|
|
147
|
+
#
|
|
148
|
+
# Without this, a stale lock would make the next write to its journal
|
|
149
|
+
# wait 10 seconds and fail.
|
|
150
|
+
#
|
|
151
|
+
# @return [void]
|
|
152
|
+
#
|
|
153
|
+
# @api private
|
|
154
|
+
def self.clear_all_atomic_fs_locks
|
|
155
|
+
Dir[File.join(journal_base_dir, '.*.lock')].each do |lock|
|
|
156
|
+
clear_atomic_fs_lock(lock)
|
|
55
157
|
end
|
|
56
158
|
end
|
|
57
159
|
|
|
58
|
-
|
|
59
|
-
|
|
160
|
+
# Return the path of a process's pid journal
|
|
161
|
+
#
|
|
162
|
+
# @param [String] journal_name the process's name
|
|
163
|
+
# @param [String] dir the journal directory
|
|
164
|
+
#
|
|
165
|
+
# @return [String] the journal's path
|
|
166
|
+
#
|
|
167
|
+
# @api private
|
|
168
|
+
def self.pid_journal_filename(journal_name, dir = journal_base_dir)
|
|
169
|
+
File.join(dir, ".bluepill_pids_journal.#{journal_name}")
|
|
60
170
|
end
|
|
61
171
|
|
|
62
|
-
|
|
63
|
-
|
|
172
|
+
# Return the path of a process's process group journal
|
|
173
|
+
#
|
|
174
|
+
# @param [String] journal_name the process's name
|
|
175
|
+
# @param [String] dir the journal directory
|
|
176
|
+
#
|
|
177
|
+
# @return [String] the journal's path
|
|
178
|
+
#
|
|
179
|
+
# @api private
|
|
180
|
+
def self.pgid_journal_filename(journal_name, dir = journal_base_dir)
|
|
181
|
+
File.join(dir, ".bluepill_pgids_journal.#{journal_name}")
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Return the ids in a journal, each with when its process started
|
|
185
|
+
#
|
|
186
|
+
# @param [String] filename the journal's path
|
|
187
|
+
#
|
|
188
|
+
# @return [Array<Array(Integer, Integer)>, Array<Array(Integer, nil)>]
|
|
189
|
+
# each id, with when its process started, or nil for a line from
|
|
190
|
+
# bluepill 0.1 and earlier, which has none
|
|
191
|
+
#
|
|
192
|
+
# @raise [Errno::ENOENT] if there's no journal
|
|
193
|
+
#
|
|
194
|
+
# @api private
|
|
195
|
+
def self.journal_entries(filename)
|
|
196
|
+
File.readlines(filename).collect do |line|
|
|
197
|
+
id, started = line.split
|
|
198
|
+
[id.to_i, started && started.to_i]
|
|
199
|
+
end
|
|
64
200
|
end
|
|
65
201
|
|
|
66
|
-
|
|
202
|
+
# Return a journal line for an id: the id, and when its process started
|
|
203
|
+
#
|
|
204
|
+
# @param [Integer] id a pid, or a process group id, whose leader's start
|
|
205
|
+
# is recorded
|
|
206
|
+
#
|
|
207
|
+
# @return [String] the line, without a start time if ps can't tell
|
|
208
|
+
#
|
|
209
|
+
# @api private
|
|
210
|
+
def self.journal_line(id)
|
|
211
|
+
[id, System.started_at(id)].compact.join(' ')
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Return the valid pids in a pid journal
|
|
215
|
+
#
|
|
216
|
+
# @param [String] filename the journal's path
|
|
217
|
+
#
|
|
218
|
+
# @return [Array<Integer>] the pids, or none if there's no journal
|
|
219
|
+
#
|
|
220
|
+
# @api private
|
|
221
|
+
def self.pid_journal(filename)
|
|
67
222
|
logger.debug("pid journal file: #{filename}")
|
|
68
|
-
result =
|
|
223
|
+
result = journal_entries(filename).collect(&:first).reject { |pid| skip_pid?(pid) }
|
|
69
224
|
logger.debug("pid journal = #{result.join(' ')}")
|
|
70
225
|
result
|
|
71
226
|
rescue Errno::ENOENT
|
|
72
227
|
[]
|
|
73
228
|
end
|
|
74
229
|
|
|
75
|
-
|
|
230
|
+
# Return the valid process group ids in a process group journal
|
|
231
|
+
#
|
|
232
|
+
# @param [String] filename the journal's path
|
|
233
|
+
#
|
|
234
|
+
# @return [Array<Integer>] the process group ids, or none if there's no
|
|
235
|
+
# journal
|
|
236
|
+
#
|
|
237
|
+
# @api private
|
|
238
|
+
def self.pgid_journal(filename)
|
|
76
239
|
logger.debug("pgid journal file: #{filename}")
|
|
77
|
-
result =
|
|
240
|
+
result = journal_entries(filename).collect(&:first).reject { |pgid| skip_pgid?(pgid) }
|
|
78
241
|
logger.debug("pgid journal = #{result.join(' ')}")
|
|
79
242
|
result
|
|
80
243
|
rescue Errno::ENOENT
|
|
81
244
|
[]
|
|
82
245
|
end
|
|
83
246
|
|
|
84
|
-
|
|
247
|
+
# Release a journal lock, if it's there
|
|
248
|
+
#
|
|
249
|
+
# @param [String] name the lock's directory
|
|
250
|
+
#
|
|
251
|
+
# @return [void]
|
|
252
|
+
#
|
|
253
|
+
# @api private
|
|
254
|
+
def self.clear_atomic_fs_lock(name)
|
|
85
255
|
return unless File.directory?(name)
|
|
256
|
+
|
|
86
257
|
Dir.rmdir(name)
|
|
87
258
|
logger.debug("Cleared lock #{name}")
|
|
88
259
|
end
|
|
89
260
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
261
|
+
# Kill the processes and process groups that a process's journals list
|
|
262
|
+
#
|
|
263
|
+
# @param [String] journal_name the process's name
|
|
264
|
+
#
|
|
265
|
+
# @return [void]
|
|
266
|
+
#
|
|
267
|
+
# @api private
|
|
268
|
+
def self.kill_all_from_journal(journal_name)
|
|
269
|
+
kill_all_pids_from_journal(journal_name)
|
|
270
|
+
kill_all_pgids_from_journal(journal_name)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Kill the process groups that a process's journals list
|
|
274
|
+
#
|
|
275
|
+
# That's the app's journal, and the one that bluepill 0.1 and earlier
|
|
276
|
+
# shared between apps.
|
|
277
|
+
#
|
|
278
|
+
# @param [String] journal_name the process's name
|
|
279
|
+
#
|
|
280
|
+
# @return [void]
|
|
281
|
+
#
|
|
282
|
+
# @api private
|
|
283
|
+
def self.kill_all_pgids_from_journal(journal_name)
|
|
284
|
+
journal_dirs.each do |dir|
|
|
285
|
+
filename = pgid_journal_filename(journal_name, dir)
|
|
286
|
+
kill_all_from_journal_file(filename, pgid_journal(filename), 'process group', -1)
|
|
95
287
|
end
|
|
96
288
|
end
|
|
97
289
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
290
|
+
# Kill the processes that a process's journals list
|
|
291
|
+
#
|
|
292
|
+
# That's the app's journal, and the one that bluepill 0.1 and earlier
|
|
293
|
+
# shared between apps.
|
|
294
|
+
#
|
|
295
|
+
# @param [String] journal_name the process's name
|
|
296
|
+
#
|
|
297
|
+
# @return [void]
|
|
298
|
+
#
|
|
299
|
+
# @api private
|
|
300
|
+
def self.kill_all_pids_from_journal(journal_name)
|
|
301
|
+
journal_dirs.each do |dir|
|
|
302
|
+
filename = pid_journal_filename(journal_name, dir)
|
|
303
|
+
kill_all_from_journal_file(filename, pid_journal(filename), 'process', 1)
|
|
304
|
+
end
|
|
101
305
|
end
|
|
102
306
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
logger.debug('Journal cleanup completed')
|
|
130
|
-
end
|
|
131
|
-
else
|
|
307
|
+
# Return the directories a process's journals can be in
|
|
308
|
+
#
|
|
309
|
+
# @return [Array<String>] the app's, and then the one before 0.2
|
|
310
|
+
#
|
|
311
|
+
# @api private
|
|
312
|
+
def self.journal_dirs
|
|
313
|
+
[journal_base_dir, legacy_journal_dir]
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# Kill the ids a journal lists, and then delete the journal
|
|
317
|
+
#
|
|
318
|
+
# Only kills the ids that still belong to what bluepill journaled (see
|
|
319
|
+
# journaled_ids). Sends TERM, waits a second for any survivors, then
|
|
320
|
+
# sends KILL to those still alive. A negative sign targets process groups
|
|
321
|
+
# instead of pids.
|
|
322
|
+
#
|
|
323
|
+
# @param [String] filename the journal's path
|
|
324
|
+
# @param [Array<Integer>] ids the pids or process group ids in it
|
|
325
|
+
# @param [String] label what the ids are, for the log
|
|
326
|
+
# @param [Integer] sign 1 for pids, or -1 for process groups
|
|
327
|
+
#
|
|
328
|
+
# @return [void]
|
|
329
|
+
#
|
|
330
|
+
# @api private
|
|
331
|
+
def self.kill_all_from_journal_file(filename, ids, label, sign)
|
|
332
|
+
if ids.empty?
|
|
132
333
|
logger.debug('No previous process journal - Skipping cleanup')
|
|
334
|
+
return
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
acquire_atomic_fs_lock(filename) do
|
|
338
|
+
ids = journaled_ids(filename, ids, label)
|
|
339
|
+
signal_journaled('TERM', ids, label, sign)
|
|
340
|
+
survivors = ids.select { |id| System.pid_alive?(sign * id) }
|
|
341
|
+
unless survivors.empty?
|
|
342
|
+
sleep(1)
|
|
343
|
+
survivors = survivors.select { |id| System.pid_alive?(sign * id) }
|
|
344
|
+
signal_journaled('KILL', survivors, label, sign)
|
|
345
|
+
end
|
|
346
|
+
System.delete_if_exists(filename) # reset journal
|
|
347
|
+
logger.debug('Journal cleanup completed')
|
|
133
348
|
end
|
|
134
349
|
end
|
|
135
350
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
351
|
+
# Return the ids that still belong to what bluepill journaled
|
|
352
|
+
#
|
|
353
|
+
# An id whose process is running is kept only if that process started
|
|
354
|
+
# when the journal says, give or take 2 seconds, since ps's elapsed time
|
|
355
|
+
# is only accurate to a second. One whose process has exited (a process
|
|
356
|
+
# group whose leader is gone) is kept only if it was journaled since the
|
|
357
|
+
# machine booted, and so is one without a start time.
|
|
358
|
+
#
|
|
359
|
+
# @param [String] filename the journal's path
|
|
360
|
+
# @param [Array<Integer>] ids the valid ids in it
|
|
361
|
+
# @param [String] label what the ids are, for the log
|
|
362
|
+
#
|
|
363
|
+
# @return [Array<Integer>] the ids that are safe to signal, or none if
|
|
364
|
+
# the journal was cleared since its ids were read
|
|
365
|
+
#
|
|
366
|
+
# @api private
|
|
367
|
+
def self.journaled_ids(filename, ids, label)
|
|
368
|
+
# Journals only grow until they're cleared, under the lock this runs in
|
|
369
|
+
return [] unless File.exist?(filename)
|
|
370
|
+
|
|
371
|
+
started = Hash[journal_entries(filename)]
|
|
372
|
+
boot = System.boot_time
|
|
373
|
+
written = Integer(File.mtime(filename))
|
|
374
|
+
ids.select do |id|
|
|
375
|
+
now = System.started_at(id)
|
|
376
|
+
journaled = started.fetch(id)
|
|
377
|
+
ours = if now && journaled
|
|
378
|
+
(now - journaled).abs <= 2
|
|
379
|
+
else
|
|
380
|
+
!boot.nil? && (journaled || written) >= boot - 2
|
|
381
|
+
end
|
|
382
|
+
logger.info("Skipping old #{label} #{id}, which isn't the one bluepill started") unless ours
|
|
383
|
+
ours
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
# Send a signal to each journaled id, and log what happened
|
|
388
|
+
#
|
|
389
|
+
# @param [String] signal TERM or KILL
|
|
390
|
+
# @param [Array<Integer>] ids the pids or process group ids
|
|
391
|
+
# @param [String] label what the ids are, for the log
|
|
392
|
+
# @param [Integer] sign 1 for pids, or -1 for process groups
|
|
393
|
+
#
|
|
394
|
+
# @return [void]
|
|
395
|
+
#
|
|
396
|
+
# @api private
|
|
397
|
+
def self.signal_journaled(signal, ids, label, sign)
|
|
398
|
+
past, verb = signal.eql?('TERM') ? %w[Termed term] : %w[Killed kill]
|
|
399
|
+
ids.each do |id|
|
|
400
|
+
begin
|
|
401
|
+
::Process.kill(signal, sign * id)
|
|
402
|
+
logger.info("#{past} old #{label} #{id}")
|
|
403
|
+
rescue Errno::ESRCH
|
|
404
|
+
logger.debug("Unable to #{verb} missing #{label} #{id}")
|
|
163
405
|
end
|
|
164
|
-
else
|
|
165
|
-
logger.debug('No previous process journal - Skipping cleanup')
|
|
166
406
|
end
|
|
167
407
|
end
|
|
168
408
|
|
|
169
|
-
|
|
409
|
+
# Add a process group id to a process's journal, unless it's there
|
|
410
|
+
#
|
|
411
|
+
# @param [String] journal_name the process's name
|
|
412
|
+
# @param [Integer, nil] pgid the process group id
|
|
413
|
+
#
|
|
414
|
+
# @return [void]
|
|
415
|
+
#
|
|
416
|
+
# @api private
|
|
417
|
+
def self.append_pgid_to_journal(journal_name, pgid)
|
|
170
418
|
if skip_pgid?(pgid)
|
|
171
419
|
logger.debug("Skipping invalid pgid #{pgid} (class #{pgid.class})")
|
|
172
420
|
return
|
|
173
421
|
end
|
|
422
|
+
# skip_pgid? made sure it's an Integer
|
|
423
|
+
valid_pgid = pgid #: Integer
|
|
174
424
|
|
|
175
425
|
filename = pgid_journal_filename(journal_name)
|
|
176
426
|
acquire_atomic_fs_lock(filename) do
|
|
@@ -178,22 +428,32 @@ module Bluepill
|
|
|
178
428
|
logger.debug("Skipping duplicate pgid #{pgid} already in journal #{journal_name}")
|
|
179
429
|
else
|
|
180
430
|
logger.debug("Saving pgid #{pgid} to process journal #{journal_name}")
|
|
181
|
-
File.open(filename, 'a+',
|
|
431
|
+
File.open(filename, 'a+', 0o600) { |f| f.puts(journal_line(valid_pgid)) }
|
|
182
432
|
logger.info("Saved pgid #{pgid} to journal #{journal_name}")
|
|
183
|
-
logger.debug("Journal now = #{File.
|
|
433
|
+
logger.debug("Journal now = #{File.read(filename)}")
|
|
184
434
|
end
|
|
185
435
|
end
|
|
186
436
|
end
|
|
187
437
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
438
|
+
# Add a pid, and its process group id, to a process's journals
|
|
439
|
+
#
|
|
440
|
+
# @param [String] journal_name the process's name
|
|
441
|
+
# @param [Integer, nil] pid the pid
|
|
442
|
+
#
|
|
443
|
+
# @return [void]
|
|
444
|
+
#
|
|
445
|
+
# @api private
|
|
446
|
+
def self.append_pid_to_journal(journal_name, pid)
|
|
193
447
|
if skip_pid?(pid)
|
|
194
448
|
logger.debug("Skipping invalid pid #{pid} (class #{pid.class})")
|
|
195
449
|
return
|
|
196
450
|
end
|
|
451
|
+
# skip_pid? made sure it's an Integer
|
|
452
|
+
valid_pid = pid #: Integer
|
|
453
|
+
begin
|
|
454
|
+
append_pgid_to_journal(journal_name, ::Process.getpgid(valid_pid))
|
|
455
|
+
rescue Errno::ESRCH
|
|
456
|
+
end
|
|
197
457
|
|
|
198
458
|
filename = pid_journal_filename(journal_name)
|
|
199
459
|
acquire_atomic_fs_lock(filename) do
|
|
@@ -201,9 +461,9 @@ module Bluepill
|
|
|
201
461
|
logger.debug("Skipping duplicate pid #{pid} already in journal #{journal_name}")
|
|
202
462
|
else
|
|
203
463
|
logger.debug("Saving pid #{pid} to process journal #{journal_name}")
|
|
204
|
-
File.open(filename, 'a+',
|
|
464
|
+
File.open(filename, 'a+', 0o600) { |f| f.puts(journal_line(valid_pid)) }
|
|
205
465
|
logger.info("Saved pid #{pid} to journal #{journal_name}")
|
|
206
|
-
logger.debug("Journal now = #{File.
|
|
466
|
+
logger.debug("Journal now = #{File.read(filename)}")
|
|
207
467
|
end
|
|
208
468
|
end
|
|
209
469
|
end
|
|
@@ -1,21 +1,51 @@
|
|
|
1
1
|
module Bluepill
|
|
2
|
+
# Keeps a process's last few events, which bluepill status shows
|
|
3
|
+
#
|
|
4
|
+
# @api private
|
|
2
5
|
class ProcessStatistics
|
|
3
|
-
|
|
6
|
+
# How status shows each event's time
|
|
7
|
+
STRFTIME = '%m/%d/%Y %H:%M:%S'.freeze
|
|
8
|
+
|
|
9
|
+
# How many events are kept
|
|
4
10
|
EVENTS_TO_PERSIST = 10
|
|
5
11
|
|
|
12
|
+
# Return the recorded events, newest last
|
|
13
|
+
#
|
|
14
|
+
# @return [Util::RotationalArray<Array(Symbol, String, nil, Time)>] each
|
|
15
|
+
# event, its reason and its time
|
|
16
|
+
#
|
|
17
|
+
# @api private
|
|
6
18
|
attr_reader :events
|
|
7
19
|
|
|
8
|
-
#
|
|
20
|
+
# Create an empty event history
|
|
21
|
+
#
|
|
22
|
+
# @return [void]
|
|
23
|
+
#
|
|
24
|
+
# @api private
|
|
9
25
|
def initialize
|
|
10
26
|
@events = Util::RotationalArray.new(EVENTS_TO_PERSIST)
|
|
11
27
|
end
|
|
12
28
|
|
|
29
|
+
# Add an event to the history, with the time it happened
|
|
30
|
+
#
|
|
31
|
+
# @param [Symbol, String] event the event, such as :restart
|
|
32
|
+
# @param [String, nil] reason why it happened
|
|
33
|
+
#
|
|
34
|
+
# @return [Util::RotationalArray<Array(Symbol, String, nil, Time)>] the
|
|
35
|
+
# history, newest last
|
|
36
|
+
#
|
|
37
|
+
# @api private
|
|
13
38
|
def record_event(event, reason)
|
|
14
39
|
events.push([event, reason, Time.now])
|
|
15
40
|
end
|
|
16
41
|
|
|
42
|
+
# Return the history as text, newest first, for bluepill status
|
|
43
|
+
#
|
|
44
|
+
# @return [String]
|
|
45
|
+
#
|
|
46
|
+
# @api private
|
|
17
47
|
def to_s
|
|
18
|
-
str = events.reverse.collect do |
|
|
48
|
+
str = events.reverse.collect do |event, reason, time|
|
|
19
49
|
" #{event} at #{time.strftime(STRFTIME)} - #{reason || 'unspecified'}"
|
|
20
50
|
end.join("\n")
|
|
21
51
|
|