Skip to content

Commit 8084a96

Browse files
Reducing complexity
1 parent 1ad32f5 commit 8084a96

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

aws_lambda_powertools/utilities/batch/sqs_fifo_partial_processor.py

+25-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import List, Optional, Tuple
2+
from typing import Dict, List, Optional, Tuple
33

44
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType
55
from aws_lambda_powertools.utilities.batch.types import BatchSqsTypeModel
@@ -106,23 +106,36 @@ def process(self) -> List[Tuple]:
106106
# If a processed message fail and skip_group_on_error is True,
107107
# mark subsequent messages from the same MessageGroupId as skipped
108108
if processed_messages[0] == "fail" and self._skip_group_on_error:
109-
_attributes_record = record.get("attributes", {})
110-
for subsequent_record in self.records[i + 1 :]:
111-
_attributes = subsequent_record.get("attributes", {})
112-
if _attributes.get("MessageGroupId") == _attributes_record.get("MessageGroupId"):
113-
skip_messages_group_id.append(subsequent_record.get("messageId"))
114-
data = self._to_batch_type(
115-
record=subsequent_record,
116-
event_type=self.event_type,
117-
model=self.model,
118-
)
119-
result.append(self.failure_handler(record=data, exception=self.circuit_breaker_exc))
109+
self._process_failed_subsequent_messages(record, i, skip_messages_group_id, result)
120110

121111
# Append the processed message normally
122112
result.append(processed_messages)
123113

124114
return result
125115

116+
def _process_failed_subsequent_messages(
117+
self,
118+
record: Dict,
119+
i: int,
120+
skip_messages_group_id: List,
121+
result: List[Tuple],
122+
) -> None:
123+
"""
124+
Process failed subsequent messages from the same MessageGroupId and mark them as skipped.
125+
"""
126+
_attributes_record = record.get("attributes", {})
127+
128+
for subsequent_record in self.records[i + 1 :]:
129+
_attributes = subsequent_record.get("attributes", {})
130+
if _attributes.get("MessageGroupId") == _attributes_record.get("MessageGroupId"):
131+
skip_messages_group_id.append(subsequent_record.get("messageId"))
132+
data = self._to_batch_type(
133+
record=subsequent_record,
134+
event_type=self.event_type,
135+
model=self.model,
136+
)
137+
result.append(self.failure_handler(record=data, exception=self.circuit_breaker_exc))
138+
126139
def _short_circuit_processing(self, first_failure_index: int, result: List[Tuple]) -> List[Tuple]:
127140
"""
128141
Starting from the first failure index, fail all the remaining messages, and append them to the result list.

0 commit comments

Comments
 (0)