Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scully): kill background server on exit #1535

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`);
}