(feature) Add run completion notifications
Add email and webhook notification targets with delivery tracking, and send notifications when backup runs reach a terminal status. Expose notification target management in the Django UI and keep delivery failures recorded without failing the backup worker.
This commit is contained in:
@@ -135,6 +135,63 @@ class BackupRun(models.Model):
|
||||
return f"{self.host} {self.run_type} {self.status}"
|
||||
|
||||
|
||||
def default_notification_statuses() -> list[str]:
|
||||
return [
|
||||
BackupRun.Status.SUCCESS,
|
||||
BackupRun.Status.WARNING,
|
||||
BackupRun.Status.FAILED,
|
||||
BackupRun.Status.CANCELLED,
|
||||
]
|
||||
|
||||
|
||||
class NotificationTarget(TimestampedModel):
|
||||
class Channel(models.TextChoices):
|
||||
EMAIL = "email", "Email"
|
||||
WEBHOOK = "webhook", "Webhook"
|
||||
|
||||
name = models.CharField(max_length=128, unique=True)
|
||||
enabled = models.BooleanField(default=True)
|
||||
channel = models.CharField(max_length=16, choices=Channel.choices)
|
||||
statuses = models.JSONField(default=default_notification_statuses, blank=True)
|
||||
email_to = models.TextField(blank=True)
|
||||
webhook_url = models.URLField(max_length=1024, blank=True)
|
||||
webhook_headers = models.JSONField(default=dict, blank=True)
|
||||
notes = models.TextField(blank=True)
|
||||
last_status = models.CharField(max_length=16, blank=True)
|
||||
last_error = models.TextField(blank=True)
|
||||
last_sent_at = models.DateTimeField(null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["name"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class NotificationDelivery(models.Model):
|
||||
class Status(models.TextChoices):
|
||||
SENT = "sent", "Sent"
|
||||
FAILED = "failed", "Failed"
|
||||
SKIPPED = "skipped", "Skipped"
|
||||
|
||||
target = models.ForeignKey(NotificationTarget, on_delete=models.CASCADE, related_name="deliveries")
|
||||
run = models.ForeignKey(BackupRun, on_delete=models.CASCADE, related_name="notification_deliveries")
|
||||
status = models.CharField(max_length=16, choices=Status.choices)
|
||||
error = models.TextField(blank=True)
|
||||
payload = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["target", "run"], name="unique_notification_delivery_per_target_run"),
|
||||
]
|
||||
ordering = ["-created_at", "target__name"]
|
||||
verbose_name_plural = "notification deliveries"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.target} run {self.run_id} {self.status}"
|
||||
|
||||
|
||||
class SnapshotRecord(models.Model):
|
||||
class Kind(models.TextChoices):
|
||||
SCHEDULED = "scheduled", "Scheduled"
|
||||
|
||||
Reference in New Issue
Block a user