Skip to content

Commit

Permalink
fix: cleanup meta
Browse files Browse the repository at this point in the history
  • Loading branch information
zetxx committed Dec 17, 2019
1 parent 2f1cad8 commit 8278d53
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

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

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

registerMethod({method, channel, fn, meta}) {
this.log('info', {in: 'base.registerMethod', description: `register method: ${channel}.${method}`, method, channel, meta});
this.log('info', {in: 'base.registerMethod', description: `register method: ${channel}.${method}`, method, channel, meta: getSerializableMeta(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});
this.log('trace', {in: 'base.registerMethod', description: `exec method: ${channel}.${method}`, message, meta: getSerializableMeta(meta)});
return fn.call(ctx, ...args);
};
}

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

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

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

getInternalCommunicationContext(meta, extraContext = {}) {
this.log('debug', {in: 'base.getInternalCommunicationContext', meta});
this.log('debug', {in: 'base.getInternalCommunicationContext', meta: getSerializableMeta(meta)});
return {
sharedContext: extraContext || {},
request: async(destination, message, metaParams = {}) => {
this.log('trace', {in: 'base.getInternalCommunicationContext.request', destination, message, metaParams, meta});
this.log('trace', {in: 'base.getInternalCommunicationContext.request', destination, message, metaParams, meta: getSerializableMeta(meta)});
var globTraceId = this.getGlobTraceId(meta);
return this.remoteApiRequest({destination, message, meta: Object.assign({}, meta, {globTraceId}, metaParams, {isNotification: 0})});
},
notification: async(destination, message, metaParams = {}) => {
this.log('trace', {in: 'base.getInternalCommunicationContext.notification', destination, message, metaParams, meta});
this.log('trace', {in: 'base.getInternalCommunicationContext.notification', destination, message, metaParams, meta: getSerializableMeta(meta)});
var globTraceId = this.getGlobTraceId(meta);
this.remoteApiRequest({destination, message, meta: Object.assign({}, meta, {globTraceId}, metaParams, {isNotification: 1})})
.catch((error) => this.log('error', {
Expand All @@ -127,7 +129,7 @@ class Brid {
error,
message,
metaParams,
meta
meta: getSerializableMeta(meta)
}));
return {};
},
Expand All @@ -137,13 +139,13 @@ class Brid {
}

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

// when someone hits api method
apiRequestReceived({message, meta}) {
this.log('info', {in: 'base.apiRequestReceived', count: 1, message, meta});
this.log('info', {in: 'base.apiRequestReceived', count: 1, message, meta: getSerializableMeta(meta)});
return new Promise(async(resolve, reject) => {
// create meta
const m = Object.assign({}, meta, {resolve, reject, apiRequestId: this.getApiRequestId()});
Expand All @@ -153,9 +155,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: m, pack});
this.log('trace', {in: 'base.apiRequestReceived', count: 2, description: 'api "in" method found', message, meta: getSerializableMeta(m), pack});
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: m, pack});
this.log('trace', {in: 'base.apiRequestReceived', count: 3, description: 'api "in" method found, but notification', message, meta: getSerializableMeta(m), pack});
this.apiResponseReceived({result: {}, meta: m});
}
// call found method
Expand All @@ -166,26 +168,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: m, pack, apiMethodFnResult});
this.log('trace', {in: 'base.apiRequestReceived > send to external', count: 4, message, meta: getSerializableMeta(m), pack, 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: m, pack, apiMethodFnResult, extOutResP});
this.log('trace', {in: 'base.apiRequestReceived > timeout started', count: 5, message, meta: getSerializableMeta(m), pack, 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: m, pack, apiMethodFnResult, extOutResP});
this.log('info', {in: 'base.apiRequestReceived > method timeout', count: 6, message, meta: getSerializableMeta(m), pack, 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: m, pack, apiMethodFnResult});
this.log('info', {in: 'base.apiRequestReceived > skip external, result from api fn call is false', count: 7, message, meta: getSerializableMeta(m), pack, apiMethodFnResult});
return (!meta.isNotification && this.apiResponseReceived({result: apiMethodFnResult, meta: m}));
} catch (e) {
this.log('error', {in: 'base.apiRequestReceived', count: 8, message, meta: m, pack, error: e});
this.log('error', {in: 'base.apiRequestReceived', count: 8, message, meta: getSerializableMeta(m), pack, error: e});
return this.apiResponseReceived({error: e, meta: m});
}
});
Expand Down Expand Up @@ -219,7 +221,7 @@ class Brid {
}
// when someone hits external
async externalIn({result, error, meta = {}} = {}) {
this.log('info', {in: 'base.externalIn', result, error, meta});
this.log('info', {in: 'base.externalIn', result, error, meta: getSerializableMeta(meta)});
var {method, apiRequestId, isNotification} = meta;
const message = (result && {result}) || (error && {error}) || undefined;
// find if there is api request pending by id
Expand All @@ -230,50 +232,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, fnExtResult});
this.log('trace', {in: 'base.externalIn', description: 'external request is response of api request', result, error, meta: getSerializableMeta(meta), fnExtResult});
// call api response
return this.apiResponseReceived({result: fnExtResult, meta});
} catch (e) {
this.log('error', {in: 'base.externalIn:userDefinedTransformation', result, error, meta});
this.log('error', {in: 'base.externalIn:userDefinedTransformation', result, error, meta: getSerializableMeta(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});
this.log('warn', {in: 'base.externalIn', errorMessage: 'message timed out', result, error, meta: getSerializableMeta(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});
this.log('trace', {in: 'base.externalIn', description: 'external request is response of api request, matched by "apiRequestIdByMatchKey"', result, error, meta: getSerializableMeta(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});
this.log('trace', {in: 'base.externalIn', description: 'external request is NOT response of api request', result, error, meta: getSerializableMeta(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});
this.log('info', {in: 'base.externalIn', description: 'external request is NOT response of api request > send response to external', result, error, meta: getSerializableMeta(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, 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: getSerializableMeta(meta), transformedMsg});
return {...message, meta: Object.assign({deadIn: 1}, meta)};
} catch (e) {
this.log('error', {in: 'base.externalIn', result, error, meta});
this.log('error', {in: 'base.externalIn', result, error, meta: getSerializableMeta(meta)});
return this.externalOut({error: e, meta});
}
} else {
this.log('warn', {in: 'base.externalIn', errorMessage: 'message missing: apiRequestId, method', result, error, meta});
this.log('warn', {in: 'base.externalIn', errorMessage: 'message missing: apiRequestId, method', result, error, meta: getSerializableMeta(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});
this.log('trace', {in: 'base.externalOut', result, meta: getSerializableMeta(meta)});
return {result, meta};
}

Expand Down

0 comments on commit 8278d53

Please sign in to comment.