|
| 1 | +import type { SQSRecord } from 'aws-lambda'; |
| 2 | +import { BatchProcessor } from './BatchProcessor.js'; |
| 3 | +import { SqsFifoProcessor } from './SqsFifoProcessor.js'; |
| 4 | +import { EventType } from './constants.js'; |
| 5 | +import { |
| 6 | + type BatchProcessingError, |
| 7 | + SqsFifoMessageGroupShortCircuitError, |
| 8 | + SqsFifoShortCircuitError, |
| 9 | +} from './errors.js'; |
| 10 | +import type { |
| 11 | + BaseRecord, |
| 12 | + EventSourceDataClassTypes, |
| 13 | + FailureResponse, |
| 14 | + SuccessResponse, |
| 15 | +} from './types.js'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Batch processor for SQS FIFO queues |
| 19 | + * |
| 20 | + * This class extends the {@link BatchProcessor} class and provides |
| 21 | + * a mechanism to process records from SQS FIFO queues asynchronously. |
| 22 | + * |
| 23 | + * By default, we will stop processing at the first failure and mark unprocessed messages as failed to preserve ordering. |
| 24 | + * |
| 25 | + * However, this behavior may not be optimal for customers who wish to proceed with processing messages from a different group ID. |
| 26 | + * |
| 27 | + * @example |
| 28 | + * ```typescript |
| 29 | + * import { |
| 30 | + * BatchProcessor, |
| 31 | + * SqsFifoPartialProcessorAsync, |
| 32 | + * processPartialResponse, |
| 33 | + * } from '@aws-lambda-powertools/batch'; |
| 34 | + * import type { SQSRecord, SQSHandler } from 'aws-lambda'; |
| 35 | + * |
| 36 | + * const processor = new SqsFifoPartialProcessorAsync(); |
| 37 | + * |
| 38 | + * const recordHandler = async (record: SQSRecord): Promise<void> => { |
| 39 | + * const payload = JSON.parse(record.body); |
| 40 | + * }; |
| 41 | + * |
| 42 | + * export const handler: SQSHandler = async (event, context) => |
| 43 | + * processPartialResponse(event, recordHandler, processor, { |
| 44 | + * context, |
| 45 | + * }); |
| 46 | + * ``` |
| 47 | + */ |
| 48 | +class SqsFifoPartialProcessorAsync extends BatchProcessor { |
| 49 | + /** |
| 50 | + * Processor for handling SQS FIFO message |
| 51 | + */ |
| 52 | + readonly #processor: SqsFifoProcessor; |
| 53 | + |
| 54 | + public constructor() { |
| 55 | + super(EventType.SQS); |
| 56 | + this.#processor = new SqsFifoProcessor(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Handles a failure for a given record. |
| 61 | + * |
| 62 | + * @param record - The record that failed. |
| 63 | + * @param exception - The error that occurred. |
| 64 | + */ |
| 65 | + public failureHandler( |
| 66 | + record: EventSourceDataClassTypes, |
| 67 | + exception: Error |
| 68 | + ): FailureResponse { |
| 69 | + this.#processor.processFailureForCurrentGroup(this.options); |
| 70 | + |
| 71 | + return super.failureHandler(record, exception); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Process a record with a asynchronous handler |
| 76 | + * |
| 77 | + * This method orchestrates the processing of a batch of records asynchronously |
| 78 | + * for SQS FIFO queues. |
| 79 | + * |
| 80 | + * The method calls the prepare hook to initialize the processor and then |
| 81 | + * iterates over each record in the batch, processing them one by one. |
| 82 | + * |
| 83 | + * If one of them fails and `skipGroupOnError` is not true, the method short circuits |
| 84 | + * the processing and fails the remaining records in the batch. |
| 85 | + * |
| 86 | + * If one of them fails and `skipGroupOnError` is true, then the method fails the current record |
| 87 | + * if the message group has any previous failure, otherwise keeps processing. |
| 88 | + * |
| 89 | + * Then, it calls the clean hook to clean up the processor and returns the |
| 90 | + * processed records. |
| 91 | + */ |
| 92 | + public async process(): Promise<(SuccessResponse | FailureResponse)[]> { |
| 93 | + this.prepare(); |
| 94 | + |
| 95 | + const processedRecords: (SuccessResponse | FailureResponse)[] = []; |
| 96 | + let currentIndex = 0; |
| 97 | + for (const record of this.records) { |
| 98 | + this.#processor.setCurrentGroup( |
| 99 | + (record as SQSRecord).attributes?.MessageGroupId |
| 100 | + ); |
| 101 | + |
| 102 | + if ( |
| 103 | + this.#processor.shouldShortCircuit(this.failureMessages, this.options) |
| 104 | + ) { |
| 105 | + return this.shortCircuitProcessing(currentIndex, processedRecords); |
| 106 | + } |
| 107 | + |
| 108 | + const result = this.#processor.shouldSkipCurrentGroup(this.options) |
| 109 | + ? this.#processFailRecord( |
| 110 | + record, |
| 111 | + new SqsFifoMessageGroupShortCircuitError() |
| 112 | + ) |
| 113 | + : await this.processRecord(record); |
| 114 | + |
| 115 | + processedRecords.push(result); |
| 116 | + currentIndex++; |
| 117 | + } |
| 118 | + |
| 119 | + this.clean(); |
| 120 | + |
| 121 | + return processedRecords; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Starting from the first failure index, fail all remaining messages regardless |
| 126 | + * of their group ID. |
| 127 | + * |
| 128 | + * This short circuit mechanism is used when we detect a failed message in the batch. |
| 129 | + * |
| 130 | + * Since messages in a FIFO queue are processed in order, we must stop processing any |
| 131 | + * remaining messages in the batch to prevent out-of-order processing. |
| 132 | + * |
| 133 | + * @param firstFailureIndex Index of first message that failed |
| 134 | + * @param processedRecords Array of response items that have been processed both successfully and unsuccessfully |
| 135 | + */ |
| 136 | + protected shortCircuitProcessing( |
| 137 | + firstFailureIndex: number, |
| 138 | + processedRecords: (SuccessResponse | FailureResponse)[] |
| 139 | + ): (SuccessResponse | FailureResponse)[] { |
| 140 | + const remainingRecords = this.records.slice(firstFailureIndex); |
| 141 | + |
| 142 | + for (const record of remainingRecords) { |
| 143 | + this.#processFailRecord(record, new SqsFifoShortCircuitError()); |
| 144 | + } |
| 145 | + |
| 146 | + this.clean(); |
| 147 | + |
| 148 | + return processedRecords; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Processes a fail record. |
| 153 | + * |
| 154 | + * @param record - The record that failed. |
| 155 | + * @param exception - The error that occurred. |
| 156 | + */ |
| 157 | + #processFailRecord( |
| 158 | + record: BaseRecord, |
| 159 | + exception: BatchProcessingError |
| 160 | + ): FailureResponse { |
| 161 | + const data = this.toBatchType(record, this.eventType); |
| 162 | + |
| 163 | + return this.failureHandler(data, exception); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +export { SqsFifoPartialProcessorAsync }; |
0 commit comments