196 lines
6.7 KiB
Python
196 lines
6.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
ServiceTaskRetriever Usage Example
|
|
|
|
This script demonstrates how to use the ServiceTaskRetriever class
|
|
and the updated MainRedisHandler for direct Redis task retrieval and updates.
|
|
"""
|
|
|
|
import uuid
|
|
import logging
|
|
from json import dumps
|
|
|
|
from config import Status, RedisData
|
|
from redis_handlers import ServiceTaskRetriever, MainRedisHandler
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def direct_retriever_example():
|
|
"""Example using ServiceTaskRetriever directly"""
|
|
logger.info("=== ServiceTaskRetriever Direct Usage Example ===")
|
|
|
|
# Create a retriever instance
|
|
retriever = ServiceTaskRetriever()
|
|
|
|
# Generate a test UUID
|
|
test_uuid = str(uuid.uuid4())
|
|
logger.info(f"Generated test UUID: {test_uuid}")
|
|
|
|
# Example mail data
|
|
mail_data = {
|
|
"id": "test-mail-123",
|
|
"subject": "Test Mail Subject",
|
|
"body": "This is a test mail body",
|
|
"from": "test@example.com",
|
|
"to": "recipient@example.com",
|
|
"date": "2025-08-09T14:08:05+03:00"
|
|
}
|
|
|
|
try:
|
|
# Store a new task with UUID
|
|
logger.info("Storing new task with UUID...")
|
|
success = retriever.store_task_with_uuid(
|
|
test_uuid,
|
|
"MailReader",
|
|
mail_data
|
|
)
|
|
logger.info(f"Task stored successfully: {success}")
|
|
|
|
# Retrieve the task by UUID
|
|
logger.info("Retrieving task by UUID...")
|
|
task = retriever.get_task_by_uuid(test_uuid)
|
|
logger.info(f"Retrieved task: service={task.service}, status={task.status}")
|
|
|
|
# Get service-specific data
|
|
logger.info("Retrieving service data by UUID...")
|
|
mail_reader_data = retriever.get_service_data_by_uuid(test_uuid, "MailReader")
|
|
logger.info(f"Mail subject: {mail_reader_data.get('subject')}")
|
|
|
|
# Update task status
|
|
logger.info("Updating task status...")
|
|
retriever.update_task_status(
|
|
test_uuid,
|
|
is_completed=True,
|
|
status=Status.COMPLETED
|
|
)
|
|
|
|
# Verify status update
|
|
updated_task = retriever.get_task_by_uuid(test_uuid)
|
|
logger.info(f"Updated task status: {updated_task.status}, completed: {updated_task.is_completed}")
|
|
|
|
# Update service data
|
|
logger.info("Updating service data...")
|
|
new_mail_data = mail_data.copy()
|
|
new_mail_data["subject"] = "Updated Subject"
|
|
retriever.update_service_data(test_uuid, "MailReader", new_mail_data)
|
|
|
|
# Verify service data update
|
|
updated_mail_data = retriever.get_service_data_by_uuid(test_uuid, "MailReader")
|
|
logger.info(f"Updated mail subject: {updated_mail_data.get('subject')}")
|
|
|
|
except FileNotFoundError as e:
|
|
logger.error(f"Not found error: {e}")
|
|
except ValueError as e:
|
|
logger.error(f"Value error: {e}")
|
|
|
|
def main_handler_example():
|
|
"""Example using MainRedisHandler"""
|
|
logger.info("\n=== MainRedisHandler Usage Example ===")
|
|
|
|
# Create a handler instance
|
|
handler = MainRedisHandler()
|
|
|
|
# Generate a test UUID
|
|
test_uuid = str(uuid.uuid4())
|
|
logger.info(f"Generated test UUID: {test_uuid}")
|
|
|
|
# Example mail data
|
|
mail_data = {
|
|
"id": "test-mail-456",
|
|
"subject": "Test Mail via MainRedisHandler",
|
|
"body": "This is a test mail body via MainRedisHandler",
|
|
"from": "test@example.com",
|
|
"to": "recipient@example.com",
|
|
"date": "2025-08-09T14:08:05+03:00"
|
|
}
|
|
|
|
try:
|
|
# Store a new task with UUID
|
|
logger.info("Storing new task with UUID via MainRedisHandler...")
|
|
success = handler.store_task_with_uuid(
|
|
test_uuid,
|
|
"MailReader",
|
|
mail_data
|
|
)
|
|
logger.info(f"Task stored successfully: {success}")
|
|
|
|
# Retrieve the task by UUID
|
|
logger.info("Retrieving task by UUID...")
|
|
task = handler.get_task_by_uuid(test_uuid)
|
|
logger.info(f"Retrieved task: service={task.service}, status={task.status}")
|
|
|
|
# Get service-specific data
|
|
logger.info("Retrieving service data by UUID...")
|
|
mail_reader_data = handler.get_service_data_by_uuid(test_uuid, "MailReader")
|
|
logger.info(f"Mail subject: {mail_reader_data.get('subject')}")
|
|
|
|
# Update task status
|
|
logger.info("Updating task status...")
|
|
handler.update_task_status(
|
|
test_uuid,
|
|
is_completed=True,
|
|
status=Status.COMPLETED
|
|
)
|
|
|
|
# Verify status update
|
|
updated_task = handler.get_task_by_uuid(test_uuid)
|
|
logger.info(f"Updated task status: {updated_task.status}, completed: {updated_task.is_completed}")
|
|
|
|
# Update service data
|
|
logger.info("Updating service data...")
|
|
new_mail_data = mail_data.copy()
|
|
new_mail_data["subject"] = "Updated Subject via MainRedisHandler"
|
|
handler.update_service_data(test_uuid, "MailReader", new_mail_data)
|
|
|
|
# Verify service data update
|
|
updated_mail_data = handler.get_service_data_by_uuid(test_uuid, "MailReader")
|
|
logger.info(f"Updated mail subject: {updated_mail_data.get('subject')}")
|
|
|
|
except FileNotFoundError as e:
|
|
logger.error(f"Not found error: {e}")
|
|
except ValueError as e:
|
|
logger.error(f"Value error: {e}")
|
|
|
|
def error_handling_example():
|
|
"""Example demonstrating error handling"""
|
|
logger.info("\n=== Error Handling Example ===")
|
|
|
|
retriever = ServiceTaskRetriever()
|
|
|
|
# Try to retrieve non-existent task
|
|
try:
|
|
logger.info("Attempting to retrieve non-existent task...")
|
|
task = retriever.get_task_by_uuid("non-existent-uuid")
|
|
except FileNotFoundError as e:
|
|
logger.info(f"Expected error caught: {e}")
|
|
|
|
# Try to update non-existent task
|
|
try:
|
|
logger.info("Attempting to update non-existent task...")
|
|
retriever.update_task_status("non-existent-uuid", True, Status.COMPLETED)
|
|
except FileNotFoundError as e:
|
|
logger.info(f"Expected error caught: {e}")
|
|
|
|
# Try to update with invalid service name
|
|
test_uuid = str(uuid.uuid4())
|
|
mail_data = {"subject": "Test"}
|
|
retriever.store_task_with_uuid(test_uuid, "MailReader", mail_data)
|
|
|
|
try:
|
|
logger.info("Attempting to update with invalid service name...")
|
|
retriever.update_service_data(test_uuid, "NonExistentService", {"data": "test"})
|
|
except ValueError as e:
|
|
logger.info(f"Expected error caught: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
direct_retriever_example()
|
|
main_handler_example()
|
|
error_handling_example()
|