From 30e7a8cdb0249522b1cd7120594c5324393766ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Sat, 10 Jul 2021 15:10:17 +0200 Subject: [PATCH] Delay headless clients startup until server is started --- lib/logs.js | 16 ++++------------ lib/server.js | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/logs.js b/lib/logs.js index 1bf8f9c..3e30109 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -135,18 +135,6 @@ Logs.prototype.readLogFile = function (filename, callback) { fs.readFile(filename, callback) } -Logs.prototype.logServerProcesses = function (prefix, serverProcess, headlessClientProcesses) { - var self = this - this.logServerProcess(serverProcess, prefix, 'server') - headlessClientProcesses.forEach(function (headlessClientProcess, idx) { - self.logServerProcess(headlessClientProcess, prefix, 'hc_' + (idx + 1)) - }) - - if (this.config.type === 'linux') { - this.cleanupOldLogFiles() - } -} - Logs.prototype.logServerProcess = function (serverProcess, prefix, suffix) { if (this.config.type !== 'linux') { return @@ -183,6 +171,10 @@ Logs.prototype.logServerProcess = function (serverProcess, prefix, suffix) { } Logs.prototype.cleanupOldLogFiles = function () { + if (this.config.type !== 'linux') { + return + } + var self = this self.logFiles(function (err, files) { diff --git a/lib/server.js b/lib/server.js index 0546171..98deb30 100644 --- a/lib/server.js +++ b/lib/server.js @@ -89,6 +89,7 @@ Server.prototype.queryStatus = function () { self.state = null } else { self.state = state + self.startHeadlessClientsIfNeeded() } self.emit('state') @@ -178,19 +179,25 @@ Server.prototype.start = function () { this.pid = instance.pid this.instance = instance + this.headlessClientInstances = [] this.queryStatusInterval = setInterval(function () { self.queryStatus() }, queryInterval) - this.startHeadlessClients() - - this.logs.logServerProcesses(this.id, this.instance, this.headlessClientInstances) + this.logs.logServerProcess(this.instance, this.id, 'server') + this.logs.cleanupOldLogFiles() this.emit('state') return this } +Server.prototype.startHeadlessClientsIfNeeded = function () { + if (this.number_of_headless_clients > 0 && this.headlessClientInstances.length === 0) { + this.startHeadlessClients() + } +} + Server.prototype.startHeadlessClients = function () { var parameters = this.getParameters() var self = this @@ -207,6 +214,7 @@ Server.prototype.startHeadlessClients = function () { port: self.port }) var headlessInstance = headless.start() + self.logs.logServerProcess(headlessInstance, self.id, 'hc_' + (i + 1)) return headlessInstance }) @@ -245,6 +253,7 @@ Server.prototype.stopHeadlessClients = function () { this.headlessClientInstances.map(function (headlessClientInstance) { headlessClientInstance.kill() }) + this.headlessClientInstances = [] } Server.prototype.toJSON = function () {