Refactor journalctl monitor to only spawn new process on exit

Change-type: patch
Closes: #1591
Signed-off-by: Miguel Casqueira <miguel@balena.io>
This commit is contained in:
Miguel Casqueira 2021-02-11 17:43:12 -05:00
parent 64127b24d2
commit 898b72c7f7

View File

@ -73,13 +73,18 @@ class LogMonitor {
this.handleRow(row); this.handleRow(row);
} }
}, },
async (data: Buffer) => { (data: Buffer) => {
log.error( log.error('journalctl - balena.service stderr: ', data.toString());
'Non-empty stderr stream from journalctl log fetching: ', },
data.toString(), () => {
); // noop for closed
},
async () => {
log.debug('balena.service journalctl process exit.');
// On exit of process try to create another
await delay(JOURNALCTL_ERROR_RETRY_DELAY); await delay(JOURNALCTL_ERROR_RETRY_DELAY);
this.start(); log.debug('Spawning another process to watch balena.service logs.');
return this.start();
}, },
); );
} }
@ -113,6 +118,7 @@ class LogMonitor {
onRow: (row: JournalRow) => void, onRow: (row: JournalRow) => void,
onError: (data: Buffer) => void, onError: (data: Buffer) => void,
onClose?: () => void, onClose?: () => void,
onExit?: () => void,
): ReturnType<typeof spawnJournalctl> { ): ReturnType<typeof spawnJournalctl> {
const journalctl = spawnJournalctl(options); const journalctl = spawnJournalctl(options);
journalctl.stdout?.pipe(JSONstream.parse(true).on('data', onRow)); journalctl.stdout?.pipe(JSONstream.parse(true).on('data', onRow));
@ -120,6 +126,9 @@ class LogMonitor {
if (onClose) { if (onClose) {
journalctl.on('close', onClose); journalctl.on('close', onClose);
} }
if (onExit) {
journalctl.on('exit', onExit);
}
return journalctl; return journalctl;
} }
@ -137,7 +146,10 @@ class LogMonitor {
}, },
(row: JournalRow) => this.handleRow(row), (row: JournalRow) => this.handleRow(row),
async (data: Buffer) => { async (data: Buffer) => {
log.error('journalctl backfill error: ', data.toString()); log.error(
`journalctl - container ${containerId} stderr: `,
data.toString(),
);
}, },
() => { () => {
this.containers[containerId].follow = true; this.containers[containerId].follow = true;