Better shutdown

Summary: ^

Reviewed By: ivanmisuno

Differential Revision: D49593599

fbshipit-source-id: 196e98653cc0cdda4fdf11e366e5d3ab5debe6e2
This commit is contained in:
Lorenzo Blasa
2023-09-25 09:59:24 -07:00
committed by Facebook GitHub Bot
parent 1c4224d716
commit 6e97e73cf1

View File

@@ -20,8 +20,10 @@ import open from 'open';
import os from 'os'; import os from 'os';
import {initCompanionEnv} from 'flipper-server-companion'; import {initCompanionEnv} from 'flipper-server-companion';
import { import {
checkPortInUse, checkServerRunning,
compareServerVersion,
getEnvironmentInfo, getEnvironmentInfo,
shutdownRunningInstance,
startFlipperServer, startFlipperServer,
startServer, startServer,
tracker, tracker,
@@ -98,19 +100,6 @@ const rootPath = argv.bundler
: path.resolve(__dirname, '..'); // in pre packaged versions of the server, static is copied inside the package : path.resolve(__dirname, '..'); // in pre packaged versions of the server, static is copied inside the package
const staticPath = path.join(rootPath, 'static'); const staticPath = path.join(rootPath, 'static');
async function shutdown(): Promise<boolean> {
console.info('[flipper-server] Attempt to shutdown.');
try {
const response = await fetch(`http://localhost:${argv.port}/shutdown`);
const json = await response.json();
return json?.success;
} catch {}
return false;
}
async function start() { async function start() {
const t0 = performance.now(); const t0 = performance.now();
@@ -159,14 +148,25 @@ async function start() {
`[flipper-server][bootstrap] Keytar loaded (${keytarLoadedMS} ms)`, `[flipper-server][bootstrap] Keytar loaded (${keytarLoadedMS} ms)`,
); );
if (await checkPortInUse(argv.port)) { let launchAndFinish = false;
console.warn(`[flipper-server] Port ${argv.port} is already in use`);
if (!argv.replace) { console.info('[flipper-server] Check for running instances');
console.info(`[flipper-server] Not replacing existing instance, exiting`); const existingRunningInstanceVersion = await checkServerRunning(argv.port);
return; if (existingRunningInstanceVersion) {
console.info(
`[flipper-server] Running instance found with version: ${existingRunningInstanceVersion}, current version: ${environmentInfo.appVersion}`,
);
if (
compareServerVersion(
environmentInfo.appVersion,
existingRunningInstanceVersion,
) > 0
) {
console.info(`[flipper-server] Shutdown running instance`);
await shutdownRunningInstance(argv.port);
} else {
launchAndFinish = true;
} }
const success = await shutdown();
console.info(`[flipper-server] Shutdown: ${success}`);
} }
const t3 = performance.now(); const t3 = performance.now();
@@ -175,6 +175,10 @@ async function start() {
`[flipper-server][bootstrap] Check for running instances completed (${runningInstanceShutdownMS} ms)`, `[flipper-server][bootstrap] Check for running instances completed (${runningInstanceShutdownMS} ms)`,
); );
if (launchAndFinish) {
return await launch();
}
const {app, server, socket, readyForIncomingConnections} = await startServer( const {app, server, socket, readyForIncomingConnections} = await startServer(
{ {
staticPath, staticPath,