@aws-lambda-powertools/batch
Version:
The batch processing package for the Powertools for AWS Lambda (TypeScript) library.
75 lines (74 loc) • 2.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqsFifoProcessorStore = void 0;
require("@aws/lambda-invoke-store");
const env_1 = require("@aws-lambda-powertools/commons/utils/env");
/**
* Manages storage of SQS FIFO processor state with automatic context detection.
*
* This class abstracts the storage mechanism for SQS FIFO processing state,
* automatically choosing between InvokeStore (when in Lambda context) and
* fallback instance variables (when outside Lambda context). The decision is
* made at runtime on every method call to support Lambda's concurrent execution
* isolation.
*/
class SqsFifoProcessorStore {
#currentGroupIdKey = Symbol('powertools.batch.sqsFifo.currentGroupId');
#failedGroupIdsKey = Symbol('powertools.batch.sqsFifo.failedGroupIds');
#fallbackCurrentGroupId;
#fallbackFailedGroupIds = new Set();
getCurrentGroupId() {
if (!(0, env_1.shouldUseInvokeStore)()) {
return this.#fallbackCurrentGroupId;
}
if (globalThis.awslambda?.InvokeStore === undefined) {
throw new Error('InvokeStore is not available');
}
const store = globalThis.awslambda.InvokeStore;
return store.get(this.#currentGroupIdKey);
}
setCurrentGroupId(groupId) {
if (!(0, env_1.shouldUseInvokeStore)()) {
this.#fallbackCurrentGroupId = groupId;
return;
}
if (globalThis.awslambda?.InvokeStore === undefined) {
throw new Error('InvokeStore is not available');
}
const store = globalThis.awslambda.InvokeStore;
store.set(this.#currentGroupIdKey, groupId);
}
addFailedGroupId(groupId) {
this.getFailedGroupIds().add(groupId);
}
hasFailedGroupId(groupId) {
return this.getFailedGroupIds().has(groupId);
}
getFailedGroupIds() {
if (!(0, env_1.shouldUseInvokeStore)()) {
return this.#fallbackFailedGroupIds;
}
if (globalThis.awslambda?.InvokeStore === undefined) {
throw new Error('InvokeStore is not available');
}
const store = globalThis.awslambda.InvokeStore;
let failedGroupIds = store.get(this.#failedGroupIdsKey);
if (failedGroupIds == null) {
failedGroupIds = new Set();
store.set(this.#failedGroupIdsKey, failedGroupIds);
}
return failedGroupIds;
}
clearFailedGroupIds() {
if (!(0, env_1.shouldUseInvokeStore)()) {
this.#fallbackFailedGroupIds = new Set();
return;
}
if (globalThis.awslambda?.InvokeStore === undefined) {
throw new Error('InvokeStore is not available');
}
const store = globalThis.awslambda.InvokeStore;
store.set(this.#failedGroupIdsKey, new Set());
}
}
exports.SqsFifoProcessorStore = SqsFifoProcessorStore;