Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions frappe_restic/restic_backup/backup_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from frappe.utils import cint, get_time, now_datetime
from frappe.utils.background_jobs import enqueue
from frappe.utils.backups import BackupGenerator, get_backup_path
from frappe.utils.user import get_users_with_role
from redis.exceptions import LockError

from frappe_restic.config import namespace
Expand Down Expand Up @@ -475,10 +474,12 @@ def _send_alert(subject: str, message: str, *, succeeded: bool = False, run_name
if succeeded and not _settings_int("email_on_success", 0):
return "Not requested"
try:
# Frappe's own role lookups leave out Administrator, the only manager on a small site.
names = frappe.get_all(
"Has Role", filters={"role": SYSTEM_MANAGER_ROLE, "parenttype": "User"}, pluck="parent"
)
managers = frappe.get_all(
"User",
filters={"name": ["in", get_users_with_role(SYSTEM_MANAGER_ROLE)]},
fields=["name", "email"],
"User", filters={"name": ["in", names], "enabled": 1}, fields=["name", "email"]
)
except Exception: # notification failure must not replace the operation outcome
frappe.logger("restic_backup").exception("Could not find the System Managers to notify")
Expand Down
8 changes: 6 additions & 2 deletions frappe_restic/restic_backup/test_backup_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,19 @@ def _alert(self, *, succeeded: bool, email_on_success: int = 0, desk=None, email
]
with (
patch.object(control, "_settings_int", return_value=email_on_success),
patch.object(control, "get_users_with_role", return_value=["Administrator", "ops@example.com"]),
patch("frappe.get_all", return_value=managers),
patch("frappe.get_all", side_effect=[["Administrator", "ops@example.com"], managers]) as get_all,
patch("frappe.logger"),
patch.object(control, "enqueue_create_notification", side_effect=desk) as notify,
patch("frappe.sendmail", side_effect=email) as sendmail,
):
status = control._send_alert(
"FAILED: backup", "disk full", succeeded=succeeded, run_name="run123"
)
if get_all.call_count:
self.assertEqual(
get_all.call_args.kwargs["filters"],
{"name": ["in", ["Administrator", "ops@example.com"]], "enabled": 1},
)
return status, notify, sendmail

def test_failure_reaches_every_system_manager_in_the_desk_and_by_email(self) -> None:
Expand Down
Loading