|
| 1 | +import sys |
| 2 | +from typing import List, Optional, Tuple, Type |
| 3 | + |
| 4 | +from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType |
| 5 | +from aws_lambda_powertools.utilities.parser.models import SqsRecordModel |
| 6 | + |
| 7 | +# |
| 8 | +# type specifics |
| 9 | +# |
| 10 | +has_pydantic = "pydantic" in sys.modules |
| 11 | + |
| 12 | +# For IntelliSense and Mypy to work, we need to account for possible SQS subclasses |
| 13 | +# We need them as subclasses as we must access their message ID or sequence number metadata via dot notation |
| 14 | +if has_pydantic: |
| 15 | + BatchTypeModels = Optional[Type[SqsRecordModel]] |
| 16 | + |
| 17 | + |
| 18 | +class SQSFifoCircuitBreakerError(Exception): |
| 19 | + """ |
| 20 | + Signals a record not processed due to the SQS FIFO processing being interrupted |
| 21 | + """ |
| 22 | + |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +class SQSFifoPartialProcessor(BatchProcessor): |
| 27 | + """Specialized BatchProcessor subclass that handles FIFO SQS batch records. |
| 28 | +
|
| 29 | + As soon as the processing of the first record fails, the remaining records |
| 30 | + are marked as failed without processing, and returned as native partial responses. |
| 31 | +
|
| 32 | + Example |
| 33 | + _______ |
| 34 | +
|
| 35 | + ## Process batch triggered by a FIFO SQS |
| 36 | +
|
| 37 | + ```python |
| 38 | + import json |
| 39 | +
|
| 40 | + from aws_lambda_powertools import Logger, Tracer |
| 41 | + from aws_lambda_powertools.utilities.batch import SQSFifoPartialProcessor, EventType, batch_processor |
| 42 | + from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord |
| 43 | + from aws_lambda_powertools.utilities.typing import LambdaContext |
| 44 | +
|
| 45 | +
|
| 46 | + processor = SQSFifoPartialProcessor() |
| 47 | + tracer = Tracer() |
| 48 | + logger = Logger() |
| 49 | +
|
| 50 | +
|
| 51 | + @tracer.capture_method |
| 52 | + def record_handler(record: SQSRecord): |
| 53 | + payload: str = record.body |
| 54 | + if payload: |
| 55 | + item: dict = json.loads(payload) |
| 56 | + ... |
| 57 | +
|
| 58 | + @logger.inject_lambda_context |
| 59 | + @tracer.capture_lambda_handler |
| 60 | + @batch_processor(record_handler=record_handler, processor=processor) |
| 61 | + def lambda_handler(event, context: LambdaContext): |
| 62 | + return processor.response() |
| 63 | + ``` |
| 64 | + """ |
| 65 | + |
| 66 | + circuitBreakerError = SQSFifoCircuitBreakerError("A previous record failed processing.") |
| 67 | + |
| 68 | + def __init__(self, model: Optional["BatchTypeModels"] = None): |
| 69 | + super().__init__(EventType.SQS, model) |
| 70 | + |
| 71 | + def process(self) -> List[Tuple]: |
| 72 | + result: List[Tuple] = [] |
| 73 | + |
| 74 | + for i, record in enumerate(self.records): |
| 75 | + """ |
| 76 | + If we have failed messages, it means that the last message failed. |
| 77 | + We then short circuit the process, failing the remaining messages |
| 78 | + """ |
| 79 | + if self.fail_messages: |
| 80 | + return self._short_circuit_processing(i, result) |
| 81 | + |
| 82 | + """ |
| 83 | + Otherwise, process the message normally |
| 84 | + """ |
| 85 | + result.append(self._process_record(record)) |
| 86 | + |
| 87 | + return result |
| 88 | + |
| 89 | + def _short_circuit_processing(self, first_failure_index: int, result: List[Tuple]) -> List[Tuple]: |
| 90 | + remaining_records = self.records[first_failure_index:] |
| 91 | + for remaining_record in remaining_records: |
| 92 | + data = self._to_batch_type(record=remaining_record, event_type=self.event_type, model=self.model) |
| 93 | + result.append( |
| 94 | + self.failure_handler( |
| 95 | + record=data, exception=(type(self.circuitBreakerError), self.circuitBreakerError, None) |
| 96 | + ) |
| 97 | + ) |
| 98 | + return result |
| 99 | + |
| 100 | + async def _async_process_record(self, record: dict): |
| 101 | + raise NotImplementedError() |
0 commit comments