From 21d0d43edc387dac8d4fc9a788a6548a80cda32a Mon Sep 17 00:00:00 2001 From: mikiher <mikiher@gmail.com> Date: Wed, 27 Dec 2023 15:33:33 +0200 Subject: [PATCH 1/2] Add SocketAuthority.close() --- server/Server.js | 2 +- server/SocketAuthority.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/server/Server.js b/server/Server.js index 3de8ff7f..bb683826 100644 --- a/server/Server.js +++ b/server/Server.js @@ -401,7 +401,7 @@ class Server { Logger.info('Watcher Closed') return new Promise((resolve) => { - this.server.close((err) => { + SocketAuthority.close((err) => { if (err) { Logger.error('Failed to close server', err) } else { diff --git a/server/SocketAuthority.js b/server/SocketAuthority.js index da17f5df..b4698ef9 100644 --- a/server/SocketAuthority.js +++ b/server/SocketAuthority.js @@ -73,6 +73,15 @@ class SocketAuthority { } } + close(callback) { + Logger.info('[SocketAuthority] Shutting down') + // This will close all open socket connections, and also close the underlying http server + if (this.io) + this.io.close(callback) + else + callback() + } + initialize(Server) { this.Server = Server From 9a634e0de576d6e700f3a3a29f1394fc65347432 Mon Sep 17 00:00:00 2001 From: advplyr <advplyr@protonmail.com> Date: Thu, 28 Dec 2023 16:32:21 -0600 Subject: [PATCH 2/2] Add JS docs for server stop --- server/Server.js | 6 +++++- server/SocketAuthority.js | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/server/Server.js b/server/Server.js index bb683826..57b9c74a 100644 --- a/server/Server.js +++ b/server/Server.js @@ -284,7 +284,7 @@ class Server { await this.stop() Logger.info('Server stopped. Exiting.') } else { - Logger.info('SIGINT (Ctrl+C) received again. Exiting immediately.') + Logger.info('SIGINT (Ctrl+C) received again. Exiting immediately.') } process.exit(0) }) @@ -395,6 +395,10 @@ class Server { res.sendStatus(200) } + /** + * Gracefully stop server + * Stops watcher and socket server + */ async stop() { Logger.info('=== Stopping Server ===') await this.watcher.close() diff --git a/server/SocketAuthority.js b/server/SocketAuthority.js index b4698ef9..00f0a63e 100644 --- a/server/SocketAuthority.js +++ b/server/SocketAuthority.js @@ -73,10 +73,15 @@ class SocketAuthority { } } + /** + * Closes the Socket.IO server and disconnect all clients + * + * @param {Function} callback + */ close(callback) { Logger.info('[SocketAuthority] Shutting down') // This will close all open socket connections, and also close the underlying http server - if (this.io) + if (this.io) this.io.close(callback) else callback()