Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zetxx committed Jan 31, 2020
1 parent d506c44 commit c1fa64a
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const errors = require('./errors.js');

const getSerializableMeta = ({timeoutId, ...restMeta}) => restMeta;

class Brid {
constructor() {
this.apiMethods = {};
Expand Down Expand Up @@ -50,22 +48,22 @@ class Brid {
}

registerMethod({method, channel, fn, meta}) {
this.log('info', {in: 'base.registerMethod', description: `register method: ${channel}.${method}`, method, channel, meta: getSerializableMeta(meta)});
this.log('info', {in: 'base.registerMethod', description: `register method: ${channel}.${method}`, method, channel, meta});
!this.apiMethods[channel] && (this.apiMethods[channel] = {});
this.apiMethods[channel][method] = async(ctx, ...args) => {
let [message, meta] = args;
this.log('trace', {in: 'base.registerMethod', description: `exec method: ${channel}.${method}`, message, meta: getSerializableMeta(meta)});
this.log('trace', {in: 'base.registerMethod', description: `exec method: ${channel}.${method}`, message, meta});
return fn.call(ctx, ...args);
};
}

registerApiMethod({method, fn, meta = {}}) {
this.log('info', {in: 'base.registerApiMethod', method, meta: getSerializableMeta(meta)});
this.log('info', {in: 'base.registerApiMethod', method, meta});
this.registerMethod({method, fn, channel: 'api', meta});
}

registerExternalMethod({method, fn, meta = {}}) {
this.log('info', {in: 'base.registerExternalMethod', method, meta: getSerializableMeta(meta)});
this.log('info', {in: 'base.registerExternalMethod', method, meta});
this.registerMethod({method, fn, channel: 'external', meta});
}

Expand Down Expand Up @@ -110,7 +108,7 @@ class Brid {
}

getInternalCommunicationContext(meta, extraContext = {}) {
this.log('debug', {in: 'base.getInternalCommunicationContext', meta: getSerializableMeta(meta)});
this.log('debug', {in: 'base.getInternalCommunicationContext', meta});
return {
sharedContext: extraContext || {},
request: this.remoteApiCallBuild({meta, extraContext}),
Expand All @@ -122,36 +120,36 @@ class Brid {

remoteApiCallBuild({meta, extraContext, isNotification = 0}) {
return async(destination, message, metaParams = {}) => {
this.log('trace', {in: 'base.getInternalCommunicationContext.request', destination, message, metaParams, meta: getSerializableMeta(meta)});
this.log('trace', {in: 'base.getInternalCommunicationContext.request', destination, message, metaParams, meta});
var globTrace = this.getGlobTrace(meta);
try {
let rq = this.remoteApiCall({destination, message, meta: Object.assign({}, meta, {globTrace}, metaParams, {isNotification})});
if (!isNotification) {
return await rq;
}
rq.catch((error) => this.log('error', {in: 'base.getInternalCommunicationContext.notification.error', destination, message, metaParams, meta: getSerializableMeta(meta), error}));
rq.catch((error) => this.log('error', {in: 'base.getInternalCommunicationContext.notification.error', destination, message, metaParams, meta, error}));
} catch (e) {
this.log('error', {
in: 'base.getInternalCommunicationContext.request.response',
destination,
error: e,
message,
metaParams,
meta: getSerializableMeta(meta)
meta
});
throw e;
}
};
}

async remoteApiCall({destination, message, meta}) {
this.log('trace', {in: 'base.remoteApiCall', description: `try to call micro-service: ${destination}`, destination, message, meta: getSerializableMeta(meta)});
this.log('trace', {in: 'base.remoteApiCall', description: `try to call micro-service: ${destination}`, destination, message, meta});
throw errors.notImplemented();
}

// when someone hits api method
apiRequestReceived({message, meta}) {
this.log('info', {in: 'base.apiRequestReceived', count: 1, message, meta: getSerializableMeta(meta)});
this.log('info', {in: 'base.apiRequestReceived', count: 1, message, meta});
return new Promise(async(resolve, reject) => {
// create meta
const m = Object.assign({}, meta, {resolve, reject, apiRequestId: this.getApiRequestId()});
Expand All @@ -161,9 +159,9 @@ class Brid {
try {
// find api request method (method.in)
let apiMethodFn = this.findApiMethod({method: meta.method, direction: 'in'});
this.log('trace', {in: 'base.apiRequestReceived', count: 2, description: 'api "in" method found', message, meta: getSerializableMeta(m), pack});
this.log('trace', {in: 'base.apiRequestReceived', count: 2, description: 'api "in" method found', message, meta: m});
if (meta.isNotification) { // respond if notification, don't wait for anything to be executed
this.log('trace', {in: 'base.apiRequestReceived', count: 3, description: 'api "in" method found, but notification', message, meta: getSerializableMeta(m), pack});
this.log('trace', {in: 'base.apiRequestReceived', count: 3, description: 'api "in" method found, but notification', message, meta: m});
this.apiResponseReceived({result: {}, meta: m});
}
// call found method
Expand All @@ -174,26 +172,26 @@ class Brid {
}
// send it to external only if result from api method call is full
if (apiMethodFnResult) {
this.log('trace', {in: 'base.apiRequestReceived > send to external', count: 4, message, meta: getSerializableMeta(m), pack, apiMethodFnResult});
this.log('trace', {in: 'base.apiRequestReceived > send to external', count: 4, message, meta: m, apiMethodFnResult});
let extOutResP = this.externalOut({result: apiMethodFnResult, meta: m});
// start timer if request is not notification
if (!meta.isNotification && m && (m.apiRequestId || m.apiRequestId >= 0) && this.isApiRequest(m.apiRequestId) >= 0) {
this.log('trace', {in: 'base.apiRequestReceived > timeout started', count: 5, message, meta: getSerializableMeta(m), pack, apiMethodFnResult, extOutResP});
this.log('trace', {in: 'base.apiRequestReceived > timeout started', count: 5, message, meta: m, apiMethodFnResult, extOutResP});
// timeout external, do timeout if it is notification, timeout means, that request to external didn't receive response that marks external call as finished
// for call to bi finished it should go trough apiResponseReceived
var timeoutId = setTimeout(() => {
this.log('info', {in: 'base.apiRequestReceived > method timeout', count: 6, message, meta: getSerializableMeta(m), pack, apiMethodFnResult, extOutResP});
this.log('info', {in: 'base.apiRequestReceived > method timeout', count: 6, message, meta: m, apiMethodFnResult, extOutResP});
return this.apiResponseReceived({error: errors.methodTimedOut(m), meta: m});
}, meta.timeout || 30000);
m.timeoutId = timeoutId;
return await extOutResP;
}
return {};
}
this.log('info', {in: 'base.apiRequestReceived > skip external, result from api fn call is false', count: 7, message, meta: getSerializableMeta(m), pack, apiMethodFnResult});
this.log('info', {in: 'base.apiRequestReceived > skip external, result from api fn call is false', count: 7, message, meta: m, apiMethodFnResult});
return (!meta.isNotification && this.apiResponseReceived({result: apiMethodFnResult, meta: m}));
} catch (e) {
this.log('error', {in: 'base.apiRequestReceived', count: 8, message, meta: getSerializableMeta(m), pack, error: e});
this.log('error', {in: 'base.apiRequestReceived', count: 8, message, meta: m, error: e});
return this.apiResponseReceived({error: e, meta: m});
}
});
Expand Down Expand Up @@ -227,7 +225,7 @@ class Brid {
}
// when someone hits external
async externalIn({result, error, meta = {}} = {}) {
this.log('info', {in: 'base.externalIn', result, error, meta: getSerializableMeta(meta)});
this.log('info', {in: 'base.externalIn', result, error, meta});
var {method, apiRequestId, isNotification} = meta;
const message = (result && {result}) || (error && {error}) || undefined;
// find if there is api request pending by id
Expand All @@ -238,50 +236,50 @@ class Brid {
let fnExt = this.findExternalMethod({method});
// call external method
let fnExtResult = await fnExt(this.getInternalCommunicationContext(Object.assign({direction: 'in'}, meta)), message, Object.assign({}, meta));
this.log('trace', {in: 'base.externalIn', description: 'external request is response of api request', result, error, meta: getSerializableMeta(meta), fnExtResult});
this.log('trace', {in: 'base.externalIn', description: 'external request is response of api request', result, error, meta, fnExtResult});
// call api response
return this.apiResponseReceived({result: fnExtResult, meta});
} catch (e) {
this.log('error', {in: 'base.externalIn:userDefinedTransformation', error: e, meta: getSerializableMeta(meta)});
this.log('error', {in: 'base.externalIn:userDefinedTransformation', error: e, meta});
// call api response with error
return this.apiResponseReceived({error: e, meta});
}
// there is apiRequestId but no info in api queue, this means that message is probably timeouts
} else if (!isNotification) {
this.log('warn', {in: 'base.externalIn', errorMessage: 'message timed out', result, error, meta: getSerializableMeta(meta)});
this.log('warn', {in: 'base.externalIn', errorMessage: 'message timed out', result, error, meta});
return {...message, meta: Object.assign({deadIn: 1}, meta)};
}
return {...message, meta: Object.assign({deadIn: 1}, meta)};
} else {
var apiRequestIdByMatchKey = this.isApiRequestByMatchKey(message); // try to match api key by matchKeys
if (apiRequestIdByMatchKey > 0) {
this.log('trace', {in: 'base.externalIn', description: 'external request is response of api request, matched by "apiRequestIdByMatchKey"', result, error, meta: getSerializableMeta(meta)});
this.log('trace', {in: 'base.externalIn', description: 'external request is response of api request, matched by "apiRequestIdByMatchKey"', result, error, meta});
return this.externalIn({result, error, meta: Object.assign({}, meta, {apiRequestId: apiRequestIdByMatchKey})});
} else if (method) { // try to execute method and return it to caller
this.log('trace', {in: 'base.externalIn', description: 'external request is NOT response of api request', result, error, meta: getSerializableMeta(meta)});
this.log('trace', {in: 'base.externalIn', description: 'external request is NOT response of api request', result, error, meta});
try {
let fnExt = this.findExternalMethod({method});
let transformedMsg = await fnExt(this.getInternalCommunicationContext(meta), message, Object.assign({}, meta));
if (transformedMsg) { // respond to external if result from transform function is not false.
this.log('info', {in: 'base.externalIn', description: 'external request is NOT response of api request > send response to external', result, error, meta: getSerializableMeta(meta)});
this.log('info', {in: 'base.externalIn', description: 'external request is NOT response of api request > send response to external', result, error, meta});
return this.externalOut({result: transformedMsg, meta});
}
this.log('info', {in: 'base.externalIn', description: 'external request is NOT response of api request > don\'t send response to external because user transform function returned false', result, error, meta: getSerializableMeta(meta), transformedMsg});
this.log('info', {in: 'base.externalIn', description: 'external request is NOT response of api request > don\'t send response to external because user transform function returned false', result, error, meta, transformedMsg});
return {...message, meta: Object.assign({deadIn: 1}, meta)};
} catch (e) {
this.log('error', {in: 'base.externalIn', error: e, meta: getSerializableMeta(meta)});
this.log('error', {in: 'base.externalIn', error: e, meta});
return this.externalOut({error: e, meta});
}
} else {
this.log('warn', {in: 'base.externalIn', errorMessage: 'message missing: apiRequestId, method', result, error, meta: getSerializableMeta(meta)});
this.log('warn', {in: 'base.externalIn', errorMessage: 'message missing: apiRequestId, method', result, error, meta});
return {...message, meta: {deadIn: 1}};
}
}
}

// when request or response goes to external
async externalOut({result, meta}) {
this.log('trace', {in: 'base.externalOut', result, meta: getSerializableMeta(meta)});
this.log('trace', {in: 'base.externalOut', result, meta});
return {result, meta};
}

Expand All @@ -292,6 +290,10 @@ class Brid {
console.log(level, '------------stop');
return {};
}

cleanMeta({timeoutId, ...metaClean}) {
return {meta: metaClean};
}
}

module.exports = Brid;

0 comments on commit c1fa64a

Please sign in to comment.