UNPKG

@bitblit/epsilon

Version:

Tiny adapter to simplify building API gateway Lambda APIS

53 lines 3.02 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.RetryProcessor = void 0; const common_1 = require("@bitblit/ratchet/common"); const string_ratchet_1 = require("@bitblit/ratchet/common/string-ratchet"); const number_ratchet_1 = require("@bitblit/ratchet/common/number-ratchet"); class RetryProcessor { constructor(delegate, opts) { this.delegate = delegate; this.opts = opts; } get typeName() { var _a, _b; return string_ratchet_1.StringRatchet.trimToEmpty((_a = this.opts) === null || _a === void 0 ? void 0 : _a.typePrefix) + this.delegate.typeName + string_ratchet_1.StringRatchet.trimToEmpty((_b = this.opts) === null || _b === void 0 ? void 0 : _b.typeSuffix); } handleEvent(data, mgr) { return __awaiter(this, void 0, void 0, function* () { const tryNumber = data && data[RetryProcessor.RETRY_FIELD_NAME] ? number_ratchet_1.NumberRatchet.safeNumber(data[RetryProcessor.RETRY_FIELD_NAME]) : 1; const dataCopy = data ? Object.assign({}, data) : null; delete dataCopy[RetryProcessor.RETRY_FIELD_NAME]; common_1.Logger.info('RetryProcessor : %s : Try %d of %d', this.delegate.typeName, tryNumber, this.opts.retryCount); try { yield this.delegate.handleEvent(dataCopy, mgr); } catch (err) { common_1.Logger.error('Failed to process : %s', err, err); if (tryNumber < this.opts.retryCount) { const waitTimeMS = tryNumber * this.opts.baseDelayMS; common_1.Logger.info('Firing automatic retry after a wait of %s', common_1.DurationRatchet.formatMsDuration(waitTimeMS)); yield common_1.PromiseRatchet.wait(waitTimeMS); const wrapped = dataCopy || {}; wrapped[RetryProcessor.RETRY_FIELD_NAME] = tryNumber + 1; yield mgr.fireImmediateProcessRequestByParts(this.typeName, wrapped); } else { common_1.Logger.error('That was the last try - giving up'); } } }); } } exports.RetryProcessor = RetryProcessor; RetryProcessor.RETRY_FIELD_NAME = '___RetryProcessorTryNumber'; //# sourceMappingURL=retry-processor.js.map