From a26a7cce2fba85fc5fae35a018f91efe0d12a1c3 Mon Sep 17 00:00:00 2001 From: Kasra Bigdeli Date: Wed, 14 Aug 2024 22:11:01 -0700 Subject: [PATCH] Fixed nginx --- src/user/system/LoadBalancerManager.ts | 82 ++++++++++++++------------ 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/src/user/system/LoadBalancerManager.ts b/src/user/system/LoadBalancerManager.ts index 23c5e88..f36d175 100644 --- a/src/user/system/LoadBalancerManager.ts +++ b/src/user/system/LoadBalancerManager.ts @@ -184,38 +184,7 @@ class LoadBalancerManager { return self.createRootConfFile() }) .then(function () { - return self.dockerApi - .executeCommand(CaptainConstants.nginxServiceName, [ - 'nginx', - '-t', - ]) - .then(function (result) { - // nginx:1.24 - // Failed example: - // - // 2024/08/11 22:32:59 [emerg] 28#28: unknown directive "eventsxxx" in /etc/nginx/nginx.conf:8 - // nginx: [emerg] unknown directive "eventsxxx" in /etc/nginx/nginx.conf:8 - // nginx: configuration file /etc/nginx/nginx.conf test failed - - // Successful example: - // - // nginx: the configuration file /etc/nginx/nginx.conf syntax is ok - // nginx: configuration file /etc/nginx/nginx.conf test is successful - - if (result.indexOf('test is successful') < 0) { - throw ApiStatusCodes.createError( - ApiStatusCodes.STATUS_ERROR_NGINX_VALIDATION_FAILED, - result - ) - } - }) - }) - - .then(function () { - Logger.d('sendReloadSignal...') - return self.dockerApi.sendSingleContainerKillHUP( - CaptainConstants.nginxServiceName - ) + return self.validateNginxConfigAndReload() }) .then(function () { Logger.d('SUCCESS: UNLocking NGINX configuration reloading...') @@ -232,6 +201,43 @@ class LoadBalancerManager { }) } + validateNginxConfigAndReload() { + const self = this + return Promise.resolve() + .then(function () { + return self.dockerApi.executeCommand( + CaptainConstants.nginxServiceName, + ['nginx', '-t'] + ) + }) + .then(function (result) { + // nginx:1.24 + // Failed example: + // + // 2024/08/11 22:32:59 [emerg] 28#28: unknown directive "eventsxxx" in /etc/nginx/nginx.conf:8 + // nginx: [emerg] unknown directive "eventsxxx" in /etc/nginx/nginx.conf:8 + // nginx: configuration file /etc/nginx/nginx.conf test failed + + // Successful example: + // + // nginx: the configuration file /etc/nginx/nginx.conf syntax is ok + // nginx: configuration file /etc/nginx/nginx.conf test is successful + + if (result.indexOf('test is successful') < 0) { + throw ApiStatusCodes.createError( + ApiStatusCodes.STATUS_ERROR_NGINX_VALIDATION_FAILED, + result + ) + } + }) + .then(function () { + Logger.d('sendReloadSignal...') + return self.dockerApi.sendSingleContainerKillHUP( + CaptainConstants.nginxServiceName + ) + }) + } + getServerList() { const self = this @@ -711,12 +717,6 @@ class LoadBalancerManager { .then(function () { return fs.ensureDir(CaptainConstants.nginxSharedPathOnHost) }) - .then(function () { - Logger.d( - 'Updating Load Balancer - Setting up NGINX conf file...' - ) - return self.rePopulateNginxConfigFile() - }) .then(function () { return dockerApi.isServiceRunningByName( CaptainConstants.nginxServiceName @@ -755,6 +755,12 @@ class LoadBalancerManager { return true } }) + .then(function () { + Logger.d( + 'Updating Load Balancer - Setting up NGINX conf file...' + ) + return self.rePopulateNginxConfigFile() + }) .then(function () { Logger.d('Updating NGINX service...')