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

feat(fusion-cli): add graceful shutdown to start command #758

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion fusion-cli-tests/test/e2e/dynamic-import-app/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ test('`fusion build` app with Safari user agent and same-origin', async () => {
await cmd(`build --dir=${dir} --production`, {env});

// Run puppeteer test to ensure that page loads with dynamic content.
const {proc, port} = await start(`--dir=${dir}`, {env});
const {proc, port, promise} = await start(`--dir=${dir}`, {env});
promise.catch(console.error);

const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
Expand Down
5 changes: 3 additions & 2 deletions fusion-cli-tests/test/e2e/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ function cmd(args /*: any */, options /*: any */) {

async function start(args /*: any */, options /*: any */) {
const port = await getPort();
const promise = cmd(`start --port=${port} ${args}`, options);
// $FlowFixMe
const {proc} = cmd(`start --port=${port} ${args}`, options);
const {proc} = promise;
const res = await waitForServer(port);
return {proc, res, port};
return {proc, res, port, promise};
}

async function dev(args /*: any */, options /*: any */) {
Expand Down
31 changes: 30 additions & 1 deletion fusion-cli/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ const fs = require('fs');
const path = require('path');
const cp = require('child_process');

// Gracefully shuts down server when it receives signals
function listenForShutdown(server, ...signals) {
let closing = false;

const shutDown = (signalName, signal) => {
if (closing) {
return;
}
closing = true;

console.info(`Received ${signalName}, draining open connections`);
server.close(() => {
console.info(`Drained connections, stopping server`);
process.exit(128 + signal);
});
};

signals.forEach(signal => {
process.once(signal, shutDown);
});
}

exports.run = async function({dir = '.', environment, port, debug} /*: any */) {
if (debug && !process.env.__FUSION_DEBUGGING__) {
const command = process.argv.shift();
Expand Down Expand Up @@ -39,7 +61,14 @@ exports.run = async function({dir = '.', environment, port, debug} /*: any */) {
const entry = getEntry(env);
// $FlowFixMe
const {start} = require(entry);
return start({dir, port: port || process.env.PORT_HTTP || 3000}); // handle server bootstrap errors (e.g. port already in use)
const server = await start({
dir,
port: port || process.env.PORT_HTTP || 3000,
}); // handle server bootstrap errors (e.g. port already in use)

listenForShutdown(server, 'SIGTERM', 'SIGINT');

return server;
} else {
throw new Error(`App can't start. JS isn't compiled`); // handle compilation errors
}
Expand Down