Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(csi-532): added logs for restarting #492

Merged
merged 14 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions modules/api-svc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mojaloop/sdk-scheme-adapter-api-svc",
"version": "20.8.0-snapshot.5",
"version": "20.8.0-snapshot.9",
"description": "An adapter for connecting to Mojaloop API enabled switches.",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down Expand Up @@ -72,7 +72,7 @@
"@mojaloop/sdk-scheme-adapter-private-shared-lib": "workspace:^",
"@mojaloop/sdk-standard-components": "18.4.0",
"ajv": "8.17.1",
"axios": "^1.7.5",
"axios": "^1.7.7",
"co-body": "^6.2.0",
"dotenv": "^16.4.5",
"env-var": "^7.5.0",
Expand Down Expand Up @@ -102,14 +102,14 @@
"babel-jest": "^29.7.0",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^28.8.0",
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-jest": "^28.8.2",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"nock": "^13.5.5",
"npm-check-updates": "^16.7.10",
"openapi-response-validator": "^12.1.3",
"openapi-typescript": "^7.3.0",
"openapi-typescript": "^7.4.0",
"redis-mock": "^0.56.3",
"replace": "^1.2.2",
"standard-version": "^9.5.0",
Expand Down
33 changes: 27 additions & 6 deletions modules/api-svc/src/ControlAgent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const jsonPatch = require('fast-json-patch');
const { generateSlug } = require('random-word-slugs');
const _ = require('lodash');

const FORCE_WS_CLOSE_TIMEOUT_MS = 5000;

/**************************************************************************
* The message protocol messages, verbs, and errors
*************************************************************************/
Expand Down Expand Up @@ -136,8 +138,8 @@ class Client extends ws {
static Create(...args) {
return new Promise((resolve, reject) => {
const client = new Client(...args);
client.on('open', () => resolve(client));
client.on('error', (err) => reject(err));
client.once('open', () => resolve(client));
client.once('error', (err) => reject(err));
client.on('message', client._handle);
});
}
Expand All @@ -159,8 +161,27 @@ class Client extends ws {

// Close connection
async stop() {
this._logger.isInfoEnabled && this._logger.info('Control client shutting down...');
this.close();
this._logger.isDebugEnabled && this._logger.debug('Control client shutting down...');
return new Promise((resolve) => {
let timer = setTimeout(() => {
this._logger.isInfoEnabled && this._logger.info('Control client forced to close');
timer = null;
resolve(false);
}, FORCE_WS_CLOSE_TIMEOUT_MS);

this.once('close', () => {
this._logger.isInfoEnabled && this._logger.info('Control client is closed');
if (timer) clearTimeout(timer);
resolve(true);
});
this.once('error', (error) => {
this._logger.isWarnEnabled && this._logger.push({ error }).warn('Control client failed to close');
if (timer) clearTimeout(timer);
resolve(false);
});

this.close();
});
}

// Handle incoming message from the server.
Expand All @@ -181,14 +202,14 @@ class Client extends ws {
case VERB.NOTIFY: {
const dup = JSON.parse(JSON.stringify(this._appConfig)); // fast-json-patch explicitly mutates
_.merge(dup, msg.data);
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug(`Emitting new agent configuration [${VERB.NOTIFY}]`);
this.emit(EVENT.RECONFIGURE, dup);
break;
}
case VERB.PATCH: {
const dup = JSON.parse(JSON.stringify(this._appConfig)); // fast-json-patch explicitly mutates
jsonPatch.applyPatch(dup, msg.data);
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug(`Emitting new agent configuration [${VERB.PATCH}]`);
this.emit(EVENT.RECONFIGURE, dup);
break;
}
Expand Down
16 changes: 8 additions & 8 deletions modules/api-svc/src/ControlServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,19 @@ class Server extends ws.Server {
client.send(build.CONFIGURATION.NOTIFY(this._appConfig, msg.id));
break;
case VERB.NOTIFY: {
const dup = structuredClone(this._appConfig); // fast-json-patch explicitly mutates
_.merge(dup, msg.data);
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this.emit(EVENT.RECONFIGURE, dup);
const newConf = structuredClone(this._appConfig); // fast-json-patch explicitly mutates
_.merge(newConf, msg.data);
this._logger.isDebugEnabled && this._logger.push({ oldConf: this._appConfig, newConf }).debug(`Emitting new configuration [${VERB.NOTIFY}]`);
this.emit(EVENT.RECONFIGURE, newConf);
break;
}
case VERB.PATCH: {
// TODO: validate the incoming patch? Or assume clients have used the
// client library?
const dup = structuredClone(this._appConfig); // fast-json-patch explicitly mutates
jsonPatch.applyPatch(dup, msg.data);
logger.isDebugEnabled && logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this.emit(EVENT.RECONFIGURE, dup);
const newConf = structuredClone(this._appConfig); // fast-json-patch explicitly mutates
jsonPatch.applyPatch(newConf, msg.data);
logger.isDebugEnabled && logger.push({ oldConf: this._appConfig, newConf }).debug(`Emitting new configuration [${VERB.PATCH}]`);
this.emit(EVENT.RECONFIGURE, newConf);
break;
}
default:
Expand Down
10 changes: 8 additions & 2 deletions modules/api-svc/src/InboundServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,16 @@ class InboundServer extends EventEmitter {

async stop() {
if (this._server.listening) {
await new Promise(resolve => this._server.close(resolve));
await new Promise(resolve => {
this._server.close(() => {
this._logger.isDebugEnabled && this._logger.debug('inbound API is closed');
resolve();
});
this._server.closeAllConnections();
});
}
await this._api.stop();
this._logger.isInfoEnabled && this._logger.info('inbound shut down complete');
this._logger.isInfoEnabled && this._logger.info('inbound API shut down complete');
}

_createServer(tlsEnabled, tlsCreds, handler) {
Expand Down
8 changes: 7 additions & 1 deletion modules/api-svc/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class Server extends EventEmitter {
// todo: clarify, why do we need this method?
// (!) lots of code duplication
async restart(newConf) {
this.logger.isDebugEnabled && this.logger.debug('Server is restarting...');
const updateLogger = !_.isEqual(newConf.logIndent, this.conf.logIndent);
if (updateLogger) {
this.logger = new Logger.Logger({
Expand Down Expand Up @@ -263,7 +264,11 @@ class Server extends EventEmitter {
appConfig: newConf,
});
this.controlClient.on(ControlAgent.EVENT.RECONFIGURE, this.restart.bind(this));
this.controlClient.on('close', () => setTimeout(() => this.restart(_.merge({}, newConf, { control: { stopped: Date.now() } })), RESTART_INTERVAL_MS));
this.controlClient.on('close', () => setTimeout(() => {
this.restart(_.merge({}, newConf, {
control: { stopped: Date.now() }
}));
}, RESTART_INTERVAL_MS));
}
}

Expand Down Expand Up @@ -299,6 +304,7 @@ class Server extends EventEmitter {
await Promise.all([
oldCache?.disconnect(),
]);
this.logger.isDebugEnabled && this.logger.debug('Server is restarted');
}

stop() {
Expand Down
4 changes: 4 additions & 0 deletions modules/api-svc/src/lib/model/OutboundTransfersModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class OutboundTransfersModel {
};

this.getServicesFxpResponse = config.getServicesFxpResponse; // todo: replace with real request

this._logger.isDebugEnabled && this._logger.push(config.outbound.tls.creds).debug('OutboundTransfersModel is created with outbound.tls.creds');
}


Expand Down Expand Up @@ -516,6 +518,8 @@ class OutboundTransfersModel {
async _requestServicesFxp() {
this.data.fxProviders = this.getServicesFxpResponse;
// todo: add impl. with real http-request
this._logger.isInfoEnabled && this._logger.push(this.data.fxProviders).info('servicesFxp configured response');

if (!this.data.fxProviders?.length) {
throw new Error(ErrorMessages.noFxProviderDetected);
}
Expand Down
39 changes: 12 additions & 27 deletions modules/api-svc/src/lib/model/lib/requests/backendRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,8 @@ class BackendRequests {
uri: buildUrl(this.backendEndpoint, url),
headers: this._buildHeaders(),
};

// Note we do not JWS sign requests with no body i.e. GET requests

try {
this.logger.isDebugEnabled && this.logger.push({ reqOpts }).debug('Executing HTTP GET');
return request({...reqOpts, agent: this.agent}).then(throwOrJson);
}
catch (e) {
this.logger.isErrorEnabled && this.logger.push({ e }).error('Error attempting HTTP GET');
throw e;
}
return this.sendRequest(reqOpts);
}


Expand All @@ -229,15 +220,7 @@ class BackendRequests {
headers: this._buildHeaders(),
body: JSON.stringify(body)
};

try {
this.logger.isDebugEnabled && this.logger.push({ reqOpts }).debug('Executing HTTP PUT');
return request({...reqOpts, agent: this.agent}).then(throwOrJson);
}
catch (e) {
this.logger.push({ e }).bebug('Error attempting HTTP PUT');
throw e;
}
return this.sendRequest(reqOpts);
}


Expand All @@ -248,15 +231,17 @@ class BackendRequests {
headers: this._buildHeaders(),
body: JSON.stringify(body),
};
return this.sendRequest(reqOpts);
}

try {
this.logger.isDebugEnabled && this.logger.push({ reqOpts }).debug('Executing HTTP POST');
return request({...reqOpts, agent: this.agent}).then(throwOrJson);
}
catch (e) {
this.logger.isErrorEnabled && this.logger.push({ e }).error('Error attempting POST.');
throw e;
}
async sendRequest(reqOptions) {
this.logger.isDebugEnabled && this.logger.push({ reqOptions }).debug(`Executing HTTP ${reqOptions?.method}...`);
return request({ ...reqOptions, agent: this.agent })
.then(throwOrJson)
.catch(e => {
this.logger.push({ e }).error(`Error attempting ${reqOptions?.method} ${reqOptions?.uri}`);
throw e;
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/api-svc/src/lib/model/lib/requests/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const throwOrJson = async (res) => {
// success but no content, return null
return null;
}
if(res.statusCode < 200 || res.statusCode >= 300) {
if (res.statusCode < 200 || res.statusCode >= 300) {
// not a successful request
throw new HTTPResponseError({ msg: `Request returned non-success status code ${res.statusCode}`,
res
Expand Down
4 changes: 2 additions & 2 deletions modules/outbound-command-event-handler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mojaloop/sdk-scheme-adapter-outbound-command-event-handler",
"version": "0.2.20-snapshot.16",
"version": "0.2.20-snapshot.20",
"description": "Mojaloop sdk scheme adapter command event handler",
"license": "Apache-2.0",
"homepage": "https://github.com/mojaloop/sdk-scheme-adapter/",
Expand Down Expand Up @@ -58,7 +58,7 @@
"@types/convict": "^6.1.6",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^22.5.1",
"@types/node": "^22.5.3",
"@types/node-cache": "^4.2.5",
"@types/supertest": "^6.0.2",
"@types/swagger-ui-express": "^4.1.6",
Expand Down
4 changes: 2 additions & 2 deletions modules/outbound-domain-event-handler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mojaloop/sdk-scheme-adapter-outbound-domain-event-handler",
"version": "0.2.20-snapshot.16",
"version": "0.2.20-snapshot.20",
"description": "mojaloop sdk scheme adapter outbound domain event handler",
"license": "Apache-2.0",
"homepage": "https://github.com/mojaloop/sdk-scheme-adapter/",
Expand Down Expand Up @@ -56,7 +56,7 @@
"@types/convict": "^6.1.6",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^22.5.1",
"@types/node": "^22.5.3",
"@types/node-cache": "^4.2.5",
"@types/supertest": "^6.0.2",
"@types/swagger-ui-express": "^4.1.6",
Expand Down
4 changes: 2 additions & 2 deletions modules/private-shared-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mojaloop/sdk-scheme-adapter-private-shared-lib",
"version": "0.3.20-snapshot.16",
"version": "0.3.20-snapshot.20",
"description": "SDK Scheme Adapter private shared library.",
"license": "Apache-2.0",
"homepage": "https://github.com/mojaloop/accounts-and-balances-bc/tree/main/modules/private-types",
Expand Down Expand Up @@ -37,7 +37,7 @@
"uuid": "^10.0.0"
},
"devDependencies": {
"@types/node": "^22.5.1",
"@types/node": "^22.5.3",
"@types/uuid": "^10.0.0",
"eslint": "^8.57.0",
"jest": "^29.7.0",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mojaloop/sdk-scheme-adapter",
"version": "23.6.0-snapshot.4",
"version": "23.6.0-snapshot.8",
"description": "mojaloop sdk-scheme-adapter",
"license": "Apache-2.0",
"homepage": "https://github.com/mojaloop/sdk-scheme-adapter",
Expand Down Expand Up @@ -79,7 +79,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^22.5.1",
"@types/node": "^22.5.3",
"@types/node-cache": "^4.2.5",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
Expand Down
Loading