Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/services/channel_partners/get_configurations.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,7 +15,7 @@

class ChannelPartners:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUNTIME_ERROR: The new configuration parameter CONFIG_SYNC_DELAY is introduced, but there's no error handling if this configuration is missing. This could lead to a runtime error if the configuration is not properly set.

# Add a default value and error handling
_periodic_sync_delay = CONFIG.config.get("CHANNEL_PARTNERS", {}).get("CONFIG_SYNC_DELAY", 300)  # Default to 5 minutes

_periodic_sync_delay = 5*60
_periodic_sync_delay = CONFIG.config["CHANNEL_PARTNERS"]["CONFIG_SYNC_DELAY"]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PERFORMANCE: The _periodic_sync_delay value is now being read from the configuration file, which is a good practice. However, there's no validation or error handling for this value. If the configuration is missing or invalid, it could lead to unexpected behavior. Consider adding a default value and error handling to ensure the system behaves predictably even if the configuration is missing or invalid.

class ChannelPartners:
    _periodic_sync_delay = CONFIG.config.get("CHANNEL_PARTNERS", {}).get("CONFIG_SYNC_DELAY", 300)  # Default to 5 minutes
    PROVIDERS_CONFIG = dict()

    @classmethod
    def __init__(cls):
        if cls._periodic_sync_delay <= 0:
            logger.warning("Invalid CONFIG_SYNC_DELAY, using default of 300 seconds")
            cls._periodic_sync_delay = 300

PROVIDERS_CONFIG = dict()

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions app/services/handlers/abstract_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CODE_ROBUSTNESS: The addition of the get_configured_gateways method to the AbstractHandler class enhances code robustness by providing a fallback mechanism when no gateways are available in the priority list.

return list(cls.PROVIDERS.keys())

@classmethod
def update_configuration(cls, handler_configuration: dict):
cls._HANDLER_CONFIG = handler_configuration
Expand Down
14 changes: 13 additions & 1 deletion app/services/handlers/gateway_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CODE_ROBUSTNESS: The code now handles the case when no priority is set or when the total_gateways is 0, improving the robustness of the gateway selection process.

return current_priority[n_attempts % total_gateways]
else:
all_gateways = cls.get_configured_gateways()
return all_gateways[n_attempts % len(all_gateways)]
3 changes: 3 additions & 0 deletions config_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
"NOTIFYONE_CORE": {
"HOST": "http://localhost:9402",
"TIMEOUT": 10
},
"CHANNEL_PARTNERS": {
"CONFIG_SYNC_DELAY": 5
}
}