UNPKG

@aws-lambda-powertools/batch

Version:

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

78 lines 3.26 kB
import { BatchProcessor } from './BatchProcessor.js'; import type { EventSourceDataClassTypes, FailureResponse, SuccessResponse } from './types.js'; /** * Batch processor for SQS FIFO queues * * This class extends the {@link BatchProcessor} class and provides * a mechanism to process records from SQS FIFO queues asynchronously. * * By default, we will stop processing at the first failure and mark unprocessed messages as failed to preserve ordering. * * However, this behavior may not be optimal for customers who wish to proceed with processing messages from a different group ID. * * @example * ```typescript * import { * BatchProcessor, * SqsFifoPartialProcessorAsync, * processPartialResponse, * } from '@aws-lambda-powertools/batch'; * import type { SQSRecord, SQSHandler } from 'aws-lambda'; * * const processor = new SqsFifoPartialProcessorAsync(); * * const recordHandler = async (record: SQSRecord): Promise<void> => { * const payload = JSON.parse(record.body); * }; * * export const handler: SQSHandler = async (event, context) => * processPartialResponse(event, recordHandler, processor, { * context, * }); * ``` */ declare class SqsFifoPartialProcessorAsync extends BatchProcessor { #private; constructor(); /** * Handles a failure for a given record. * * @param record - The record that failed. * @param exception - The error that occurred. */ failureHandler(record: EventSourceDataClassTypes, exception: Error): FailureResponse; /** * Process a record with a asynchronous handler * * This method orchestrates the processing of a batch of records asynchronously * for SQS FIFO queues. * * The method calls the prepare hook to initialize the processor and then * iterates over each record in the batch, processing them one by one. * * If one of them fails and `skipGroupOnError` is not true, the method short circuits * the processing and fails the remaining records in the batch. * * If one of them fails and `skipGroupOnError` is true, then the method fails the current record * if the message group has any previous failure, otherwise keeps processing. * * Then, it calls the clean hook to clean up the processor and returns the * processed records. */ process(): Promise<(SuccessResponse | FailureResponse)[]>; /** * Starting from the first failure index, fail all remaining messages regardless * of their group ID. * * This short circuit mechanism is used when we detect a failed message in the batch. * * Since messages in a FIFO queue are processed in order, we must stop processing any * remaining messages in the batch to prevent out-of-order processing. * * @param firstFailureIndex Index of first message that failed * @param processedRecords Array of response items that have been processed both successfully and unsuccessfully */ protected shortCircuitProcessing(firstFailureIndex: number, processedRecords: (SuccessResponse | FailureResponse)[]): (SuccessResponse | FailureResponse)[]; } export { SqsFifoPartialProcessorAsync }; //# sourceMappingURL=SqsFifoPartialProcessorAsync.d.ts.map