feat(systemdNotify): limit to one invocation every 15 minutes per service
All checks were successful
Test / tests (push) Successful in 4m5s

Also adds an `ExecCondition` for ignoring service auto-resarts.
This commit is contained in:
Felix Schröter 2025-09-23 22:15:57 +02:00
parent c8e0b67ed4
commit 9690a9de0f
Signed by: felschr
GPG key ID: 671E39E6744C807D

View file

@ -26,6 +26,16 @@ let
$(systemctl status --full "$1") $(systemctl status --full "$1")
ERRMAIL ERRMAIL
''; '';
checkConditions = pkgs.writeScript "check-conditions" ''
#!/bin/sh
STATUS=$(systemctl status --full "$1")
case "$STATUS" in
*"activating (auto-restart) (Result: timeout)"*) exit 1 ;;
*) exit 0 ;;
esac
'';
in in
{ {
options = { options = {
@ -85,32 +95,38 @@ in
} }
]; ];
systemd.services."notify@" = { systemd.services."notify@" =
onFailure = lib.mkForce [ ]; lib.recursiveUpdate
} {
// optionalAttrs (cfg.method == "libnotify") { onFailure = lib.mkForce [ ];
description = "Desktop notifications for %i service failure"; unitConfig = {
environment = { StartLimitIntervalSec = "15m";
DISPLAY = ":0"; StartLimitBurst = 1;
INSTANCE = "%i"; };
}; serviceConfig = {
script = '' Type = "oneshot";
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u '${cfg.libnotify.user}')/bus" ExecCondition = "${checkConditions} %i";
${pkgs.libnotify}/bin/notify-send --app-name="$INSTANCE" --urgency=critical \ };
"Service '$INSTANCE' failed" \ }
"$(journalctl -n 6 -o cat -u $INSTANCE)" (
''; optionalAttrs (cfg.method == "libnotify") {
serviceConfig = { description = "Desktop notifications for %i service failure";
Type = "oneshot"; environment = {
User = cfg.libnotify.user; DISPLAY = ":0";
}; INSTANCE = "%i";
} };
// optionalAttrs (cfg.method == "email") { script = ''
description = "E-Mail notifications for %i service failure"; export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u '${cfg.libnotify.user}')/bus"
serviceConfig = { ${pkgs.libnotify}/bin/notify-send --app-name="$INSTANCE" --urgency=critical \
ExecStart = "${sendmail} %i"; "Service '$INSTANCE' failed" \
Type = "oneshot"; "$(journalctl -n 6 -o cat -u $INSTANCE)"
}; '';
}; serviceConfig.User = cfg.libnotify.user;
}
// optionalAttrs (cfg.method == "email") {
description = "E-Mail notifications for %i service failure";
serviceConfig.ExecStart = "${sendmail} %i";
}
);
}; };
} }