bluepill 0.1.4 → 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 +4 -4
- data/CHANGELOG.md +16 -1
- data/README.md +2 -0
- data/lib/bluepill/application.rb +17 -10
- data/lib/bluepill/logger.rb +3 -1
- data/lib/bluepill/process.rb +4 -1
- data/lib/bluepill/process_commands.rb +3 -1
- data/lib/bluepill/process_journal.rb +151 -43
- data/lib/bluepill/system.rb +31 -0
- data/lib/bluepill/version.rb +7 -7
- data/sig/bluepill.rbs +21 -6
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ea20c4756cdf70d6b42f9c86361254c13bf933038bd3a5baa9e6218cfea17296
|
|
4
|
+
data.tar.gz: 1a7999e78a5e25ede02d2ea71d1f165a9b5d44afc658a9815477e2cb35f5b048
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 81675c58e49c91da17dbc60e052158ef1072fa6f711c3da32fe73040ad6732d7949c00e44a0055c3dacb904fdef18fb792ab04a7a8fb8c9896a1f15cc6d11875
|
|
7
|
+
data.tar.gz: d55cdef40d00d3a4bb085b8ee67a93a6fe00bbe8d6b32a512a196952b5ded55a3e80e1fa95c143a632203bcf71cdf4c055e6ba88363ddf2ba434d10d37cf3226
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.1.5] - 2026-11-01
|
|
11
|
+
|
|
12
|
+
### Removed
|
|
13
|
+
|
|
14
|
+
- `ProcessJournal.kill_all_from_all_journals`, which was meant to kill every journaled process when bluepill started or quit, but looked in the wrong directory and never found any ([#59](https://github.com/bluepill-rb/bluepill/issues/59)). Bluepill still kills what a process's journal lists before it starts that process, and `bluepill load` keeps monitoring the processes that are already running instead of restarting them.
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- A restart that stops and starts the process, because a check asked for one or from `bluepill restart`, left bluepill watching the old pid. So the process went from `restarting` to `down` and back to `up`, and bluepill logged an error about signaling the old pid. `bluepill restart` now also goes through `restarting`, like a check's restart, instead of leaving the process `up` with its old pid while it restarts.
|
|
19
|
+
- `SIGHUP` never reopened bluepill's log, because daemonizing ignored it after bluepill trapped it. So after log rotation, bluepill kept writing to the rotated file. Bluepill now traps `SIGHUP` again once it has daemonized. Reopening syslog on `SIGHUP` also no longer crashes bluepill, which 0.1.4 did in foreground mode, since it loads syslog only when it logs there.
|
|
20
|
+
- Stale journal locks, which a daemon that died leaves behind, were never cleared, since bluepill looked for them in the current directory instead of the journal directory, and removed them as if they were files ([#59](https://github.com/bluepill-rb/bluepill/issues/59)). A stale lock made the next write to its journal wait 10 seconds and fail. Bluepill now clears the app's own locks when it starts and quits.
|
|
21
|
+
- After a reboot, or once pids wrapped around, bluepill could kill an unrelated process whose pid it had journaled ([#3](https://github.com/bluepill-rb/bluepill/issues/3)). The journal now records when each process started, and bluepill only kills a pid whose process started then. A process group whose leader has exited, and a journal line from bluepill 0.1 and earlier, are only trusted if they were journaled since the machine booted.
|
|
22
|
+
- Two apps with a process of the same name shared its journal, so starting the process in one app killed the other app's ([#34](https://github.com/bluepill-rb/bluepill/issues/34)). Each app now keeps its journals in a directory of its own, `journals/<app>`. Journals that bluepill 0.1 and earlier wrote are still read and cleared, so upgrading doesn't leave their processes running.
|
|
23
|
+
|
|
10
24
|
## [0.1.4] - 2026-11-01
|
|
11
25
|
|
|
12
26
|
### Added
|
|
@@ -744,7 +758,8 @@ Initial release.
|
|
|
744
758
|
- Added the `bluepill` CLI with the `load`, `status`, `start`, `stop`, `restart`, `unmonitor`, `quit` and `log` commands.
|
|
745
759
|
- Added syslog logging.
|
|
746
760
|
|
|
747
|
-
[unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.1.
|
|
761
|
+
[unreleased]: https://github.com/bluepill-rb/bluepill/compare/v0.1.5...HEAD
|
|
762
|
+
[0.1.5]: https://github.com/bluepill-rb/bluepill/compare/v0.1.4...v0.1.5
|
|
748
763
|
[0.1.4]: https://github.com/bluepill-rb/bluepill/compare/v0.1.3...v0.1.4
|
|
749
764
|
[0.1.3]: https://github.com/bluepill-rb/bluepill/compare/v0.1.2...v0.1.3
|
|
750
765
|
[0.1.2]: https://github.com/bluepill-rb/bluepill/compare/v0.1.1...v0.1.2
|
data/README.md
CHANGED
|
@@ -347,6 +347,8 @@ To quit the bluepill daemon for an application:
|
|
|
347
347
|
|
|
348
348
|
sudo bluepill quit
|
|
349
349
|
|
|
350
|
+
Its processes keep running. The next `bluepill load` monitors them again, from their pid files, without restarting them, so you can upgrade bluepill this way.
|
|
351
|
+
|
|
350
352
|
#### bpsv
|
|
351
353
|
|
|
352
354
|
`bpsv` runs a bluepill command on the process it's named after, so a
|
data/lib/bluepill/application.rb
CHANGED
|
@@ -104,9 +104,10 @@ module Bluepill
|
|
|
104
104
|
self.log_file = options[:log_file]
|
|
105
105
|
# Only used for a non-root user, who has a home
|
|
106
106
|
home = ENV['HOME'] #: String
|
|
107
|
-
self.base_dir =
|
|
108
|
-
|
|
109
|
-
|
|
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)
|
|
110
111
|
self.pid_file = File.join(base_dir, 'pids', "#{name}.pid")
|
|
111
112
|
self.pids_dir = File.join(base_dir, 'pids', name)
|
|
112
113
|
self.kill_timeout = options[:kill_timeout] || 10
|
|
@@ -320,16 +321,17 @@ module Bluepill
|
|
|
320
321
|
|
|
321
322
|
# Start the daemon
|
|
322
323
|
#
|
|
323
|
-
# It stops a previous daemon for the application,
|
|
324
|
-
#
|
|
325
|
-
#
|
|
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.
|
|
326
329
|
#
|
|
327
330
|
# @return [void]
|
|
328
331
|
#
|
|
329
332
|
# @api private
|
|
330
333
|
def start_server
|
|
331
334
|
kill_previous_bluepill
|
|
332
|
-
ProcessJournal.kill_all_from_all_journals
|
|
333
335
|
ProcessJournal.clear_all_atomic_fs_locks
|
|
334
336
|
|
|
335
337
|
begin
|
|
@@ -337,7 +339,11 @@ module Bluepill
|
|
|
337
339
|
rescue Errno::EPERM
|
|
338
340
|
end
|
|
339
341
|
|
|
340
|
-
|
|
342
|
+
unless foreground?
|
|
343
|
+
Daemonize.daemonize
|
|
344
|
+
# Daemonizing ignores HUP, so trap it again to reopen the log on HUP
|
|
345
|
+
setup_signal_traps
|
|
346
|
+
end
|
|
341
347
|
|
|
342
348
|
logger.reopen
|
|
343
349
|
|
|
@@ -368,13 +374,14 @@ module Bluepill
|
|
|
368
374
|
end
|
|
369
375
|
end
|
|
370
376
|
|
|
371
|
-
#
|
|
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.
|
|
372
380
|
#
|
|
373
381
|
# @return [void]
|
|
374
382
|
#
|
|
375
383
|
# @api private
|
|
376
384
|
def cleanup
|
|
377
|
-
ProcessJournal.kill_all_from_all_journals
|
|
378
385
|
ProcessJournal.clear_all_atomic_fs_locks
|
|
379
386
|
begin
|
|
380
387
|
System.delete_if_exists(socket.path) if socket
|
data/lib/bluepill/logger.rb
CHANGED
|
@@ -150,7 +150,9 @@ module Bluepill
|
|
|
150
150
|
#
|
|
151
151
|
# @api private
|
|
152
152
|
def require_syslog
|
|
153
|
-
require
|
|
153
|
+
# A HUP reopens the log from a signal trap, where require raises
|
|
154
|
+
# ThreadError, and syslog is loaded by then
|
|
155
|
+
require 'syslog' unless defined?(::Syslog)
|
|
154
156
|
rescue LoadError
|
|
155
157
|
message = 'Bluepill logs to syslog unless log_file is set, but the syslog library ' \
|
|
156
158
|
"could not be loaded. Since Ruby 3.4 it's a separate gem, so add gem 'syslog' " \
|
data/lib/bluepill/process.rb
CHANGED
|
@@ -660,7 +660,10 @@ module Bluepill
|
|
|
660
660
|
stop_process
|
|
661
661
|
dispatch!(:unmonitor, 'user initiated')
|
|
662
662
|
when 'restart'
|
|
663
|
-
|
|
663
|
+
# Through the state machine, like a check's restart, so the process
|
|
664
|
+
# shows as restarting until it's up again. A state restart can't
|
|
665
|
+
# leave, such as unmonitored, restarts it directly.
|
|
666
|
+
restart_process unless dispatch!(:restart, 'user initiated')
|
|
664
667
|
when 'unmonitor'
|
|
665
668
|
# When the user issues an unmonitor cmd, reset any triggers so that
|
|
666
669
|
# scheduled events gets cleared
|
|
@@ -120,7 +120,8 @@ module Bluepill
|
|
|
120
120
|
#
|
|
121
121
|
# Bluepill only writes the pid file for processes it daemonizes, but it
|
|
122
122
|
# removes it for every process, so a stopped process never leaves a stale
|
|
123
|
-
# pid behind for a later process to reuse.
|
|
123
|
+
# pid behind for a later process to reuse. It forgets the pid it cached
|
|
124
|
+
# too, so a restart that stops and starts the process reads the new one.
|
|
124
125
|
#
|
|
125
126
|
# @return [void]
|
|
126
127
|
#
|
|
@@ -128,6 +129,7 @@ module Bluepill
|
|
|
128
129
|
def cleanup_process
|
|
129
130
|
ProcessJournal.kill_all_from_journal(name) # finish cleanup
|
|
130
131
|
unlink_pid
|
|
132
|
+
clear_pid
|
|
131
133
|
end
|
|
132
134
|
|
|
133
135
|
# Return a command with <tt>{{PID}}</tt> replaced by the process's pid
|
|
@@ -4,8 +4,22 @@ module Bluepill
|
|
|
4
4
|
# Remember the pids and process groups of the processes bluepill starts
|
|
5
5
|
#
|
|
6
6
|
# Each process has a pid journal and a process group journal, files in the
|
|
7
|
-
# journal directory with one id a line.
|
|
8
|
-
# kills whatever an earlier run left in
|
|
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.
|
|
9
23
|
#
|
|
10
24
|
# @api private
|
|
11
25
|
module ProcessJournal
|
|
@@ -17,13 +31,20 @@ module Bluepill
|
|
|
17
31
|
# @api private
|
|
18
32
|
attr_reader :logger
|
|
19
33
|
|
|
20
|
-
# Return the directory the journals are in
|
|
34
|
+
# Return the directory the app's journals are in
|
|
21
35
|
#
|
|
22
|
-
# @return [String] the
|
|
36
|
+
# @return [String] the app's directory in the journals directory
|
|
23
37
|
#
|
|
24
38
|
# @api private
|
|
25
39
|
attr_reader :journal_base_dir
|
|
26
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
|
+
|
|
27
48
|
# Set the logger the journal writes to, unless it already has one
|
|
28
49
|
#
|
|
29
50
|
# Only the first logger is kept.
|
|
@@ -37,20 +58,25 @@ module Bluepill
|
|
|
37
58
|
@logger ||= new_logger
|
|
38
59
|
end
|
|
39
60
|
|
|
40
|
-
# Set the journal directory from bluepill's base dir, and create it
|
|
61
|
+
# Set the app's journal directory from bluepill's base dir, and create it
|
|
41
62
|
#
|
|
42
|
-
# Only the first base dir
|
|
43
|
-
# so processes that run as other users can lock their
|
|
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.
|
|
44
66
|
#
|
|
45
67
|
# @param [String] base_dir bluepill's base directory
|
|
68
|
+
# @param [#to_s] app_name the app's name
|
|
46
69
|
#
|
|
47
70
|
# @return [void]
|
|
48
71
|
#
|
|
49
72
|
# @api private
|
|
50
|
-
def
|
|
51
|
-
@
|
|
52
|
-
|
|
53
|
-
|
|
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
|
|
54
80
|
end
|
|
55
81
|
end
|
|
56
82
|
|
|
@@ -117,37 +143,72 @@ module Bluepill
|
|
|
117
143
|
end
|
|
118
144
|
end
|
|
119
145
|
|
|
120
|
-
# Remove the journal locks
|
|
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.
|
|
121
150
|
#
|
|
122
151
|
# @return [void]
|
|
123
152
|
#
|
|
124
153
|
# @api private
|
|
125
154
|
def self.clear_all_atomic_fs_locks
|
|
126
|
-
Dir['.*.lock'].each do |
|
|
127
|
-
|
|
155
|
+
Dir[File.join(journal_base_dir, '.*.lock')].each do |lock|
|
|
156
|
+
clear_atomic_fs_lock(lock)
|
|
128
157
|
end
|
|
129
158
|
end
|
|
130
159
|
|
|
131
160
|
# Return the path of a process's pid journal
|
|
132
161
|
#
|
|
133
162
|
# @param [String] journal_name the process's name
|
|
163
|
+
# @param [String] dir the journal directory
|
|
134
164
|
#
|
|
135
165
|
# @return [String] the journal's path
|
|
136
166
|
#
|
|
137
167
|
# @api private
|
|
138
|
-
def self.pid_journal_filename(journal_name)
|
|
139
|
-
File.join(
|
|
168
|
+
def self.pid_journal_filename(journal_name, dir = journal_base_dir)
|
|
169
|
+
File.join(dir, ".bluepill_pids_journal.#{journal_name}")
|
|
140
170
|
end
|
|
141
171
|
|
|
142
172
|
# Return the path of a process's process group journal
|
|
143
173
|
#
|
|
144
174
|
# @param [String] journal_name the process's name
|
|
175
|
+
# @param [String] dir the journal directory
|
|
145
176
|
#
|
|
146
177
|
# @return [String] the journal's path
|
|
147
178
|
#
|
|
148
179
|
# @api private
|
|
149
|
-
def self.pgid_journal_filename(journal_name)
|
|
150
|
-
File.join(
|
|
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
|
|
200
|
+
end
|
|
201
|
+
|
|
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(' ')
|
|
151
212
|
end
|
|
152
213
|
|
|
153
214
|
# Return the valid pids in a pid journal
|
|
@@ -159,7 +220,7 @@ module Bluepill
|
|
|
159
220
|
# @api private
|
|
160
221
|
def self.pid_journal(filename)
|
|
161
222
|
logger.debug("pid journal file: #{filename}")
|
|
162
|
-
result =
|
|
223
|
+
result = journal_entries(filename).collect(&:first).reject { |pid| skip_pid?(pid) }
|
|
163
224
|
logger.debug("pid journal = #{result.join(' ')}")
|
|
164
225
|
result
|
|
165
226
|
rescue Errno::ENOENT
|
|
@@ -176,7 +237,7 @@ module Bluepill
|
|
|
176
237
|
# @api private
|
|
177
238
|
def self.pgid_journal(filename)
|
|
178
239
|
logger.debug("pgid journal file: #{filename}")
|
|
179
|
-
result =
|
|
240
|
+
result = journal_entries(filename).collect(&:first).reject { |pgid| skip_pgid?(pgid) }
|
|
180
241
|
logger.debug("pgid journal = #{result.join(' ')}")
|
|
181
242
|
result
|
|
182
243
|
rescue Errno::ENOENT
|
|
@@ -197,19 +258,6 @@ module Bluepill
|
|
|
197
258
|
logger.debug("Cleared lock #{name}")
|
|
198
259
|
end
|
|
199
260
|
|
|
200
|
-
# Kill what every journal in the current directory lists
|
|
201
|
-
#
|
|
202
|
-
# @return [void]
|
|
203
|
-
#
|
|
204
|
-
# @api private
|
|
205
|
-
def self.kill_all_from_all_journals
|
|
206
|
-
pids = Dir['.bluepill_pids_journal.*'].collect { |p| p.sub(/\A\.bluepill_pids_journal\./, '') }
|
|
207
|
-
pids.reject! { |p| p.end_with?('.lock') }
|
|
208
|
-
pids.each do |journal_name|
|
|
209
|
-
kill_all_from_journal(journal_name)
|
|
210
|
-
end
|
|
211
|
-
end
|
|
212
|
-
|
|
213
261
|
# Kill the processes and process groups that a process's journals list
|
|
214
262
|
#
|
|
215
263
|
# @param [String] journal_name the process's name
|
|
@@ -222,7 +270,10 @@ module Bluepill
|
|
|
222
270
|
kill_all_pgids_from_journal(journal_name)
|
|
223
271
|
end
|
|
224
272
|
|
|
225
|
-
# Kill the process groups that a process's
|
|
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.
|
|
226
277
|
#
|
|
227
278
|
# @param [String] journal_name the process's name
|
|
228
279
|
#
|
|
@@ -230,11 +281,16 @@ module Bluepill
|
|
|
230
281
|
#
|
|
231
282
|
# @api private
|
|
232
283
|
def self.kill_all_pgids_from_journal(journal_name)
|
|
233
|
-
|
|
234
|
-
|
|
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)
|
|
287
|
+
end
|
|
235
288
|
end
|
|
236
289
|
|
|
237
|
-
# Kill the processes that a process's
|
|
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.
|
|
238
294
|
#
|
|
239
295
|
# @param [String] journal_name the process's name
|
|
240
296
|
#
|
|
@@ -242,14 +298,27 @@ module Bluepill
|
|
|
242
298
|
#
|
|
243
299
|
# @api private
|
|
244
300
|
def self.kill_all_pids_from_journal(journal_name)
|
|
245
|
-
|
|
246
|
-
|
|
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
|
|
305
|
+
end
|
|
306
|
+
|
|
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]
|
|
247
314
|
end
|
|
248
315
|
|
|
249
316
|
# Kill the ids a journal lists, and then delete the journal
|
|
250
317
|
#
|
|
251
|
-
#
|
|
252
|
-
#
|
|
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.
|
|
253
322
|
#
|
|
254
323
|
# @param [String] filename the journal's path
|
|
255
324
|
# @param [Array<Integer>] ids the pids or process group ids in it
|
|
@@ -266,6 +335,7 @@ module Bluepill
|
|
|
266
335
|
end
|
|
267
336
|
|
|
268
337
|
acquire_atomic_fs_lock(filename) do
|
|
338
|
+
ids = journaled_ids(filename, ids, label)
|
|
269
339
|
signal_journaled('TERM', ids, label, sign)
|
|
270
340
|
survivors = ids.select { |id| System.pid_alive?(sign * id) }
|
|
271
341
|
unless survivors.empty?
|
|
@@ -278,6 +348,42 @@ module Bluepill
|
|
|
278
348
|
end
|
|
279
349
|
end
|
|
280
350
|
|
|
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
|
+
|
|
281
387
|
# Send a signal to each journaled id, and log what happened
|
|
282
388
|
#
|
|
283
389
|
# @param [String] signal TERM or KILL
|
|
@@ -313,6 +419,8 @@ module Bluepill
|
|
|
313
419
|
logger.debug("Skipping invalid pgid #{pgid} (class #{pgid.class})")
|
|
314
420
|
return
|
|
315
421
|
end
|
|
422
|
+
# skip_pgid? made sure it's an Integer
|
|
423
|
+
valid_pgid = pgid #: Integer
|
|
316
424
|
|
|
317
425
|
filename = pgid_journal_filename(journal_name)
|
|
318
426
|
acquire_atomic_fs_lock(filename) do
|
|
@@ -320,7 +428,7 @@ module Bluepill
|
|
|
320
428
|
logger.debug("Skipping duplicate pgid #{pgid} already in journal #{journal_name}")
|
|
321
429
|
else
|
|
322
430
|
logger.debug("Saving pgid #{pgid} to process journal #{journal_name}")
|
|
323
|
-
File.open(filename, 'a+', 0o600) { |f| f.puts(
|
|
431
|
+
File.open(filename, 'a+', 0o600) { |f| f.puts(journal_line(valid_pgid)) }
|
|
324
432
|
logger.info("Saved pgid #{pgid} to journal #{journal_name}")
|
|
325
433
|
logger.debug("Journal now = #{File.read(filename)}")
|
|
326
434
|
end
|
|
@@ -353,7 +461,7 @@ module Bluepill
|
|
|
353
461
|
logger.debug("Skipping duplicate pid #{pid} already in journal #{journal_name}")
|
|
354
462
|
else
|
|
355
463
|
logger.debug("Saving pid #{pid} to process journal #{journal_name}")
|
|
356
|
-
File.open(filename, 'a+', 0o600) { |f| f.puts(
|
|
464
|
+
File.open(filename, 'a+', 0o600) { |f| f.puts(journal_line(valid_pid)) }
|
|
357
465
|
logger.info("Saved pid #{pid} to journal #{journal_name}")
|
|
358
466
|
logger.debug("Journal now = #{File.read(filename)}")
|
|
359
467
|
end
|
data/lib/bluepill/system.rb
CHANGED
|
@@ -46,6 +46,37 @@ module Bluepill
|
|
|
46
46
|
false # no such process (ESRCH), or no pid
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
+
# Return when a process started, in seconds since the epoch
|
|
50
|
+
#
|
|
51
|
+
# It's read from ps's elapsed time for the process, so it's accurate to
|
|
52
|
+
# about a second, and works wherever ps does.
|
|
53
|
+
#
|
|
54
|
+
# @param [Integer] pid the process's pid
|
|
55
|
+
#
|
|
56
|
+
# @return [Integer, nil] when it started, or nil if there's no such
|
|
57
|
+
# process
|
|
58
|
+
#
|
|
59
|
+
# @api private
|
|
60
|
+
def self.started_at(pid)
|
|
61
|
+
# ps says why on stderr when there's no such process
|
|
62
|
+
elapsed = IO.popen(['ps', '-o', 'etime=', '-p', Integer(pid).to_s], err: File::NULL, &:read).rstrip
|
|
63
|
+
Integer(Time.now) - parse_elapsed_time(elapsed) unless elapsed.empty?
|
|
64
|
+
rescue SystemCallError
|
|
65
|
+
nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Return when the machine booted, in seconds since the epoch
|
|
69
|
+
#
|
|
70
|
+
# That's when its first process, pid 1, started. In a container, it's
|
|
71
|
+
# when the container started, which is when its pids start over.
|
|
72
|
+
#
|
|
73
|
+
# @return [Integer, nil] when it booted, or nil if ps can't tell
|
|
74
|
+
#
|
|
75
|
+
# @api private
|
|
76
|
+
def self.boot_time
|
|
77
|
+
started_at(1)
|
|
78
|
+
end
|
|
79
|
+
|
|
49
80
|
# Return a process's CPU usage, from this tick's ps table
|
|
50
81
|
#
|
|
51
82
|
# @param [Integer, nil] pid the process's pid
|
data/lib/bluepill/version.rb
CHANGED
|
@@ -2,7 +2,7 @@ module Bluepill
|
|
|
2
2
|
# The version of bluepill
|
|
3
3
|
#
|
|
4
4
|
# @example
|
|
5
|
-
# Bluepill::Version.to_s # => "0.1.
|
|
5
|
+
# Bluepill::Version.to_s # => "0.1.5"
|
|
6
6
|
#
|
|
7
7
|
# @api public
|
|
8
8
|
module Version
|
|
@@ -33,13 +33,13 @@ module Bluepill
|
|
|
33
33
|
# Return the patch version
|
|
34
34
|
#
|
|
35
35
|
# @example
|
|
36
|
-
# Bluepill::Version.patch # =>
|
|
36
|
+
# Bluepill::Version.patch # => 5
|
|
37
37
|
#
|
|
38
38
|
# @return [Integer] the patch version
|
|
39
39
|
#
|
|
40
40
|
# @api public
|
|
41
41
|
def self.patch
|
|
42
|
-
|
|
42
|
+
5
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
# Return the pre-release version
|
|
@@ -55,7 +55,7 @@ module Bluepill
|
|
|
55
55
|
# Return the version's parts by name
|
|
56
56
|
#
|
|
57
57
|
# @example
|
|
58
|
-
# Bluepill::Version.to_h # => {major: 0, minor: 1, patch:
|
|
58
|
+
# Bluepill::Version.to_h # => {major: 0, minor: 1, patch: 5, pre: nil}
|
|
59
59
|
#
|
|
60
60
|
# @return [Hash{Symbol => Integer, nil}] the major, minor, patch and
|
|
61
61
|
# pre-release versions
|
|
@@ -73,7 +73,7 @@ module Bluepill
|
|
|
73
73
|
# Return the version's parts, without a missing pre-release version
|
|
74
74
|
#
|
|
75
75
|
# @example
|
|
76
|
-
# Bluepill::Version.to_a # => [0, 1,
|
|
76
|
+
# Bluepill::Version.to_a # => [0, 1, 5]
|
|
77
77
|
#
|
|
78
78
|
# @return [Array<Integer>] the parts of the version
|
|
79
79
|
#
|
|
@@ -85,9 +85,9 @@ module Bluepill
|
|
|
85
85
|
# Return the version as a string
|
|
86
86
|
#
|
|
87
87
|
# @example
|
|
88
|
-
# Bluepill::Version.to_s # => "0.1.
|
|
88
|
+
# Bluepill::Version.to_s # => "0.1.5"
|
|
89
89
|
#
|
|
90
|
-
# @return [String] the version, such as "0.1.
|
|
90
|
+
# @return [String] the version, such as "0.1.5"
|
|
91
91
|
#
|
|
92
92
|
# @api public
|
|
93
93
|
def self.to_s
|
data/sig/bluepill.rbs
CHANGED
|
@@ -182,6 +182,7 @@ module Bluepill
|
|
|
182
182
|
def clean_threads: () -> void
|
|
183
183
|
def signal_process: (Integer | String | Symbol code) -> bool
|
|
184
184
|
def unlink_pid: () -> void
|
|
185
|
+
def clear_pid: () -> void
|
|
185
186
|
def skip_ticks_for: (seconds seconds) -> Integer
|
|
186
187
|
def dispatch!: (Symbol | String event, ?String? reason) -> bool
|
|
187
188
|
end
|
|
@@ -529,7 +530,7 @@ module Bluepill
|
|
|
529
530
|
|
|
530
531
|
def create_logger: () -> (Object & _Backend)
|
|
531
532
|
|
|
532
|
-
def require_syslog: () ->
|
|
533
|
+
def require_syslog: () -> void
|
|
533
534
|
end
|
|
534
535
|
|
|
535
536
|
# A value a ConditionWatch got, and whether it passed the check
|
|
@@ -731,6 +732,10 @@ module Bluepill
|
|
|
731
732
|
|
|
732
733
|
def self.pid_alive?: (Integer pid) -> bool
|
|
733
734
|
|
|
735
|
+
def self.started_at: (Integer pid) -> Integer?
|
|
736
|
+
|
|
737
|
+
def self.boot_time: () -> Integer?
|
|
738
|
+
|
|
734
739
|
def self.cpu_usage: (Integer? pid, boolish include_children) -> Float?
|
|
735
740
|
|
|
736
741
|
def self.memory_usage: (Integer? pid, boolish include_children) -> Float?
|
|
@@ -774,15 +779,18 @@ module Bluepill
|
|
|
774
779
|
module ProcessJournal
|
|
775
780
|
self.@logger: Logger
|
|
776
781
|
self.@journal_base_dir: String
|
|
782
|
+
self.@legacy_journal_dir: String
|
|
777
783
|
|
|
778
784
|
def self.logger: () -> Logger
|
|
779
785
|
|
|
780
786
|
def self.journal_base_dir: () -> String
|
|
781
787
|
|
|
788
|
+
def self.legacy_journal_dir: () -> String
|
|
789
|
+
|
|
782
790
|
# Only the first logger is kept
|
|
783
791
|
def self.logger=: (Logger new_logger) -> Logger
|
|
784
792
|
|
|
785
|
-
def self.
|
|
793
|
+
def self.setup: (String base_dir, _ToS app_name) -> void
|
|
786
794
|
|
|
787
795
|
def self.skip_pid?: (Integer? pid) -> bool
|
|
788
796
|
|
|
@@ -792,9 +800,14 @@ module Bluepill
|
|
|
792
800
|
|
|
793
801
|
def self.clear_all_atomic_fs_locks: () -> void
|
|
794
802
|
|
|
795
|
-
def self.pid_journal_filename: (String journal_name) -> String
|
|
803
|
+
def self.pid_journal_filename: (String journal_name, ?String dir) -> String
|
|
804
|
+
|
|
805
|
+
def self.pgid_journal_filename: (String journal_name, ?String dir) -> String
|
|
796
806
|
|
|
797
|
-
|
|
807
|
+
# Each id, with when its process started (nil before 0.2)
|
|
808
|
+
def self.journal_entries: (String filename) -> Array[[Integer, Integer?]]
|
|
809
|
+
|
|
810
|
+
def self.journal_line: (Integer id) -> String
|
|
798
811
|
|
|
799
812
|
def self.pid_journal: (String filename) -> Array[Integer]
|
|
800
813
|
|
|
@@ -802,16 +815,18 @@ module Bluepill
|
|
|
802
815
|
|
|
803
816
|
def self.clear_atomic_fs_lock: (String name) -> void
|
|
804
817
|
|
|
805
|
-
def self.kill_all_from_all_journals: () -> void
|
|
806
|
-
|
|
807
818
|
def self.kill_all_from_journal: (String journal_name) -> void
|
|
808
819
|
|
|
809
820
|
def self.kill_all_pgids_from_journal: (String journal_name) -> void
|
|
810
821
|
|
|
811
822
|
def self.kill_all_pids_from_journal: (String journal_name) -> void
|
|
812
823
|
|
|
824
|
+
def self.journal_dirs: () -> Array[String]
|
|
825
|
+
|
|
813
826
|
def self.kill_all_from_journal_file: (String filename, Array[Integer] ids, String label, Integer sign) -> void
|
|
814
827
|
|
|
828
|
+
def self.journaled_ids: (String filename, Array[Integer] ids, String label) -> Array[Integer]
|
|
829
|
+
|
|
815
830
|
def self.signal_journaled: (String signal, Array[Integer] ids, String label, Integer sign) -> void
|
|
816
831
|
|
|
817
832
|
def self.append_pgid_to_journal: (String journal_name, Integer? pgid) -> void
|