From 105345c078ff8dd4434582d06660164654540c91 Mon Sep 17 00:00:00 2001 From: "Prashant.Mishra" Date: Tue, 24 Dec 2024 18:34:31 +0530 Subject: [PATCH] Handle no priority set case. Take CP config refresh delay from config --- .../channel_partners/get_configurations.py | 4 +++- app/services/handlers/abstract_handler.py | 4 ++++ app/services/handlers/gateway_priority.py | 14 +++++++++++++- config_template.json | 3 +++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/services/channel_partners/get_configurations.py b/app/services/channel_partners/get_configurations.py index 581fc62..c225099 100644 --- a/app/services/channel_partners/get_configurations.py +++ b/app/services/channel_partners/get_configurations.py @@ -1,6 +1,8 @@ import asyncio import logging +from torpedo import CONFIG + from app.constants import Channels from app.services.handlers.email.handler import EmailHandler from app.services.handlers.sms.handler import SmsHandler @@ -13,7 +15,7 @@ class ChannelPartners: - _periodic_sync_delay = 5*60 + _periodic_sync_delay = CONFIG.config["CHANNEL_PARTNERS"]["CONFIG_SYNC_DELAY"] PROVIDERS_CONFIG = dict() @classmethod diff --git a/app/services/handlers/abstract_handler.py b/app/services/handlers/abstract_handler.py index 8c607f7..b8ce03f 100644 --- a/app/services/handlers/abstract_handler.py +++ b/app/services/handlers/abstract_handler.py @@ -40,6 +40,10 @@ def get_default_priority(cls) -> list: def get_priority_logic(cls) -> str: return cls._HANDLER_CONFIG["dynamic_priority"] + @classmethod + def get_configured_gateways(cls) -> list: + return list(cls.PROVIDERS.keys()) + @classmethod def update_configuration(cls, handler_configuration: dict): cls._HANDLER_CONFIG = handler_configuration diff --git a/app/services/handlers/gateway_priority.py b/app/services/handlers/gateway_priority.py index f69f67b..f40be8b 100644 --- a/app/services/handlers/gateway_priority.py +++ b/app/services/handlers/gateway_priority.py @@ -22,6 +22,14 @@ def get_default_priority(cls) -> list: """ pass + @classmethod + @abstractmethod + def get_configured_gateways(cls) -> list: + """ + This abstract method must be overridden by the channel handler + """ + pass + @classmethod def select_gateway(cls, n_attempts: int, request_data: dict) -> str: """ @@ -50,4 +58,8 @@ def select_gateway(cls, n_attempts: int, request_data: dict) -> str: # Log the exception and fallback to default priority order pass total_gateways = min(len(cls.get_default_priority()), len(current_priority)) - return current_priority[n_attempts % total_gateways] + if total_gateways > 0: + return current_priority[n_attempts % total_gateways] + else: + all_gateways = cls.get_configured_gateways() + return all_gateways[n_attempts % len(all_gateways)] diff --git a/config_template.json b/config_template.json index f3b4d83..d99240f 100644 --- a/config_template.json +++ b/config_template.json @@ -61,5 +61,8 @@ "NOTIFYONE_CORE": { "HOST": "http://localhost:9402", "TIMEOUT": 10 + }, + "CHANNEL_PARTNERS": { + "CONFIG_SYNC_DELAY": 5 } } \ No newline at end of file