Shutdown after 5 hours
Summary: If after 5 hours there are no connected clients, shutdown the server. This is to prevent cases whereas the long-lived instance makes users use stale versions of Flipper if Flipper stays indefinitely running in the background. Reviewed By: passy Differential Revision: D51111926 fbshipit-source-id: 4c38e392cf8a6a4fb840bffdea92c0b0314aefb9
This commit is contained in:
committed by
Facebook GitHub Bot
parent
640fb86edc
commit
3993e7461d
@@ -17,6 +17,7 @@ import {
|
|||||||
SystemError,
|
SystemError,
|
||||||
getLogger,
|
getLogger,
|
||||||
CompanionEventWebSocketMessage,
|
CompanionEventWebSocketMessage,
|
||||||
|
isProduction,
|
||||||
} from 'flipper-common';
|
} from 'flipper-common';
|
||||||
import {FlipperServerImpl} from '../FlipperServerImpl';
|
import {FlipperServerImpl} from '../FlipperServerImpl';
|
||||||
import {RawData, WebSocketServer} from 'ws';
|
import {RawData, WebSocketServer} from 'ws';
|
||||||
@@ -27,6 +28,7 @@ import {
|
|||||||
import {URLSearchParams} from 'url';
|
import {URLSearchParams} from 'url';
|
||||||
import {tracker} from '../tracker';
|
import {tracker} from '../tracker';
|
||||||
import {performance} from 'perf_hooks';
|
import {performance} from 'perf_hooks';
|
||||||
|
import {getFlipperServerConfig} from '../FlipperServerConfig';
|
||||||
|
|
||||||
const safe = (f: () => void) => {
|
const safe = (f: () => void) => {
|
||||||
try {
|
try {
|
||||||
@@ -38,8 +40,8 @@ const safe = (f: () => void) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
||||||
let numberOfConnectedClients = 0;
|
let numberOfConnectedClients = 0;
|
||||||
|
let disconnectTimeout: NodeJS.Timeout | undefined;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach and handle incoming messages from clients.
|
* Attach and handle incoming messages from clients.
|
||||||
@@ -68,6 +70,10 @@ export function attachSocketServer(
|
|||||||
console.log('Client connected', clientAddress);
|
console.log('Client connected', clientAddress);
|
||||||
numberOfConnectedClients++;
|
numberOfConnectedClients++;
|
||||||
|
|
||||||
|
if (disconnectTimeout) {
|
||||||
|
clearTimeout(disconnectTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
clearTimeout(browserConnectionTimeout);
|
clearTimeout(browserConnectionTimeout);
|
||||||
tracker.track('browser-connection-created', {
|
tracker.track('browser-connection-created', {
|
||||||
successful: true,
|
successful: true,
|
||||||
@@ -254,6 +260,25 @@ export function attachSocketServer(
|
|||||||
code,
|
code,
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
getFlipperServerConfig().environmentInfo.isHeadlessBuild &&
|
||||||
|
isProduction()
|
||||||
|
) {
|
||||||
|
const FIVE_HOURS = 5 * 60 * 60 * 1000;
|
||||||
|
if (disconnectTimeout) {
|
||||||
|
clearTimeout(disconnectTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnectTimeout = setTimeout(() => {
|
||||||
|
if (numberOfConnectedClients === 0) {
|
||||||
|
console.info(
|
||||||
|
'[flipper-server] Shutdown as no clients are currently connected',
|
||||||
|
);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
}, FIVE_HOURS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.on('close', (code, _reason) => {
|
client.on('close', (code, _reason) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user