UNPKG

@bitblit/epsilon

Version:

Tiny adapter to simplify building API gateway Lambda APIS

116 lines 5.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractBackgroundManager = void 0; const common_1 = require("@bitblit/ratchet/common"); const luxon_1 = require("luxon"); const context_util_1 = require("../../util/context-util"); /** * Handles all submission of work to the background processing system. * * Note that this does NOT validate the input, it just passes it along. This is * because it creates a circular reference to the processors if we try since they * define the type and validation. */ class AbstractBackgroundManager { createEntry(type, data) { const rval = { type: type, data: data, }; return rval; } wrapEntryForInternal(entry, overrideTraceId, overrideTraceDepth) { return __awaiter(this, void 0, void 0, function* () { const rval = Object.assign({}, entry, { createdEpochMS: new Date().getTime(), guid: AbstractBackgroundManager.generateBackgroundGuid(), traceId: overrideTraceId || context_util_1.ContextUtil.currentTraceId(), traceDepth: overrideTraceDepth || context_util_1.ContextUtil.currentTraceDepth() + 1, // || no 0 allowed either }); return rval; }); } addEntryToQueueByParts(type, data, fireStartMessage) { return __awaiter(this, void 0, void 0, function* () { let rval = null; const entry = this.createEntry(type, data); if (entry) { rval = yield this.addEntryToQueue(entry, fireStartMessage); } return rval; }); } addEntriesToQueue(entries, fireStartMessage) { return __awaiter(this, void 0, void 0, function* () { const rval = []; for (let i = 0; i < entries.length; i++) { try { // Always defer the fire to after the last enqueue const tmp = yield this.addEntryToQueue(entries[i], false); rval.push(tmp); } catch (err) { common_1.Logger.error('Error processing %j : %s', entries[i], err); rval.push(err['message']); } if (fireStartMessage) { const fireResult = yield this.fireStartProcessingRequest(); common_1.Logger.silly('FireResult : %s', fireResult); } } return rval; }); } fireImmediateProcessRequestByParts(type, data) { return __awaiter(this, void 0, void 0, function* () { let rval = null; const entry = this.createEntry(type, data); if (entry) { rval = yield this.fireImmediateProcessRequest(entry); } return rval; }); } static generateBackgroundGuid(targetEpochMS = new Date().getTime()) { const dt = luxon_1.DateTime.fromMillis(targetEpochMS); return dt.toFormat('yyyy-MM-dd-HH-mm-ss-') + common_1.StringRatchet.createType4Guid(); } static backgroundGuidToPath(prefix, guid) { let path = common_1.StringRatchet.trimToEmpty(prefix); if (path.length && !path.endsWith('/')) { path += '/'; } path += guid.substring(0, 4) + '/' + guid.substring(5, 7) + '/' + guid.substring(8, 10) + '/'; path += guid + '.json'; return path; } static pathToBackgroundGuid(prefix, path) { common_1.RequireRatchet.notNullOrUndefined(path, 'path'); let start = 0; if (!path.endsWith('.json')) { common_1.ErrorRatchet.throwFormattedErr('Cannot extract guid, does not end with .json : %s : %s', path, prefix); } if (common_1.StringRatchet.trimToNull(prefix)) { if (!path.startsWith(prefix)) { common_1.ErrorRatchet.throwFormattedErr('Cannot extract guid, does not start with prefix : %s : %s', path, prefix); } start = prefix.length; if (!prefix.endsWith('/')) { start++; } } start += 11; return path.substring(start, path.length - 5); // strip prefix and .json at the end } } exports.AbstractBackgroundManager = AbstractBackgroundManager; //# sourceMappingURL=abstract-background-manager.js.map