Skip to content

Commit 225409c

Browse files
avshetty1980motola
andauthored
Bug ved 1131 dlq for failed pds call fix (#1315)
* bug fix to ensure DLQ is activated after retries to PDS * bug fix to ensure DLQ is activated after retries to PDS * added comment for visibility timeout to id_sync queue * updated visibility timout from 6 to 3 times --------- Co-authored-by: Akinola Olutola <akinola.olutola1@nhs.net>
1 parent 042cace commit 225409c

5 files changed

Lines changed: 211 additions & 219 deletions

File tree

infrastructure/instance/sqs_id_sync.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_sqs_queue" "id_sync_queue" {
22
name = "imms-${local.resource_scope}-id-sync-queue"
33
kms_master_key_id = data.aws_kms_key.existing_id_sync_sqs_encryption_key.arn
4-
visibility_timeout_seconds = 360
4+
visibility_timeout_seconds = 1080 # as per AWS docs to be 6 times the Lambda function timeout but kept to 3 times
55
redrive_policy = jsonencode({
66
deadLetterTargetArn = aws_sqs_queue.id_sync_dlq.arn
77
maxReceiveCount = 4

lambdas/id_sync/src/id_sync.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
"""
2-
- Parses the incoming AWS event into `AwsLambdaEvent` and iterate its `records`.
3-
- Delegate each record to `process_record` and collect `nhs_number` from each result.
4-
- If any record has status == "error" raise `IdSyncException` with aggregated nhs_numbers.
5-
- Any unexpected error is wrapped into `IdSyncException(message="Error processing id_sync event")`.
2+
- Parses the incoming AWS event into `AwsLambdaEvent` and iterates its `records`.
3+
- Delegates each record to `process_record` with per-record exception isolation.
4+
- Returns {"batchItemFailures": [...]} for any failed records so SQS only re-drives the failing messages.
5+
- A handler-level exception (bad event schema etc.) re-raises to trigger full batch retry.
66
"""
77

88
from typing import Any
99

1010
from common.aws_lambda_event import AwsLambdaEvent
1111
from common.clients import STREAM_NAME, logger
1212
from common.log_decorator import logging_decorator
13-
from exceptions.id_sync_exception import IdSyncException
1413
from record_processor import process_record
1514

1615

@@ -25,28 +24,32 @@ def handler(event_data: dict[str, Any], _context) -> dict[str, Any]:
2524

2625
logger.info("id_sync processing event with %d records", len(records))
2726

28-
error_count = 0
27+
batch_item_failures = []
2928

3029
for record in records:
31-
result = process_record(record)
32-
33-
if result.get("status") == "error":
34-
error_count += 1
35-
36-
if error_count > 0:
37-
raise IdSyncException(
38-
message=f"Processed {len(records)} records with {error_count} errors",
39-
)
30+
try:
31+
result = process_record(record)
32+
if result.get("status") == "error":
33+
message_id = record.get("messageId")
34+
logger.error(
35+
"id_sync record processing failed for messageId: %s — %s",
36+
message_id,
37+
result.get("message"),
38+
)
39+
batch_item_failures.append({"itemIdentifier": message_id})
40+
except Exception:
41+
message_id = record.get("messageId")
42+
logger.exception("Unexpected error processing messageId: %s", message_id)
43+
batch_item_failures.append({"itemIdentifier": message_id})
44+
45+
if batch_item_failures:
46+
logger.error("id_sync completed with %d/%d failures", len(batch_item_failures), len(records))
47+
return {"batchItemFailures": batch_item_failures}
4048

4149
response = {"status": "success", "message": f"Successfully processed {len(records)} records"}
42-
4350
logger.info("id_sync handler completed: %s", response)
4451
return response
4552

46-
except IdSyncException as e:
47-
logger.exception(f"id_sync error: {e.message}")
48-
raise
4953
except Exception:
50-
msg = "Error processing id_sync event"
51-
logger.exception(msg)
52-
raise IdSyncException(message=msg)
54+
logger.exception("Unexpected error processing id_sync event")
55+
raise

0 commit comments

Comments
 (0)