chore: switch to execa for logger output

This commit is contained in:
Alexis
2021-09-13 14:16:53 +09:30
parent 00e57393fc
commit 4f889c08fe
3 changed files with 38 additions and 63 deletions

View File

@@ -1,18 +1,17 @@
import fs from 'fs';
import path from 'path';
import execa from 'execa';
import { spawn, exec } from 'child_process';
import { $ } from 'zx';
import { parse, ArgsParseOptions, ArgumentConfig } from 'ts-command-line-args';
import dotEnv from 'dotenv';
import findProcess from 'find-process';
import pidusage from 'pidusage';
import pidUsage from 'pidusage';
import prettyMs from 'pretty-ms';
import dedent from 'dedent-tabs';
import { addExitCallback } from 'catch-exit';
import { version } from '../package.json';
import { paths } from './core/paths';
import { logger } from './core/log';
import packageJson from '../package.json';
const setEnv = (envName: string, value: any) => {
if (!value || String(value).trim().length === 0) {
@@ -79,9 +78,7 @@ const getUnraidApiPid = async () => {
return pids.find(_ => _.pid !== process.pid)?.pid;
};
const logToSyslog = async function (text: string) {
return $`logger -t unraid-api[${process.pid}] ${text}`.then(() => true).catch(() => false);
};
const logToSyslog = (text: string) => execa.commandSync(`logger-t unraid-api[${process.pid}] ${text}`);
const commands = {
/**
@@ -104,8 +101,9 @@ const commands = {
setEnv('LOG_TRANSPORT', mainOptions['log-transport']);
setEnv('PORT', mainOptions.port);
await logToSyslog(`Starting unraid-api v${packageJson.version as string}`);
await logToSyslog(`Loading the "${getEnvironment()}" environment.`);
const apiVersion: string = version;
logToSyslog(`Starting unraid-api v${apiVersion}`);
logToSyslog(`Loading the "${getEnvironment()}" environment.`);
// If we're in debug mode or we're NOT
// in debug but ARE in the child process
@@ -113,19 +111,19 @@ const commands = {
// Log when the API exits
addExitCallback((signal, exitCode, error) => {
if (exitCode === 0 || signal === 'SIGTERM') {
logToSyslog('👋 Farewell. UNRAID API shutting down!').catch(() => undefined);
logToSyslog('👋 Farewell. UNRAID API shutting down!');
return;
}
// Log when the API crashes
if (signal === 'uncaughtException' && error) {
logToSyslog(`unraid-api[${process.pid}] Caught exception: ${error.message}\nException origin: ${origin}`).catch(() => undefined);
logToSyslog(`unraid-api[${process.pid}] Caught exception: ${error.message}\nException origin: ${origin}`);
}
logToSyslog('⚠️ UNRAID API crashed with ').catch(() => undefined);
logToSyslog('⚠️ UNRAID API crashed with ');
});
await logToSyslog('✔️ UNRAID API started successfully!');
logToSyslog('✔️ UNRAID API started successfully!');
}
// Load bundled index file
@@ -137,7 +135,7 @@ const commands = {
// In the child, clean up the tracking environment variable
delete process.env._DAEMONIZE_PROCESS;
} else {
await logToSyslog('Daemonizing process.');
logToSyslog('Daemonizing process.');
// Spawn child
const child = spawn(process.execPath, process.argv.slice(2), {
@@ -153,7 +151,7 @@ const commands = {
// Convert process into daemon
child.unref();
await logToSyslog('Daemonized successfully!');
logToSyslog('Daemonized successfully!');
// Exit cleanly
process.exit(0);
@@ -188,7 +186,8 @@ const commands = {
* Print API version.
*/
async version() {
console.log(`Unraid API v${version as string}`);
const apiVersion: string = version;
console.log(`Unraid API v${apiVersion}`);
},
async status() {
// Find all processes called "unraid-api" which aren't this process
@@ -198,7 +197,7 @@ const commands = {
return;
}
const stats = await pidusage(unraidApiPid);
const stats = await pidUsage(unraidApiPid);
console.log(`API has been running for ${prettyMs(stats.elapsed)} and is in "${getEnvironment()}" mode!`);
},
async report() {
@@ -256,14 +255,15 @@ const commands = {
logger.debug('Copying %s to %s', source, destination);
await new Promise<void>((resolve, reject) => {
// Use the native cp command to ensure we're outside the virtual file system
exec(`cp "${source}" "${destination}"`, error => {
if (error) {
return reject(error);
}
exec(`cp "${source}" "${destination}"`, error => {
if (error) {
reject(error);
return;
}
resolve();
});
});
resolve();
});
});
// If there's a process running restart it
const unraidApiPid = await getUnraidApiPid();

45
package-lock.json generated
View File

@@ -1451,7 +1451,6 @@
"version": "9.0.12",
"resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.12.tgz",
"integrity": "sha512-I+bsBr67CurCGnSenZZ7v94gd3tc3+Aj2taxMT4yu4ABLuOgOjeFxX3dokG24ztSRg5tnT00sL8BszO7gSMoIw==",
"dev": true,
"requires": {
"@types/node": "*"
}
@@ -1553,7 +1552,6 @@
"version": "2.5.12",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.12.tgz",
"integrity": "sha512-MKgC4dlq4kKNa/mYrwpKfzQMB5X3ee5U6fSprkKpToBqBmX4nFZL9cW5jl6sWn+xpRJ7ypWh2yyqqr8UUCstSw==",
"dev": true,
"requires": {
"@types/node": "*",
"form-data": "^3.0.0"
@@ -1563,7 +1561,6 @@
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"dev": true,
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
@@ -5813,8 +5810,7 @@
"duplexer": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz",
"integrity": "sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg==",
"dev": true
"integrity": "sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg=="
},
"duplexer3": {
"version": "0.1.4",
@@ -6354,7 +6350,6 @@
"version": "3.3.4",
"resolved": "https://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz",
"integrity": "sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=",
"dev": true,
"requires": {
"duplexer": "~0.1.1",
"from": "~0",
@@ -6369,7 +6364,6 @@
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/split/-/split-0.3.3.tgz",
"integrity": "sha1-zQ7qXmOiEd//frDwkcQTPi0N0o8=",
"dev": true,
"requires": {
"through": "2"
}
@@ -6398,9 +6392,9 @@
},
"dependencies": {
"is-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.0.tgz",
"integrity": "sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw=="
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz",
"integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg=="
}
}
},
@@ -6948,8 +6942,7 @@
"from": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/from/-/from-0.1.7.tgz",
"integrity": "sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4=",
"dev": true
"integrity": "sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4="
},
"fromentries": {
"version": "1.3.2",
@@ -9278,8 +9271,7 @@
"map-stream": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/map-stream/-/map-stream-0.1.0.tgz",
"integrity": "sha1-5WqpTEyAVaFkBKBnS3jyFffI4ZQ=",
"dev": true
"integrity": "sha1-5WqpTEyAVaFkBKBnS3jyFffI4ZQ="
},
"map-visit": {
"version": "1.0.0",
@@ -10733,7 +10725,6 @@
"version": "0.0.11",
"resolved": "https://registry.npmjs.org/pause-stream/-/pause-stream-0.0.11.tgz",
"integrity": "sha1-/lo0sMvOErWqaitAPuLnO2AvFEU=",
"dev": true,
"requires": {
"through": "~2.3"
}
@@ -10941,7 +10932,6 @@
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/ps-tree/-/ps-tree-1.2.0.tgz",
"integrity": "sha512-0VnamPPYHl4uaU/nSFeZZpR21QAWRz+sRv4iW9+v/GS/J5U5iZB5BNN6J0RMoOvdx2gWM2+ZFMIm58q24e4UYA==",
"dev": true,
"requires": {
"event-stream": "=3.3.4"
}
@@ -12487,7 +12477,6 @@
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/stream-combiner/-/stream-combiner-0.0.4.tgz",
"integrity": "sha1-TV5DPBhSYd3mI8o/RMWGvPXErRQ=",
"dev": true,
"requires": {
"duplexer": "~0.1.1"
}
@@ -12966,8 +12955,7 @@
"through": {
"version": "2.3.8",
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=",
"dev": true
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU="
},
"through2": {
"version": "2.0.5",
@@ -13922,7 +13910,6 @@
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/zx/-/zx-4.2.0.tgz",
"integrity": "sha512-/4f7FaJecA9I655KXKXIHO3CFNYjAz2uSmTz6v2eNlKdrQKyz4VyF3RjqFuP6nQG+Hd3+NjOvrVNBkv8Ne9d4Q==",
"dev": true,
"requires": {
"@types/fs-extra": "^9.0.12",
"@types/minimist": "^1.2.2",
@@ -13940,20 +13927,17 @@
"@types/minimist": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz",
"integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==",
"dev": true
"integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ=="
},
"array-union": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/array-union/-/array-union-3.0.1.tgz",
"integrity": "sha512-1OvF9IbWwaeiM9VhzYXVQacMibxpXOMYVNIvMtKRyX9SImBXpKcFr8XvFDeEslCyuH/t6KRt7HEO94AlP8Iatw==",
"dev": true
"integrity": "sha512-1OvF9IbWwaeiM9VhzYXVQacMibxpXOMYVNIvMtKRyX9SImBXpKcFr8XvFDeEslCyuH/t6KRt7HEO94AlP8Iatw=="
},
"chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dev": true,
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
@@ -13963,7 +13947,6 @@
"version": "3.2.7",
"resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.7.tgz",
"integrity": "sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q==",
"dev": true,
"requires": {
"@nodelib/fs.stat": "^2.0.2",
"@nodelib/fs.walk": "^1.2.3",
@@ -13976,7 +13959,6 @@
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz",
"integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==",
"dev": true,
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^6.0.1",
@@ -13987,7 +13969,6 @@
"version": "12.0.2",
"resolved": "https://registry.npmjs.org/globby/-/globby-12.0.2.tgz",
"integrity": "sha512-lAsmb/5Lww4r7MM9nCCliDZVIKbZTavrsunAsHLr9oHthrZP1qi7/gAnHOsUs9bLvEt2vKVJhHmxuL7QbDuPdQ==",
"dev": true,
"requires": {
"array-union": "^3.0.1",
"dir-glob": "^3.0.1",
@@ -14001,7 +13982,6 @@
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz",
"integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==",
"dev": true,
"requires": {
"graceful-fs": "^4.1.6",
"universalify": "^2.0.0"
@@ -14010,20 +13990,17 @@
"slash": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-4.0.0.tgz",
"integrity": "sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==",
"dev": true
"integrity": "sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew=="
},
"universalify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz",
"integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==",
"dev": true
"integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ=="
},
"which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
"dev": true,
"requires": {
"isexe": "^2.0.0"
}

View File

@@ -66,7 +66,7 @@
"dockerode": "^3.3.0",
"dot-prop": "^6.0.1",
"dotenv": "^10.0.0",
"execa": "^5.0.0",
"execa": "^5.1.1",
"express": "^4.17.1",
"express-list-endpoints": "^5.0.0",
"fetch-retry": "^4.1.1",
@@ -160,8 +160,7 @@
"ts-node": "10.1.0",
"tsup": "^4.12.5",
"typescript": "4.3.5",
"typescript-coverage-report": "^0.6.0",
"zx": "^4.2.0"
"typescript-coverage-report": "^0.6.0"
},
"optionalDependencies": {
"@vmngr/libvirt": "github:omgimalexis/libvirt"
@@ -263,7 +262,6 @@
"upcast",
"uuid",
"uuid-apikey",
"xhr2",
"zx"
"xhr2"
]
}
}