From 0317a06bc85c73cec8d390fa7555fa68521303a8 Mon Sep 17 00:00:00 2001 From: Sander Elias Date: Tue, 1 Feb 2022 19:35:59 +0100 Subject: [PATCH] fix(scully): kill background server on exit (#1535) This will forcefully kill the background server when scully is done processing ISSUES CLOSED: #1513 --- .../src/lib/utils/serverstuff/staticServer.ts | 15 +++------ .../utils/startup/startBackgroundServer.ts | 32 ++++++++++++++----- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/libs/scully/src/lib/utils/serverstuff/staticServer.ts b/libs/scully/src/lib/utils/serverstuff/staticServer.ts index 3df7bd769..1616fdf98 100644 --- a/libs/scully/src/lib/utils/serverstuff/staticServer.ts +++ b/libs/scully/src/lib/utils/serverstuff/staticServer.ts @@ -21,11 +21,10 @@ const dotProps = readAllDotProps(); // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export async function staticServer(port?: number) { try { - const {hostName, distFolder} = dotProps; + const { hostName, distFolder } = dotProps; port = port || dotProps.staticPort; const scullyServer = express(); - // const distFolder = join(scullyConfig.homeFolder, scullyConfig.hostFolder || scullyConfig.distFolder); if (tds) { dataServerInstance = await startDataServer(ssl); @@ -71,7 +70,7 @@ export async function staticServer(port?: number) { angularDistServer.get('/_pong', (req, res) => { res.json({ res: true, - ...readAllDotProps() + ...readAllDotProps(), }); }); angularDistServer.get('/killMe', async (req, res) => { @@ -89,13 +88,9 @@ export async function staticServer(port?: number) { angularDistServer.get('/*', handleUnknownRoute); - angularServerInstance = addSSL(angularDistServer, hostName, dotProps.appPort).listen( - dotProps.appPort, - hostName, - (x) => { - logOk(`Started Angular distribution server on "${yellow(`http${ssl ? 's' : ''}://${hostName}:${dotProps.appPort}/`)}" `); - } - ); + angularServerInstance = addSSL(angularDistServer, hostName, dotProps.appPort).listen(dotProps.appPort, hostName, (x) => { + logOk(`Started Angular distribution server on "${yellow(`http${ssl ? 's' : ''}://${hostName}:${dotProps.appPort}/`)}" `); + }); return { angularDistServer, scullyServer, diff --git a/libs/scully/src/lib/utils/startup/startBackgroundServer.ts b/libs/scully/src/lib/utils/startup/startBackgroundServer.ts index 506c0ec29..f51e6d2dc 100644 --- a/libs/scully/src/lib/utils/startup/startBackgroundServer.ts +++ b/libs/scully/src/lib/utils/startup/startBackgroundServer.ts @@ -1,7 +1,21 @@ import { fork } from 'child_process'; import { existsSync } from 'fs-extra'; import { join } from 'path'; -import { captureMessage, configFileName, disableProjectFolderCheck, handle404, log, logError, logOk, logSeverity, pjFirst, port, ScullyConfig, tds } from '../'; +import { + captureMessage, + configFileName, + disableProjectFolderCheck, + handle404, + log, + logError, + logOk, + logSeverity, + pjFirst, + port, + registerExitHandler, + ScullyConfig, + tds, +} from '../'; const baseBinary = join(__dirname, '..', 'scully.js'); @@ -9,8 +23,8 @@ export function startBackgroundServer(scullyConfig: ScullyConfig) { const binary = existsSync(baseBinary) ? baseBinary : ['/dist/scully/src/scully', '/node_modules/.bin/scully', '/node_modules/@scullyio/scully/src/scully'] - .map((p) => join(scullyConfig.homeFolder, p + '.js')) - .find((p) => existsSync(p)); + .map((p) => join(scullyConfig.homeFolder, p + '.js')) + .find((p) => existsSync(p)); if (!binary) { logError('Could not find scully binaries'); @@ -50,17 +64,19 @@ export function startBackgroundServer(scullyConfig: ScullyConfig) { // log(`Starting background servers with: node ${options.join(' ')}`); - fork( - join(__dirname, '../../../scully'), - options - ).on('close', (code) => { - logOk('Scully development Servers stopped') + const serverProcess = fork(join(__dirname, '../../../scully'), options).on('close', (code) => { if (+code > 0) { const message = 'Problem starting background servers ' + code; logError(message); captureMessage(message); process.exit(15); } + logOk('Scully development Servers stopped'); }); + + registerExitHandler(async () => { + await serverProcess.kill(); + }); + // log(` ${green('☺')} Started servers in background`); }