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

[now-static-build] Exit dev server child processes upon SIGINT/SIGTERM #3136

Merged
merged 2 commits into from
Oct 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/now-static-build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import ms from 'ms';
import path from 'path';
import spawn from 'cross-spawn';
import getPort from 'get-port';
import { SpawnOptions } from 'child_process';
import isPortReachable from 'is-port-reachable';
import { ChildProcess, SpawnOptions } from 'child_process';
import { existsSync, readFileSync, statSync, readdirSync } from 'fs';
import { frameworks, Framework } from './frameworks';
import {
Expand Down Expand Up @@ -102,7 +102,20 @@ function getCommand(pkg: PackageJson, cmd: string, { zeroConfig }: Config) {

export const version = 2;

const nowDevScriptPorts = new Map();
const nowDevScriptPorts = new Map<string, number>();
const nowDevChildProcesses = new Set<ChildProcess>();

['SIGINT', 'SIGTERM'].forEach(signal => {
process.once(signal as NodeJS.Signals, () => {
for (const child of nowDevChildProcesses) {
debug(
`Got ${signal}, killing dev server child process (pid=${child.pid})`
);
process.kill(child.pid, signal);
}
process.exit(0);
});
});

const getDevRoute = (srcBase: string, devPort: number, route: Route) => {
const basic: Route = {
Expand Down Expand Up @@ -272,8 +285,9 @@ export async function build({
env: { ...process.env, PORT: String(devPort) },
};

const child = spawn('yarn', ['run', devScript], opts);
const child: ChildProcess = spawn('yarn', ['run', devScript], opts);
child.on('exit', () => nowDevScriptPorts.delete(entrypoint));
nowDevChildProcesses.add(child);

// Now wait for the server to have listened on `$PORT`, after which we
// will ProxyPass any requests to that development server that come in
Expand Down