|
| 1 | +from typing import List, Optional, Tuple |
| 2 | + |
| 3 | +from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType |
| 4 | +from aws_lambda_powertools.utilities.batch.types import BatchSqsTypeModel |
| 5 | + |
| 6 | + |
| 7 | +class SQSFifoCircuitBreakerError(Exception): |
| 8 | + """ |
| 9 | + Signals a record not processed due to the SQS FIFO processing being interrupted |
| 10 | + """ |
| 11 | + |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class SqsFifoPartialProcessor(BatchProcessor): |
| 16 | + """Process native partial responses from SQS FIFO queues. |
| 17 | +
|
| 18 | + Stops processing records when the first record fails. The remaining records are reported as failed items. |
| 19 | +
|
| 20 | + Example |
| 21 | + _______ |
| 22 | +
|
| 23 | + ## Process batch triggered by a FIFO SQS |
| 24 | +
|
| 25 | + ```python |
| 26 | + import json |
| 27 | +
|
| 28 | + from aws_lambda_powertools import Logger, Tracer |
| 29 | + from aws_lambda_powertools.utilities.batch import SqsFifoPartialProcessor, EventType, batch_processor |
| 30 | + from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord |
| 31 | + from aws_lambda_powertools.utilities.typing import LambdaContext |
| 32 | +
|
| 33 | +
|
| 34 | + processor = SqsFifoPartialProcessor() |
| 35 | + tracer = Tracer() |
| 36 | + logger = Logger() |
| 37 | +
|
| 38 | +
|
| 39 | + @tracer.capture_method |
| 40 | + def record_handler(record: SQSRecord): |
| 41 | + payload: str = record.body |
| 42 | + if payload: |
| 43 | + item: dict = json.loads(payload) |
| 44 | + ... |
| 45 | +
|
| 46 | + @logger.inject_lambda_context |
| 47 | + @tracer.capture_lambda_handler |
| 48 | + @batch_processor(record_handler=record_handler, processor=processor) |
| 49 | + def lambda_handler(event, context: LambdaContext): |
| 50 | + return processor.response() |
| 51 | + ``` |
| 52 | + """ |
| 53 | + |
| 54 | + circuit_breaker_exc = ( |
| 55 | + SQSFifoCircuitBreakerError, |
| 56 | + SQSFifoCircuitBreakerError("A previous record failed processing"), |
| 57 | + None, |
| 58 | + ) |
| 59 | + |
| 60 | + def __init__(self, model: Optional["BatchSqsTypeModel"] = None): |
| 61 | + super().__init__(EventType.SQS, model) |
| 62 | + |
| 63 | + def process(self) -> List[Tuple]: |
| 64 | + """ |
| 65 | + Call instance's handler for each record. When the first failed message is detected, |
| 66 | + the process is short-circuited, and the remaining messages are reported as failed items. |
| 67 | + """ |
| 68 | + result: List[Tuple] = [] |
| 69 | + |
| 70 | + for i, record in enumerate(self.records): |
| 71 | + # If we have failed messages, it means that the last message failed. |
| 72 | + # We then short circuit the process, failing the remaining messages |
| 73 | + if self.fail_messages: |
| 74 | + return self._short_circuit_processing(i, result) |
| 75 | + |
| 76 | + # Otherwise, process the message normally |
| 77 | + result.append(self._process_record(record)) |
| 78 | + |
| 79 | + return result |
| 80 | + |
| 81 | + def _short_circuit_processing(self, first_failure_index: int, result: List[Tuple]) -> List[Tuple]: |
| 82 | + """ |
| 83 | + Starting from the first failure index, fail all the remaining messages, and append them to the result list. |
| 84 | + """ |
| 85 | + remaining_records = self.records[first_failure_index:] |
| 86 | + for remaining_record in remaining_records: |
| 87 | + data = self._to_batch_type(record=remaining_record, event_type=self.event_type, model=self.model) |
| 88 | + result.append(self.failure_handler(record=data, exception=self.circuit_breaker_exc)) |
| 89 | + return result |
| 90 | + |
| 91 | + async def _async_process_record(self, record: dict): |
| 92 | + raise NotImplementedError() |
0 commit comments