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

View file

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

View file

@ -46,6 +46,7 @@
40="Made the certificate renewal time configurable."; 40="Made the certificate renewal time configurable.";
41="Implemented migration mechanism for script updates."; 41="Implemented migration mechanism for script updates.";
42="Made severity in terminal output colorful, with opt-out."; 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 # Migration steps to be applied on script updates

View file

@ -8,7 +8,7 @@
# https://git.eworm.de/cgit/routeros-scripts/about/ # https://git.eworm.de/cgit/routeros-scripts/about/
# expected configuration version # expected configuration version
:global ExpectedConfigVersion 42; :global ExpectedConfigVersion 43;
# global variables not to be changed by user # global variables not to be changed by user
:global GlobalFunctionsReady false; :global GlobalFunctionsReady false;
@ -24,6 +24,7 @@
:global DeviceInfo; :global DeviceInfo;
:global DNSIsResolving; :global DNSIsResolving;
:global DownloadPackage; :global DownloadPackage;
:global FlushEmailQueue;
:global FlushTelegramQueue; :global FlushTelegramQueue;
:global GetMacVendor; :global GetMacVendor;
:global GetRandom20CharHex; :global GetRandom20CharHex;
@ -286,6 +287,47 @@
:return false; :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 # flush telegram queue
:set FlushTelegramQueue do={ :set FlushTelegramQueue do={
:global TelegramQueue; :global TelegramQueue;
@ -777,6 +819,7 @@
:global Identity; :global Identity;
:global EmailGeneralTo; :global EmailGeneralTo;
:global EmailGeneralCc; :global EmailGeneralCc;
:global EmailQueue;
:global LogPrintExit; :global LogPrintExit;
:global IfThenElse; :global IfThenElse;
@ -785,15 +828,18 @@
:return false; :return false;
} }
:do { :if ([ :typeof $EmailQueue ] = "nothing") do={
:local Signature [ / system note get note ]; :set EmailQueue [ :toarray "" ];
/ tool e-mail send to=$EmailGeneralTo cc=$EmailGeneralCc \ }
subject=("[" . $Identity . "] " . $Subject) \ :local Signature [ / system note get note ];
body=($Message . \ :set ($EmailQueue->[ :len $EmailQueue ]) {
[ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \ to=$EmailGeneralTo; cc=$EmailGeneralCc; subject=("[" . $Identity . "] " . $Subject);
[ $IfThenElse ([ :len $Signature ] > 0) ("\n-- \n" . $Signature) "" ]); body=($Message . \
} on-error={ [ $IfThenElse ([ :len $Link ] > 0) ("\n\n" . $Link) "" ] . \
$LogPrintExit warning ("Failed sending notification mail!") false; [ $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;";
} }
} }