From fe4d8798279efee98034514fa336e8cefe28241d Mon Sep 17 00:00:00 2001 From: Alexis Date: Fri, 10 Sep 2021 15:52:13 +0930 Subject: [PATCH] fix: move logging to child.on? --- app/cli.ts | 20 ++++++++++++++++---- app/server.ts | 4 ---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/cli.ts b/app/cli.ts index 43e03a6f1..446c15a15 100644 --- a/app/cli.ts +++ b/app/cli.ts @@ -98,8 +98,8 @@ const commands = { setEnv('LOG_TRANSPORT', mainOptions['log-transport']); setEnv('PORT', mainOptions.port); - console.info(`Starting unraid-api v${packageJson.version as string}`); - console.info(`Connecting to the "${getEnvironment()}" environment.`); + logger.info(`Starting unraid-api v${packageJson.version as string}`); + logger.info(`Loading the "${getEnvironment()}" environment.`); // Load bundled index file const indexPath = './index.js'; @@ -110,7 +110,7 @@ const commands = { // In the child, clean up the tracking environment variable delete process.env._DAEMONIZE_PROCESS; } else { - console.debug('Daemonizing process.'); + logger.debug('Daemonizing process.'); // Spawn child const child = spawn(process.execPath, process.argv.slice(2), { @@ -123,10 +123,22 @@ const commands = { detached: true }); + // This should log when the app closes, not the cli + child.on('exit', () => { + logger.info('👋 Farewell. UNRAID API shutting down!'); + }); + + // This should log when the app crashes, not the cli + child.on('error', error => { + logger.log('UNRAiD API crashed with "%" at %s', error.message, error.stack); + }); + // Convert process into daemon child.unref(); - console.log('Daemonized successfully!'); + logger.debug('Daemonized successfully!'); + + logger.info('🌻 UNRAID API started successfully!'); // Exit cleanly process.exit(0); diff --git a/app/server.ts b/app/server.ts index 4cad53ecc..29a991f0a 100644 --- a/app/server.ts +++ b/app/server.ts @@ -214,10 +214,6 @@ if (isNaN(parseInt(port, 10))) { stoppableServer.on('listening', () => { // Set permissions fs.chmodSync(port, 660); - - process.once('exit', () => { - console.info('👋 Farewell. UNRAID API shutting down!'); - }); }); stoppableServer.on('error', async (error: NodeJS.ErrnoException) => {