global-functions: send (and re-send) e-mails from queue

This commit is contained in:
Christian Hesse 2021-02-16 15:20:01 +01:00
parent 17d7678e2d
commit 28db473299
4 changed files with 59 additions and 12 deletions

View file

@ -8,7 +8,7 @@
# Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'!
:global GlobalConfigVersion 42;
:global GlobalConfigVersion 43;
# This is used for DNS and backup file.
:global Domain "example.com";

View file

@ -9,7 +9,7 @@
# Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'!
# Comment or remove to disable change notifications.
:global GlobalConfigVersion 42;
:global GlobalConfigVersion 43;
# Copy configuration from global-config here and modify it.

View file

@ -46,6 +46,7 @@
40="Made the certificate renewal time configurable.";
41="Implemented migration mechanism for script updates.";
42="Made severity in terminal output colorful, with opt-out.";
43="Added queue for e-mail notifications to resend later on error.";
};
# Migration steps to be applied on script updates

View file

@ -8,7 +8,7 @@
# https://git.eworm.de/cgit/routeros-scripts/about/
# expected configuration version
:global ExpectedConfigVersion 42;
:global ExpectedConfigVersion 43;
# global variables not to be changed by user
:global GlobalFunctionsReady false;
@ -24,6 +24,7 @@
:global DeviceInfo;
:global DNSIsResolving;
:global DownloadPackage;
:global FlushEmailQueue;
:global FlushTelegramQueue;
:global GetMacVendor;
:global GetRandom20CharHex;
@ -286,6 +287,47 @@
:return false;
}
# flush e-mail queue
:set FlushEmailQueue do={
:global EmailQueue;
:global LogPrintExit;
:local AllDone true;
:local QueueLen [ :len $EmailQueue ];
:if ([ :len [ / system scheduler find where name="FlushEmailQueue" ] ] > 0 && $QueueLen = 0) do={
$LogPrintExit warning ("Flushing E-Mail messages from scheduler, but queue is empty.") false;
}
/ system scheduler set interval=1m [ find where name="FlushEmailQueue" interval=1s ];
:foreach Id,Message in=$EmailQueue do={
:if ([ :typeof $Message ] = "array" ) do={
/ tool e-mail send to=($Message->"to") cc=($Message->"cc") \
subject=($Message->"subject") body=($Message->"body");
:local Wait true;
:do {
:delay 1s;
:local Status [ / tool e-mail get last-status ];
:if ($Status = "succeeded") do={
:set ($EmailQueue->$Id);
:set Wait false;
}
:if ($Status = "failed") do={
:set AllDone false;
:set Wait false;
}
} while ($Wait = true);
}
}
:if ($AllDone = true && $QueueLen = [ :len $EmailQueue ]) do={
/ system scheduler remove [ find where name="FlushEmailQueue" ];
:set EmailQueue;
}
}
# flush telegram queue
:set FlushTelegramQueue do={
:global TelegramQueue;
@ -777,6 +819,7 @@
:global Identity;
:global EmailGeneralTo;
:global EmailGeneralCc;
:global EmailQueue;
:global LogPrintExit;
:global IfThenElse;
@ -785,15 +828,18 @@
:return false;
}
:do {
:local Signature [ / system note get note ];
/ tool e-mail send to=$EmailGeneralTo cc=$EmailGeneralCc \
subject=("[" . $Identity . "] " . $Subject) \
body=($Message . \
[ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \
[ $IfThenElse ([ :len $Signature ] > 0) ("\n-- \n" . $Signature) "" ]);
} on-error={
$LogPrintExit warning ("Failed sending notification mail!") false;
:if ([ :typeof $EmailQueue ] = "nothing") do={
:set EmailQueue [ :toarray "" ];
}
:local Signature [ / system note get note ];
:set ($EmailQueue->[ :len $EmailQueue ]) {
to=$EmailGeneralTo; cc=$EmailGeneralCc; subject=("[" . $Identity . "] " . $Subject);
body=($Message . \
[ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \
[ $IfThenElse ([ :len $Signature ] > 0) ("\n-- \n" . $Signature) "" ]) };
:if ([ :len [ / system scheduler find where name="FlushEmailQueue" ] ] = 0) do={
/ system scheduler add name=FlushEmailQueue interval=1s start-time=startup \
on-event=":global FlushEmailQueue; \$FlushEmailQueue;";
}
}