-
Notifications
You must be signed in to change notification settings - Fork 8
Notification Service
Costas Yiallourides edited this page Jan 20, 2025
·
3 revisions
The Notification Service module provides a flexible and robust way to send alert notifications. It supports one-off and periodic email notifications, complete with attachment handling, retry mechanisms, and a configurable scheduler for interval-based alerts.
- One-off Notifications: Send immediate, single notification alerts.
- Periodic Notifications: Automatically send notifications at regular intervals using the built-in scheduler.
- Attachment Support: Attach files (e.g., CSVs, logs, images) to notifications.
- Retry Mechanisms: Handle failed attempts with configurable retries and backoff delays.
-
Customizable Configuration: Use dedicated dataclasses for flexible setup (e.g.,
EmailConfigandSchedulerConfig). - Ease of Use: Create notification instances with minimal setup and start sending alerts instantly.
The EmailConfig and SchedulerConfig dataclasses are used to configure email notifications and scheduling.
Example:
from notification_service import EmailConfig, SchedulerConfig
# Email and Scheduler configuration
email_config_dict = {
"subject": "Critical Alert",
"message": "Inverter is in error mode",
"recipients": ["[email protected]"],
"smtp_server": "smtp.example.com",
"smtp_port": 587,
"smtp_user": "[email protected]",
"smtp_password": "password",
"from_email": "[email protected]",
"attachments": ["alert_records.csv"],
"scheduler": {
"send_immediately": True,
"interval": 60,
"duration": 3600,
},
}
Create an EmailNotification object using the configuration.
from notification_service import EmailNotification
# Create an email notification instance
email_notification = EmailNotification.from_dict(email_config)
One-off Notification: Send a single email notification immediately.
email_notification.send()
Periodic Notifications: Start sending notifications periodically.
# Start the scheduler
email_notification.start_scheduler()
# Stop the scheduler after some time
import time
time.sleep(300) # Keep it running for 5 minutes
email_notification.stop_scheduler()
- SMTP Server Requirements: A valid SMTP server and credentials are required to send email notifications.
- Attachments: Ensure attached files exist and are accessible when sending notifications.
It is planned to extend the service to support additional channels with Slack being the first priority.