Replace existing running instance, if any

Summary:
Replace an existing running instance, if any.

This is useful for:
- Applying updates
- Ensuring freshness of server

Reviewed By: passy

Differential Revision: D46146814

fbshipit-source-id: bfb760f3ab26b7632510773609f1c6ca3a97c4ec
This commit is contained in:
Lorenzo Blasa
2023-05-25 06:32:18 -07:00
committed by Facebook GitHub Bot
parent 558316153c
commit 76fbaf245c
5 changed files with 63 additions and 5 deletions

View File

@@ -17,12 +17,14 @@
"flipper-pkg-lib": "0.0.0", "flipper-pkg-lib": "0.0.0",
"flipper-server-companion": "0.0.0", "flipper-server-companion": "0.0.0",
"flipper-server-core": "0.0.0", "flipper-server-core": "0.0.0",
"flipper-server-client": "0.0.0",
"fs-extra": "^11.1.0", "fs-extra": "^11.1.0",
"http-proxy": "^1.18.1", "http-proxy": "^1.18.1",
"metro": "^0.70.2", "metro": "^0.70.2",
"open": "^8.3.0", "open": "^8.3.0",
"p-filter": "^2.1.0", "p-filter": "^2.1.0",
"ws": "^8.5.0", "ws": "8.8.0",
"reconnecting-websocket": "^4.4.0",
"xdg-basedir": "^4", "xdg-basedir": "^4",
"yargs": "^17.6.2" "yargs": "^17.6.2"
}, },

View File

@@ -28,6 +28,12 @@ import {isTest} from 'flipper-common';
import exitHook from 'exit-hook'; import exitHook from 'exit-hook';
import {getAuthToken} from 'flipper-server-core'; import {getAuthToken} from 'flipper-server-core';
import {findInstallation} from './findInstallation'; import {findInstallation} from './findInstallation';
import ReconnectingWebSocket from 'reconnecting-websocket';
import {
createFlipperServerWithSocket,
FlipperServerState,
} from 'flipper-server-client';
import WS from 'ws';
const argv = yargs const argv = yargs
.usage('yarn flipper-server [args]') .usage('yarn flipper-server [args]')
@@ -71,6 +77,11 @@ const argv = yargs
type: 'boolean', type: 'boolean',
default: true, default: true,
}, },
replace: {
describe: 'Replaces any running instance, if any.',
type: 'boolean',
default: true,
},
}) })
.version('DEV') .version('DEV')
.help() .help()
@@ -87,6 +98,41 @@ 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 connectToRunningServer(url: URL) {
console.info(`[flipper-server] Obtain connection to existing server.`);
console.info(`[flipper-server] URL: ${url}`);
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() {
console.info('[flipper-server] Attempt to shutdown.');
const tokenPath = path.resolve(staticPath, 'auth.token');
const token = await fs.readFile(tokenPath, 'utf-8');
const searchParams = new URLSearchParams({token: token ?? ''});
const url = new URL(`ws://localhost:${argv.port}?${searchParams}`);
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');
});
}
async function start() { async function start() {
const enhanceLogger = await initializeLogger(staticPath); const enhanceLogger = await initializeLogger(staticPath);
@@ -119,8 +165,14 @@ async function start() {
); );
if (await checkPortInUse(argv.port)) { if (await checkPortInUse(argv.port)) {
console.warn(`[flipper-server] Port ${argv.port} is already in use`); console.warn(`[flipper-server] Port ${argv.port} is already in use.`);
return; if (!argv.replace) {
console.info(
`[flipper-server] Not replacing existing instance, exiting.`,
);
return;
}
await shutdown();
} }
const {app, server, socket, readyForIncomingConnections} = await startServer({ const {app, server, socket, readyForIncomingConnections} = await startServer({

View File

@@ -3,7 +3,7 @@
"compilerOptions": { "compilerOptions": {
"outDir": "lib", "outDir": "lib",
"rootDir": "src", "rootDir": "src",
"lib": ["ES2019"], "lib": ["DOM", "ES2019"],
"types": ["../types/flipperGlobals", "../types/metro-resolver", "../types/metro"] "types": ["../types/flipperGlobals", "../types/metro-resolver", "../types/metro"]
}, },
"include": ["./src/*"], "include": ["./src/*"],
@@ -17,6 +17,9 @@
{ {
"path": "../flipper-server-companion" "path": "../flipper-server-companion"
}, },
{
"path": "../flipper-server-client"
},
{ {
"path": "../pkg-lib" "path": "../pkg-lib"
} }

View File

@@ -252,6 +252,7 @@ async function linkLocalDeps(buildFolder: string) {
'flipper-common': `file:${rootDir}/flipper-common`, 'flipper-common': `file:${rootDir}/flipper-common`,
'flipper-frontend-core': `file:${rootDir}/flipper-frontend-core`, 'flipper-frontend-core': `file:${rootDir}/flipper-frontend-core`,
'flipper-plugin-core': `file:${rootDir}/flipper-plugin-core`, 'flipper-plugin-core': `file:${rootDir}/flipper-plugin-core`,
'flipper-server-client': `file:${rootDir}/flipper-server-client`,
'flipper-server-companion': `file:${rootDir}/flipper-server-companion`, 'flipper-server-companion': `file:${rootDir}/flipper-server-companion`,
'flipper-server-core': `file:${rootDir}/flipper-server-core`, 'flipper-server-core': `file:${rootDir}/flipper-server-core`,
'flipper-pkg-lib': `file:${rootDir}/pkg-lib`, 'flipper-pkg-lib': `file:${rootDir}/pkg-lib`,

View File

@@ -15434,7 +15434,7 @@ ws@^7.0.0, ws@^7.4.5, ws@^7.5.1:
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.7.tgz#9e0ac77ee50af70d58326ecff7e85eb3fa375e67" resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.7.tgz#9e0ac77ee50af70d58326ecff7e85eb3fa375e67"
integrity sha512-KMvVuFzpKBuiIXW3E4u3mySRO2/mCHSyZDJQM5NQ9Q9KHWHWh0NHgfbRMLLrceUK5qAL4ytALJbpRMjixFZh8A== integrity sha512-KMvVuFzpKBuiIXW3E4u3mySRO2/mCHSyZDJQM5NQ9Q9KHWHWh0NHgfbRMLLrceUK5qAL4ytALJbpRMjixFZh8A==
ws@^8.5.0, ws@^8.6.0: ws@^8.6.0:
version "8.6.0" version "8.6.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.6.0.tgz#e5e9f1d9e7ff88083d0c0dd8281ea662a42c9c23" resolved "https://registry.yarnpkg.com/ws/-/ws-8.6.0.tgz#e5e9f1d9e7ff88083d0c0dd8281ea662a42c9c23"
integrity sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw== integrity sha512-AzmM3aH3gk0aX7/rZLYvjdvZooofDu3fFOzGqcSnQ1tOcTWwhM/o+q++E8mAyVVIyUdajrkzWUGftaVSDLn1bw==