send notification on success or failure

This commit is contained in:
jaina heartles 2025-01-01 21:28:20 -05:00
parent 78290f44cc
commit b71910e379

View file

@ -17,8 +17,8 @@ let
tar -cz -C /var/lib/redis-misskey . tar -cz -C /var/lib/redis-misskey .
''; '';
notifyErrorPkg = pkgs.writeShellApplication { notifyEmailPkg = pkgs.writeShellApplication {
name = "notify-error"; name = "notify-email";
runtimeInputs = [ pkgs.system-sendmail pkgs.coreutils ]; runtimeInputs = [ pkgs.system-sendmail pkgs.coreutils ];
@ -29,10 +29,15 @@ let
to="admin@heartles.xyz" to="admin@heartles.xyz"
email="$(cat <<EOEMAIL header="$(cat <<EOEMAIL
To: $to To: $to
From: $from From: $from
Subject: $subject Subject: $subject
EOEMAIL
)"
email="$(cat <<EOEMAIL
$header
$text $text
@ -40,7 +45,7 @@ let
)" )"
echo "Sending message:" echo "Sending message:"
cat <<< "$email" cat <<< "$header"
sendmail -f "$from" "$to" <<< "$email" sendmail -f "$from" "$to" <<< "$email"
''; '';
@ -102,33 +107,36 @@ in {
echo "Backup complete to '$s3Dir'" echo "Backup complete to '$s3Dir'"
''; '';
after = [ "network-online.target" ]; serviceConfig.ExecStopPost = let
wants = [ "network-online.target" ]; script = pkgs.writeScript "backup-notify" ''
requires = [ "postgresql.service" ];
unitConfig.OnFailure = "misskey-backup-failure.service";
};
systemd.services.misskey-backup-failure = {
description = "Misskey backup failure notification";
serviceConfig.User = user;
serviceConfig.Type = "oneshot";
script = ''
invocationId="$(systemctl show --value -p InvocationID misskey-backup.service)" invocationId="$(systemctl show --value -p InvocationID misskey-backup.service)"
logs="$(journalctl _SYSTEMD_INVOCATION_ID="$invocationId" -u misskey-backup.service)" logs="$(journalctl _SYSTEMD_INVOCATION_ID="$invocationId" -u misskey-backup.service)"
${notifyErrorPkg}/bin/notify-error "backup" "Misskey Backup Failure Notification" "$(cat <<EOMSG if [ "$SERVICE_RESULT" = "success"]; then
${notifyEmailPkg}/bin/notify-email "backup" "SUCCESS: Misskey Backup Notification" "$(cat <<EOMSG
A backup process has succeeded. Logs to follow:
$logs
EOMSG
)"
else
${notifyEmailPkg}/bin/notify-email "backup" "FAILURE: Misskey Backup Notification" "$(cat <<EOMSG
A backup process has failed. Logs to follow: A backup process has failed. Logs to follow:
$logs $logs
EOMSG EOMSG
)" )"
fi
''; '';
in "${script}";
requires = [ "postfix.service" ]; after = [ "network-online.target" ];
wants = [ "network-online.target" ];
requires = [ "postgresql.service" ];
unitConfig.OnFailure = "misskey-backup-failure.service";
}; };
systemd.timers.misskey-backup = { timerConfig.Persistent = true; }; systemd.timers.misskey-backup = { timerConfig.Persistent = true; };