@godspeedsystems/core
Version:
> 4th Generation Declarative Microservice Framework
187 lines (186 loc) • 7.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
checkCachingDs: function() {
return checkCachingDs;
},
evaluateCachingInstAndInvalidates: function() {
return evaluateCachingInstAndInvalidates;
},
fetchFromCache: function() {
return fetchFromCache;
},
setInCache: function() {
return setInCache;
}
});
const _scriptRuntime = /*#__PURE__*/ _interop_require_default(require("./scriptRuntime"));
const _logger = require("../logger");
const _config = /*#__PURE__*/ _interop_require_default(require("config"));
const _expandVariables = /*#__PURE__*/ _interop_require_default(require("./expandVariables"));
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _async_to_generator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _object_spread(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_define_property(target, key, source[key]);
});
}
return target;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function(sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _object_spread_props(target, source) {
source = source != null ? source : {};
if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function(key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function checkCachingDs(caching, location) {
//@ts-ignore
const datasources = global.datasources;
const cachingDsName = (caching === null || caching === void 0 ? void 0 : caching.datasource) || _config.default.caching;
const evaluatedCachingDsName = (0, _expandVariables.default)(cachingDsName, location);
if (!evaluatedCachingDsName) {
_logger.logger.fatal(location, 'Exiting. Set a non null caching datasource in config/default or in the caching instruction itself %o', caching);
process.exit(1);
}
const cachingDs = datasources[evaluatedCachingDsName];
if (!cachingDs) {
_logger.logger.fatal(location, 'Exiting. Could not find a valid datasource by the name %s in the caching instruction %o', cachingDsName, caching);
process.exit(1);
}
}
function evaluateCachingInstAndInvalidates(ctx, caching, taskValue) {
return _evaluateCachingInstAndInvalidates.apply(this, arguments);
}
function _evaluateCachingInstAndInvalidates() {
_evaluateCachingInstAndInvalidates = _async_to_generator(function*(ctx, caching, taskValue) {
let cachingInstruction = null;
cachingInstruction = yield (0, _scriptRuntime.default)(ctx, caching, taskValue);
if (!cachingInstruction) {
ctx.childLogger.error('Error in evaluating cachingInstruction %o', caching);
throw new Error('Error in evaluating caching script');
}
const cachingDsName = (cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.datasource) || _config.default.caching;
const cachingDs = ctx.datasources[cachingDsName];
if (cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.invalidate) {
ctx.childLogger.debug('Invalidating cache for key %s', cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.invalidate);
yield cachingDs.del(cachingInstruction.invalidate);
}
return _object_spread_props(_object_spread({}, cachingInstruction), {
"cachingDs": cachingDs
});
});
return _evaluateCachingInstAndInvalidates.apply(this, arguments);
}
function fetchFromCache(cachingInstruction) {
return _fetchFromCache.apply(this, arguments);
}
function _fetchFromCache() {
_fetchFromCache = _async_to_generator(function*(cachingInstruction) {
let status;
const cachingDs = cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.cachingDs;
if (cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.key) {
// check in cache and return
status = yield cachingDs.get(cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.key);
}
return status;
});
return _fetchFromCache.apply(this, arguments);
}
function setInCache(ctx, cachingInstruction, status) {
return _setInCache.apply(this, arguments);
}
function _setInCache() {
_setInCache = _async_to_generator(function*(ctx, cachingInstruction, status) {
const cachingDs = cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.cachingDs;
if (cachingInstruction === null || cachingInstruction === void 0 ? void 0 : cachingInstruction.key) {
if ((status === null || status === void 0 ? void 0 : status.success) || cachingInstruction.cache_on_failure) {
if (!(status === null || status === void 0 ? void 0 : status.success)) {
ctx.childLogger.debug('Storing failure task result in cache for %s and value %o', cachingInstruction.key, status);
} else {
ctx.childLogger.debug('Storing task result in cache for %s and value %o', cachingInstruction.key, status);
}
yield cachingDs.set(cachingInstruction.key, JSON.stringify(status), cachingInstruction.options); //{ EX: cachingInstruction.expires });
}
}
});
return _setInCache.apply(this, arguments);
}