@aws-lambda-powertools/batch
Version:
The batch processing package for the Powertools for AWS Lambda (TypeScript) library.
76 lines (75 loc) • 2.68 kB
JavaScript
import { SqsFifoProcessorStore } from './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();
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);
}
}
}
export { SqsFifoProcessor };