jobs: notify on failure

master
Michele Guerini Rocco 2022-10-18 18:24:45 +02:00
parent 50daabb203
commit 1f36291ba1
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450
2 changed files with 40 additions and 12 deletions

View File

@ -1,20 +1,35 @@
{ writeScriptBin, fish, curl
{ lib
, writers
, curl
, jq
, homeserver
, roomId
, authToken
}:
writeScriptBin "notify" ''
#!${fish}/bin/fish
writers.writeDashBin "notify" ''
export PATH="$PATH:${lib.makeBinPath [ curl jq ]}"
set token (cat ${authToken})
if test (id -u) != 0
if test $(id -u) != 0; then
echo 'you must be root to send a notice'
exit 1
end
fi
set url '${homeserver}/rooms/${roomId}/send/m.room.message?access_token='$token
set msg '{"msgtype":"m.text", "body": "'$argv[1]'"}'
${curl}/bin/curl -s -XPOST -d $msg $url
token=$(cat ${authToken})
url="${homeserver}/rooms/${roomId}/send/m.room.message?access_token=$token"
if test $# -eq 1; then
# send first arg as text
msg=$(printf "%s" "$1" | jq -Rsc '{ "msgtype": "m.text", "body": . }')
else
# send stdin formatted as code
msg=$(jq -Rsc '{
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"body": "",
"formatted_body": ("<pre><code>" + . + "</code></pre>")
}')
fi
curl -s "$url" -d "$msg"
''

View File

@ -4,6 +4,17 @@ with lib;
{
systemd.services."notify-failed@" = {
description = "notify that %i has failed";
scriptArgs = "%i";
path = [ pkgs.maxwell-notify ];
script = ''
unit=$1
notify "$unit: failed. last log lines:"
journalctl -u "$unit" -o cat -n 15 | notify
'';
};
systemd.services.ydns = {
description = "update ydns address record";
after = [ "network-online.target" ];
@ -63,8 +74,9 @@ with lib;
in {
description = "system backup";
after = [ "network-online.target" ];
startAt = "*-*-* 03:00"; # every day at 3:00
after = [ "network-online.target" ];
startAt = "*-*-* 03:00"; # every day at 3:00
onFailure = [ "notify-failed@backup.service" ];
serviceConfig = {
Type = "oneshot";
@ -133,6 +145,7 @@ with lib;
description = "update namecoin names";
after = [ "namecoind.service" ];
startAt = "hourly";
onFailure = [ "notify-failed@namecoin-update.service" ];
path = [ pkgs.namecoind ];
serviceConfig.Type = "oneshot";