UNPKG

@aws-lambda-powertools/batch

Version:

The batch processing package for the Powertools for AWS Lambda (TypeScript) library.

72 lines (71 loc) 2.74 kB
import '@aws/lambda-invoke-store'; import { shouldUseInvokeStore } from '@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 (!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 (!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 (!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 (!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()); } } export { SqsFifoProcessorStore };