Close server if no clients are connected
Summary: If after 15min there are no connected clients, close flipper server. Reviewed By: antonk52 Differential Revision: D46519563 fbshipit-source-id: c3396ed8987be8ca7075c644734793e4ebceba18
This commit is contained in:
committed by
Facebook GitHub Bot
parent
132a20d5a7
commit
85f5c6f893
@@ -25,6 +25,7 @@ import {
|
|||||||
FlipperServerCompanionEnv,
|
FlipperServerCompanionEnv,
|
||||||
} from 'flipper-server-companion';
|
} from 'flipper-server-companion';
|
||||||
import {URLSearchParams} from 'url';
|
import {URLSearchParams} from 'url';
|
||||||
|
import {getFlipperServerConfig} from '../FlipperServerConfig';
|
||||||
|
|
||||||
const safe = (f: () => void) => {
|
const safe = (f: () => void) => {
|
||||||
try {
|
try {
|
||||||
@@ -36,9 +37,12 @@ const safe = (f: () => void) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let numberOfConnectedClients = 0;
|
||||||
|
let disconnectTimeout: NodeJS.Timeout | undefined;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach and handle incoming messages from clients.
|
* Attach and handle incoming messages from clients.
|
||||||
* @param flipperServer A FlipperServer instance.
|
* @param server A FlipperServer instance.
|
||||||
* @param socket A ws socket on which to listen for events.
|
* @param socket A ws socket on which to listen for events.
|
||||||
*/
|
*/
|
||||||
export function attachSocketServer(
|
export function attachSocketServer(
|
||||||
@@ -53,6 +57,8 @@ export function attachSocketServer(
|
|||||||
'';
|
'';
|
||||||
|
|
||||||
console.log('Client connected', clientAddress);
|
console.log('Client connected', clientAddress);
|
||||||
|
numberOfConnectedClients++;
|
||||||
|
|
||||||
let connected = true;
|
let connected = true;
|
||||||
|
|
||||||
let flipperServerCompanion: FlipperServerCompanion | undefined;
|
let flipperServerCompanion: FlipperServerCompanion | undefined;
|
||||||
@@ -227,6 +233,22 @@ export function attachSocketServer(
|
|||||||
console.log(`Client disconnected ${clientAddress}`);
|
console.log(`Client disconnected ${clientAddress}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
numberOfConnectedClients--;
|
||||||
|
if (getFlipperServerConfig().environmentInfo.isHeadlessBuild) {
|
||||||
|
if (disconnectTimeout) {
|
||||||
|
clearTimeout(disconnectTimeout);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* If, after 15 min, there are no more connected clients, we exit the process.
|
||||||
|
*/
|
||||||
|
disconnectTimeout = setTimeout(() => {
|
||||||
|
if (numberOfConnectedClients === 0) {
|
||||||
|
console.info('Shutdown as no clients are currently connected');
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
}, 15 * 60 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
connected = false;
|
connected = false;
|
||||||
server.offAny(onServerEvent);
|
server.offAny(onServerEvent);
|
||||||
flipperServerCompanion?.destroyAll();
|
flipperServerCompanion?.destroyAll();
|
||||||
|
|||||||
Reference in New Issue
Block a user