Use HTTP shutdown instead

Summary: Use the newly exposed HTTP shutdown API. It is simpler.

Reviewed By: antonk52

Differential Revision: D49499264

fbshipit-source-id: 2d81db1d1a66c0b7550ee1245e51d8f1a8671aa6
This commit is contained in:
Lorenzo Blasa
2023-09-21 06:59:22 -07:00
committed by Facebook GitHub Bot
parent 6df27824b7
commit b856180530
3 changed files with 24 additions and 64 deletions

View File

@@ -30,12 +30,6 @@ import {isTest} from 'flipper-common';
import exitHook from 'exit-hook';
import {getAuthToken} from 'flipper-server-core';
import {findInstallation} from './findInstallation';
import ReconnectingWebSocket from 'reconnecting-websocket';
import {
createFlipperServerWithSocket,
FlipperServerState,
} from 'flipper-server-client';
import WS from 'ws';
const argv = yargs
.usage('yarn flipper-server [args]')
@@ -104,39 +98,17 @@ const rootPath = argv.bundler
: path.resolve(__dirname, '..'); // in pre packaged versions of the server, static is copied inside the package
const staticPath = path.join(rootPath, 'static');
async function connectToRunningServer(url: URL) {
console.info(`[flipper-server] Obtain connection to existing server.`);
const options = {
WebSocket: class WSWithUnixDomainSocketSupport extends WS {
constructor(url: string, protocols: string | string[]) {
// Flipper exports could be large, and we snd them over the wire
// Setting this limit fairly high (1GB) to allow any reasonable Flipper export to be loaded
super(url, protocols, {maxPayload: 1024 * 1024 * 1024});
}
},
};
const socket = new ReconnectingWebSocket(url.toString(), [], options);
const server = await createFlipperServerWithSocket(
socket as WebSocket,
(_state: FlipperServerState) => {},
);
return server;
}
async function shutdown() {
async function shutdown(): Promise<boolean> {
console.info('[flipper-server] Attempt to shutdown.');
const token = await getAuthToken();
try {
const response = await fetch(`http://localhost:${argv.port}/shutdown`);
const json = await response.json();
const searchParams = new URLSearchParams(token ? {token} : {});
const url = new URL(`ws://localhost:${argv.port}?${searchParams}`);
return json?.success;
} catch {}
const server = await connectToRunningServer(url);
await server.exec('shutdown').catch(() => {
/** shutdown will ultimately make this request fail, ignore error. */
console.info('[flipper-server] Shutdown may have succeeded');
});
server.close();
return false;
}
async function start() {
@@ -193,7 +165,8 @@ async function start() {
console.info(`[flipper-server] Not replacing existing instance, exiting`);
return;
}
await shutdown();
const success = await shutdown();
console.info(`[flipper-server] Shutdown: ${success}`);
}
const t3 = performance.now();
@@ -238,6 +211,7 @@ async function start() {
const launchedMS = t6 - t5;
exitHook(async () => {
console.log('[flipper-server] Shutdown Flipper server');
await flipperServer.close();
});