@aws-lambda-powertools/batch
Version:
The batch processing package for the Powertools for AWS Lambda (TypeScript) library.
79 lines (78 loc) • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqsFifoProcessor = void 0;
const SqsFifoProcessorStore_js_1 = require("./SqsFifoProcessorStore.js");
/**
* Class representing a processor for SQS FIFO messages.
* This class provides utilities for handling message groups, including tracking failed groups,
* determining whether to short-circuit processing, and skipping groups based on processing options.
*/
class SqsFifoProcessor {
#store = new SqsFifoProcessorStore_js_1.SqsFifoProcessorStore();
get currentGroupId() {
return this.#store.getCurrentGroupId();
}
set currentGroupId(groupId) {
this.#store.setCurrentGroupId(groupId);
}
/**
* Adds the specified group ID to the set of failed group IDs.
*
* @param group - The group ID to be added to the set of failed group IDs.
*/
addToFailedGroup(group) {
this.#store.addFailedGroupId(group);
}
/**
* Prepares the processor for a new batch of messages.
*/
prepare() {
this.currentGroupId = undefined;
this.#store.clearFailedGroupIds();
}
/**
* Sets the current group ID for the message being processed.
*
* @param group - The group ID of the current message being processed.
*/
setCurrentGroup(group) {
this.currentGroupId = group;
}
/**
* Determines whether the current group should be short-circuited.
*
* If we have any failed messages, we should then short circuit the process and
* fail remaining messages unless `skipGroupOnError` is true
*
* @param failureMessages - The list of failure messages.
* @param options - The options for the batch processing.
*/
shouldShortCircuit(failureMessages, options) {
return !options?.skipGroupOnError && failureMessages.length !== 0;
}
/**
* Determines whether the current group should be skipped.
*
* If `skipGroupOnError` is true and the current group has previously failed,
* then we should skip processing the current group.
*
* @param options - The options for the batch processing.
*/
shouldSkipCurrentGroup(options) {
return ((options?.skipGroupOnError ?? false) &&
this.currentGroupId !== undefined &&
this.#store.hasFailedGroupId(this.currentGroupId));
}
/**
* Handles failure for current group
* Adds the current group ID to the set of failed group IDs if `skipGroupOnError` is true.
*
* @param options - The options for the batch processing.
*/
processFailureForCurrentGroup(options) {
if (options?.skipGroupOnError && this.currentGroupId) {
this.addToFailedGroup(this.currentGroupId);
}
}
}
exports.SqsFifoProcessor = SqsFifoProcessor;