From bd8b4ede9a1c5cacdf565e581d9f9af7ee78baee Mon Sep 17 00:00:00 2001 From: Adrian Clay Date: Tue, 28 Jan 2025 14:12:25 +0000 Subject: [PATCH] WIP: Don't retry if retries hasn't been specified by SDS For testing CPIS --- .../mhs_common/routing/sds_api_client.py | 5 ++++- .../workflow/asynchronous_forward_reliable.py | 8 +------- .../workflow/asynchronous_reliable.py | 19 ++++++++++--------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/mhs/common/mhs_common/routing/sds_api_client.py b/mhs/common/mhs_common/routing/sds_api_client.py index 2b470cbe0..701efeac9 100644 --- a/mhs/common/mhs_common/routing/sds_api_client.py +++ b/mhs/common/mhs_common/routing/sds_api_client.py @@ -93,7 +93,10 @@ def _set_identifier_value(resource, system, value): def _get_extension(endpoint, system, value_key): def _get_extensions(resource): return list(filter(lambda kv: kv['url'] == 'https://fhir.nhs.uk/StructureDefinition/Extension-SDS-ReliabilityConfiguration', resource['extension']))[0]['extension'] - return list(filter(lambda kv: kv['url'] == system, _get_extensions(endpoint)))[0][value_key] + extensions_matching_system = list(filter(lambda kv: kv['url'] == system, _get_extensions(endpoint))) + if len(extensions_matching_system) > 0: + return extensions_matching_system[0][value_key] + return None async def _get_endpoint_resource(self, interaction_id: str, ods_code: str = None) -> Dict: if not ods_code: diff --git a/mhs/common/mhs_common/workflow/asynchronous_forward_reliable.py b/mhs/common/mhs_common/workflow/asynchronous_forward_reliable.py index 2b5c6b7f9..3e369b764 100644 --- a/mhs/common/mhs_common/workflow/asynchronous_forward_reliable.py +++ b/mhs/common/mhs_common/workflow/asynchronous_forward_reliable.py @@ -60,12 +60,6 @@ async def handle_outbound_message(self, from_asid: Optional[str], reliability_details = await self._lookup_reliability_details(interaction_details, interaction_details.get('ods-code')) - retry_interval_xml_datetime = reliability_details[common_asynchronous.MHS_RETRY_INTERVAL] - try: - retry_interval = DateUtilities.convert_xml_date_time_format_to_seconds(retry_interval_xml_datetime) - except isoerror.ISO8601Error: - await wdo.set_outbound_status(wd.MessageStatus.OUTBOUND_MESSAGE_PREPARATION_FAILED) - return 500, 'Error when converting retry interval: {} to seconds'.format(retry_interval_xml_datetime), None error, http_headers, message = await self._serialize_outbound_message(message_id, correlation_id, interaction_details, @@ -74,7 +68,7 @@ async def handle_outbound_message(self, from_asid: Optional[str], return error[0], error[1], None return await self._make_outbound_request_with_retries_and_handle_response(url, http_headers, message, wdo, - reliability_details, retry_interval) + reliability_details) @timing.time_function async def handle_unsolicited_inbound_message(self, message_id: str, correlation_id: str, message_data: MessageData): diff --git a/mhs/common/mhs_common/workflow/asynchronous_reliable.py b/mhs/common/mhs_common/workflow/asynchronous_reliable.py index a8c51a6b9..d68840ddc 100644 --- a/mhs/common/mhs_common/workflow/asynchronous_reliable.py +++ b/mhs/common/mhs_common/workflow/asynchronous_reliable.py @@ -64,12 +64,6 @@ async def handle_outbound_message(self, from_asid: Optional[str], return 500, 'Error obtaining outbound URL', None reliability_details = await self._lookup_reliability_details(interaction_details) - retry_interval_xml_datetime = reliability_details[common_asynchronous.MHS_RETRY_INTERVAL] - try: - retry_interval = DateUtilities.convert_xml_date_time_format_to_seconds(retry_interval_xml_datetime) - except isoerror.ISO8601Error: - await wdo.set_outbound_status(wd.MessageStatus.OUTBOUND_MESSAGE_PREPARATION_FAILED) - return 500, 'Error when converting retry interval: {} to seconds'.format(retry_interval_xml_datetime), None error, http_headers, message = await self._serialize_outbound_message(message_id, correlation_id, interaction_details, @@ -78,12 +72,12 @@ async def handle_outbound_message(self, from_asid: Optional[str], return error[0], error[1], None return await self._make_outbound_request_with_retries_and_handle_response(url, http_headers, message, wdo, - reliability_details, retry_interval) + reliability_details) async def _make_outbound_request_with_retries_and_handle_response(self, url: str, http_headers: Dict[str, str], message: str, wdo: wd.WorkDescription, - reliability_details: dict, retry_interval: float): - num_of_retries = int(reliability_details[common_asynchronous.MHS_RETRIES]) + reliability_details: dict): + num_of_retries = int(reliability_details[common_asynchronous.MHS_RETRIES] or 0) # retries_remaining is a mutable integer. This is done by putting an (immutable) integer into # a mutable container. @@ -98,6 +92,13 @@ async def _make_outbound_request_with_retries_and_handle_response(self, url: str handle_error_response) except _NeedToRetryException: retries_remaining[0] -= 1 + retry_interval_xml_datetime = reliability_details[common_asynchronous.MHS_RETRY_INTERVAL] + try: + retry_interval = DateUtilities.convert_xml_date_time_format_to_seconds(retry_interval_xml_datetime) + except isoerror.ISO8601Error: + await wdo.set_outbound_status(wd.MessageStatus.OUTBOUND_MESSAGE_PREPARATION_FAILED) + return 500, 'Error when converting retry interval: {} to seconds'.format(retry_interval_xml_datetime), None + logger.info("Waiting for {retry_interval} seconds before next request attempt.", fparams={"retry_interval": retry_interval}) await asyncio.sleep(retry_interval)