email_notifications #7

Merged
jonas merged 18 commits from email_notifications into main 2025-10-16 13:20:27 +00:00
2 changed files with 47 additions and 5 deletions
Showing only changes of commit c07d025873 - Show all commits

View File

@@ -235,10 +235,10 @@ async def lifespan(app: FastAPI):
# Initialize pushover service
pushover_service = create_pushover_service(config)
# Setup logging from config with email monitoring
# Setup logging from config with email and pushover monitoring
# Only primary worker should have the report scheduler running
email_handler, report_scheduler = setup_logging(
config, email_service, loop, enable_scheduler=is_primary
config, email_service, pushover_service, loop, enable_scheduler=is_primary
)
_LOGGER.info("Application startup initiated (primary_worker=%s)", is_primary)

View File

@@ -11,13 +11,18 @@ from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from alpine_bits_python.email_monitoring import DailyReportScheduler, EmailAlertHandler
from alpine_bits_python.email_monitoring import (
DailyReportScheduler,
EmailAlertHandler,
)
from alpine_bits_python.email_service import EmailService
from alpine_bits_python.pushover_service import PushoverService
def setup_logging(
config: dict | None = None,
email_service: "EmailService | None" = None,
pushover_service: "PushoverService | None" = None,
loop: asyncio.AbstractEventLoop | None = None,
enable_scheduler: bool = True,
) -> tuple["EmailAlertHandler | None", "DailyReportScheduler | None"]:
@@ -26,12 +31,13 @@ def setup_logging(
Args:
config: Application configuration dict with optional 'logger' section
email_service: Optional email service for email alerts
pushover_service: Optional pushover service for push notifications
loop: Optional asyncio event loop for email alerts
enable_scheduler: Whether to enable the daily report scheduler
(should be False for non-primary workers)
Returns:
Tuple of (email_alert_handler, daily_report_scheduler) if email monitoring
Tuple of (email_alert_handler, daily_report_scheduler) if monitoring
is enabled, otherwise (None, None)
Logger config format:
@@ -86,10 +92,11 @@ def setup_logging(
root_logger.info("Logging configured at %s level", level)
# Setup email monitoring if configured
# Setup notification monitoring if configured
email_handler = None
report_scheduler = None
# Setup email monitoring if configured
if email_service:
email_config = config.get("email", {})
monitoring_config = email_config.get("monitoring", {})
@@ -131,6 +138,41 @@ def setup_logging(
"Daily report scheduler disabled (non-primary worker)"
)
# Check if Pushover daily reports are enabled
# If so and no report_scheduler exists yet, create one
if pushover_service and not report_scheduler:
pushover_config = config.get("pushover", {})
pushover_monitoring = pushover_config.get("monitoring", {})
pushover_daily_report = pushover_monitoring.get("daily_report", {})
if pushover_daily_report.get("enabled", False) and enable_scheduler:
try:
# Import here to avoid circular dependencies
from alpine_bits_python.email_monitoring import DailyReportScheduler
# Create a dummy config for the scheduler
# (it doesn't need email-specific fields if email is disabled)
scheduler_config = {
"send_time": pushover_daily_report.get("send_time", "08:00"),
"include_stats": pushover_daily_report.get("include_stats", True),
"include_errors": pushover_daily_report.get("include_errors", True),
"recipients": [], # Not used for Pushover
}
report_scheduler = DailyReportScheduler(
email_service=email_service, # Can be None
config=scheduler_config,
)
root_logger.info(
"Daily report scheduler configured for Pushover (primary worker)"
)
except Exception:
root_logger.exception("Failed to setup Pushover daily report scheduler")
elif pushover_daily_report.get("enabled", False) and not enable_scheduler:
root_logger.info(
"Pushover daily report scheduler disabled (non-primary worker)"
)
return email_handler, report_scheduler