From 46d059c0d72a85609ad4d9346256edc1c25096f8 Mon Sep 17 00:00:00 2001 From: Kamen Stoykov Date: Tue, 12 Jul 2022 11:18:00 +0000 Subject: [PATCH] update ports --- tools/builder/helpers/ServerHelper.js | 39 ++++++++++++++++++++------- tools/builder/targets/DevTarget.js | 9 +++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/tools/builder/helpers/ServerHelper.js b/tools/builder/helpers/ServerHelper.js index 078e9b6..39a5721 100644 --- a/tools/builder/helpers/ServerHelper.js +++ b/tools/builder/helpers/ServerHelper.js @@ -9,7 +9,7 @@ class ServerHelper { this.status = ServerHelper.STATUS_STOPPED; } - restart() { + async restart() { if (this.status === ServerHelper.STATUS_STARTING || this.status === ServerHelper.STATUS_STOPPING) { return; } @@ -19,14 +19,8 @@ class ServerHelper { return; } - this.server.send('SERVER_MSG::EXIT'); - this.server.on('exit', () => { - this.status = ServerHelper.STATUS_STOPPED; - this.start(); - }); - this.server = null; - - this.status = ServerHelper.STATUS_STOPPING; + await this.stop(); + this.start(); } start() { @@ -52,6 +46,33 @@ class ServerHelper { this.status = ServerHelper.STATUS_STARTING; } + stop() { + return new Promise((resolve, reject) => { + const run = async () => { + if (this.status === ServerHelper.STATUS_STOPPED || this.status === ServerHelper.STATUS_STOPPING) { + resolve(); + return; + } + + while (this.status === ServerHelper.STATUS_STARTING) { + await new Promise((resolveInner, rejectInner) => { setTimeout(resolveInner, 100) }); + } + + if (this.status === ServerHelper.STATUS_STARTED) { + this.server.send('SERVER_MSG::EXIT'); + this.server.on('exit', () => { + this.status = ServerHelper.STATUS_STOPPED; + resolve(); + }); + this.server = null; + this.status = ServerHelper.STATUS_STOPPING; + } + } + + run(); + }); + } + onStdOut(data) { console.log(data.toString().trim()); } diff --git a/tools/builder/targets/DevTarget.js b/tools/builder/targets/DevTarget.js index 91ad296..c662506 100644 --- a/tools/builder/targets/DevTarget.js +++ b/tools/builder/targets/DevTarget.js @@ -49,6 +49,15 @@ class DevTarget { if (serverHelper !== null) { serverHelper.start(); + + process.on('SIGINT', async () => { + await serverHelper.stop(); + process.exit(0); + }) + process.on('SIGTERM', async () => { + await serverHelper.stop(); + process.exit(0); + }) } }