Skip to content
Merged
Changes from 1 commit
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
69 changes: 47 additions & 22 deletions packages/config/src/bin/dev-proxy-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function stopContainer(containerName: string) {
try {
let isRunning: boolean = true;
try {
execSync(`${execBin} inspect -f '{{.State.Running}}' ${containerName}\n`).toString().trim().toLowerCase() === 'true';
isRunning = execSync(`${execBin} inspect -f '{{.State.Running}}' ${containerName}\n`).toString().trim().toLowerCase() === 'true';
} catch (error) {
isRunning = false;
}
Expand Down Expand Up @@ -254,23 +254,55 @@ async function devProxyScript(
// Exec
let commands: Command[] = [];
let waitOnProcess: ReturnType<typeof exec> | undefined = undefined;
let shuttingDown = false;

const cleanup = () => {
commands.forEach((cmd) => {
if (cmd.pid) {
treeKill(cmd.pid, 'SIGKILL');
const cleanup = (onComplete?: () => void) => {
const pids = [
...commands.map((cmd) => cmd.pid).filter((pid): pid is number => typeof pid === 'number'),
...(waitOnProcess?.pid ? [waitOnProcess.pid] : []),
];

const finish = () => {
stopContainer(DEV_PROXY_CONTAINER_NAME);
removeContainer(DEV_PROXY_CONTAINER_NAME);
if (SPAFallback) {
stopContainer(CHROME_CONTAINTER_NAME);
removeContainer(CHROME_CONTAINTER_NAME);
}
});
if (waitOnProcess?.pid) {
treeKill(waitOnProcess.pid, 'SIGKILL');
console.log('\n');
onComplete?.();
};

if (pids.length === 0) {
finish();
return;
}
stopContainer(DEV_PROXY_CONTAINER_NAME);
if (SPAFallback) {
stopContainer(CHROME_CONTAINTER_NAME);

let remaining = pids.length;
pids.forEach((pid) => {
treeKill(pid, 'SIGKILL', () => {
remaining -= 1;
if (remaining === 0) {
finish();
}
});
});
};

// Register before spawning children so a signal during startup still cleans up
const shutdown = (exitCode = 0) => {
if (shuttingDown) {
return;
}
console.log('\n');
shuttingDown = true;
cleanup(() => {
process.exit(exitCode);
});
};
Comment thread
adonispuente marked this conversation as resolved.
Resolved

process.on('SIGINT', () => shutdown(0));
process.on('SIGTERM', () => shutdown(0));

try {
const outputPath = webpackConfig.output.path;
const proxyEnvVar = process.env.HCC_ENV === 'stage' ? '-e HTTPS_PROXY=$RH_PROXY_URL' : '';
Expand Down Expand Up @@ -304,8 +336,7 @@ async function devProxyScript(
if (SPAFallback) {
const handleServerError = (error: Error) => {
fecLogger(LogType.error, error);
cleanup();
process.exit(1);
shutdown(1);
};

await serveChrome(
Expand Down Expand Up @@ -366,14 +397,8 @@ async function devProxyScript(
);

result.then(
() => {
cleanup();
process.exit(0);
},
() => {
cleanup();
process.exit(0);
},
() => shutdown(0),
() => shutdown(0),
Comment thread
adonispuente marked this conversation as resolved.
Resolved
);
} catch (error) {
fecLogger(LogType.error, error);
Expand Down
Loading