SIENTIAPDE-1184

refactor: enhance shutdown procedures and documentation across activities

- Added shutdown methods to MongoDB and Email classes to ensure proper resource cleanup.
- Updated docstrings for shutdown methods to clarify their purpose and functionality.
- Enhanced documentation for various methods across multiple classes, improving clarity on parameters and return values.
- Improved the main function and other utility functions with detailed docstrings for better understanding and maintainability.
This commit is contained in:
vitor-aignosi
2025-08-14 09:48:19 -03:00
parent bfb633c9d7
commit 31f2ff2439
9 changed files with 255 additions and 6 deletions

View File

@@ -82,4 +82,9 @@ class Activities( # Couchbase,
notification_handler=notification_handler)
def shutdown(self):
"""
Shutdown the MongoDB connection and clean up resources.
"""
MongoDB.shutdown(self)
Postgres.close(self)
Email.shutdown(self)

View File

@@ -42,6 +42,12 @@ class Email(BaseActivity):
logger=logger,
notification_handler=notification_handler)
def shutdown(self):
"""
Shutdown the Email connection and clean up resources.
"""
self.server.quit()
@activity.defn(name="build_email_html")
async def build_email_html(self, input_data: dict[str, Any]) -> dict[str, Any]:
"""
@@ -105,9 +111,15 @@ class Email(BaseActivity):
def try_send_email(self, msg: MIMEMultipart, receivers: str):
"""
Sends an email to the receivers.
"""
Sends an email to the receivers with automatic reconnection handling.
Args:
msg (MIMEMultipart): The email message to send.
receivers (str): Comma-separated list of email addresses to send to.
Raises:
Exception: If email sending fails after reconnection attempts.
"""
try:
self.server.sendmail(
self.sender_email, receivers, msg.as_string())

View File

@@ -64,7 +64,7 @@ class MongoDB(BaseActivity):
def shutdown(self):
"""
Close the MongoDB client connection.
Shutdown the MongoDB connection and clean up resources.
"""
try:
if self.client:
@@ -81,6 +81,16 @@ class MongoDB(BaseActivity):
self.shutdown()
def find(self, collection_name: str, filters: dict[str, Any]) -> list[dict[str, Any]]:
"""
Find documents in a MongoDB collection based on the provided filters.
Args:
collection_name (str): The name of the collection to search in.
filters (dict[str, Any]): The query filters to apply.
Returns:
list[dict[str, Any]]: List of documents matching the filters, with _id fields removed.
"""
collection = self.database[collection_name]
documents = list(collection.find(filters, {"_id": 0}))

View File

@@ -202,6 +202,14 @@ class SlotManager(Redis):
async def get_last_data_timestamp(self, input_data: dict[str, Any]) -> str | None:
"""
Gets the last data timestamp from redis.
Args:
input_data (dict[str, Any]): The input data containing:
- metadata (dict): Metadata for logging purposes.
- mail_type (str): The type of mail to get timestamp for.
Returns:
str | None: The last data timestamp as a string, or None if no timestamp exists.
"""
metadata = input_data['metadata']
key = f"notification_last_timestamp:{input_data['mail_type']}"
@@ -233,6 +241,15 @@ class SlotManager(Redis):
async def put_last_data_timestamp(self, input_data: dict[str, Any]):
"""
Puts the last data timestamp into redis.
Args:
input_data (dict[str, Any]): The input data containing:
- metadata (dict): Metadata for logging purposes.
- data (list[dict]): The data to extract timestamp from.
- mail_type (str): The type of mail to store timestamp for.
Returns:
str | None: The last data timestamp that was stored, or None if no data exists.
"""
metadata = input_data['metadata']
key = f"notification_last_timestamp:{input_data['mail_type']}"
@@ -270,7 +287,18 @@ class SlotManager(Redis):
@activity.defn(name="filter_notification_alerts")
async def filter_notification_alerts(self, input_data: dict[str, Any]) -> dict[str, Any]:
"""
Filter notification alerts
Filter notification alerts based on sending configurations and notification package.
Args:
input_data (dict[str, Any]): The input data containing:
- metadata (dict): Metadata for logging purposes.
- notification_package (list): The package of notifications to filter.
- sending_configs (list): The configurations for sending notifications.
Each config should have 'group_name', 'contents', and optionally 'ignore' fields.
- notification_ttl (int): Time to live for notifications in seconds.
Returns:
dict[str, Any]: The filtered receiver groups with their notifications.
"""
metadata = input_data['metadata']
notification_package = input_data['notification_package']
@@ -330,7 +358,13 @@ class SlotManager(Redis):
@activity.defn(name="store_notification_cache")
async def store_notification_cache(self, input_data: dict[str, Any]) -> None:
"""
Store notification cache
Store notification cache in Redis to track recently sent notifications.
Args:
input_data (dict[str, Any]): The input data containing:
- metadata (dict): Metadata for logging purposes.
- log_report (list[dict]): The log report containing notification statuses.
- sent_ttl (int): Time to live for sent notification cache in seconds.
"""
metadata = input_data['metadata']
log_report = DataFrame(input_data['log_report'])

View File

@@ -42,6 +42,10 @@ class TemporalManager(BaseActivity):
notification_handler=notification_handler)
async def connect_to_temporal(self):
"""
Connect to Temporal server namespaces for scouter and laborious workflows.
Creates client connections to both namespaces and stores them for later use.
"""
self.logger.info(
f"Connecting to Temporal side namespaces at {self.temporal_host}")
self.logger.info(f"Scouter namespace: {self.scouter_namespace}")