jobs: notify on failure

This commit is contained in:
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 , homeserver
, roomId , roomId
, authToken , authToken
}: }:
writeScriptBin "notify" '' writers.writeDashBin "notify" ''
#!${fish}/bin/fish export PATH="$PATH:${lib.makeBinPath [ curl jq ]}"
set token (cat ${authToken}) if test $(id -u) != 0; then
if test (id -u) != 0
echo 'you must be root to send a notice' echo 'you must be root to send a notice'
exit 1 exit 1
end fi
set url '${homeserver}/rooms/${roomId}/send/m.room.message?access_token='$token token=$(cat ${authToken})
set msg '{"msgtype":"m.text", "body": "'$argv[1]'"}' url="${homeserver}/rooms/${roomId}/send/m.room.message?access_token=$token"
${curl}/bin/curl -s -XPOST -d $msg $url
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 = { systemd.services.ydns = {
description = "update ydns address record"; description = "update ydns address record";
after = [ "network-online.target" ]; after = [ "network-online.target" ];
@ -63,8 +74,9 @@ with lib;
in { in {
description = "system backup"; description = "system backup";
after = [ "network-online.target" ]; after = [ "network-online.target" ];
startAt = "*-*-* 03:00"; # every day at 3:00 startAt = "*-*-* 03:00"; # every day at 3:00
onFailure = [ "notify-failed@backup.service" ];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
@ -133,6 +145,7 @@ with lib;
description = "update namecoin names"; description = "update namecoin names";
after = [ "namecoind.service" ]; after = [ "namecoind.service" ];
startAt = "hourly"; startAt = "hourly";
onFailure = [ "notify-failed@namecoin-update.service" ];
path = [ pkgs.namecoind ]; path = [ pkgs.namecoind ];
serviceConfig.Type = "oneshot"; serviceConfig.Type = "oneshot";