Skip to content

Commit

Permalink
fix(scully): kill background server on exit (#1535)
Browse files Browse the repository at this point in the history
This will forcefully kill the background server when scully is done processing

ISSUES CLOSED: #1513
  • Loading branch information
SanderElias authored Feb 1, 2022
1 parent 713dcc8 commit 0317a06
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
15 changes: 5 additions & 10 deletions libs/scully/src/lib/utils/serverstuff/staticServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down
32 changes: 24 additions & 8 deletions libs/scully/src/lib/utils/startup/startBackgroundServer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
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');

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');
Expand Down Expand Up @@ -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`);
}

0 comments on commit 0317a06

Please sign in to comment.