Reintroduce timeout and is production check

Summary:
For dev, let's not kill the server.
Also, reintroduce a timeout of 60 seconds before disconnecting.

Reviewed By: antonk52

Differential Revision: D48432317

fbshipit-source-id: bac6f67101e5be481af06a5ea6ccb3b3134c4075
This commit is contained in:
Lorenzo Blasa
2023-08-17 13:46:08 -07:00
committed by Facebook GitHub Bot
parent ce13ee426f
commit 3f37d29b95

View File

@@ -17,6 +17,7 @@ import {
SystemError,
getLogger,
CompanionEventWebSocketMessage,
isProduction,
} from 'flipper-common';
import {FlipperServerImpl} from '../FlipperServerImpl';
import {RawData, WebSocketServer} from 'ws';
@@ -38,6 +39,7 @@ const safe = (f: () => void) => {
};
let numberOfConnectedClients = 0;
let disconnectTimeout: NodeJS.Timeout | undefined;
/**
* Attach and handle incoming messages from clients.
@@ -239,10 +241,19 @@ export function attachSocketServer(
flipperServerCompanion?.destroyAll();
if (getFlipperServerConfig().environmentInfo.isHeadlessBuild) {
if (numberOfConnectedClients === 0) {
console.info('Shutdown as no clients are currently connected');
process.exit(0);
if (disconnectTimeout) {
clearTimeout(disconnectTimeout);
}
/**
* If, after 60 seconds, there are no more connected clients, we exit the process.
*/
disconnectTimeout = setTimeout(() => {
if (numberOfConnectedClients === 0 && isProduction()) {
console.info('Shutdown as no clients are currently connected');
process.exit(0);
}
}, 60 * 1000);
}
}