UNPKG

@aws-lambda-powertools/batch

Version:

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

82 lines (81 loc) 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SqsFifoProcessor = void 0; /** * 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 { /** * The ID of the current message group being processed. */ #currentGroupId; /** * A set of group IDs that have already encountered failures. */ #failedGroupIds; constructor() { this.#failedGroupIds = new Set(); } /** * 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.#failedGroupIds.add(group); } /** * Prepares the processor for a new batch of messages. */ prepare() { this.#currentGroupId = undefined; this.#failedGroupIds.clear(); } /** * 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 && this.#failedGroupIds.has(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;