telegram-chat: add (and increase) random delay on error

Chances are that two devices schedule the script (nearly) simultaneously.
Causing a lot of failures from fetch. Instead of forcing a retry on
*every* invocating we add (and increase) a random delay, that is
slowly decreasd on success. This should minimize failures...
This commit is contained in:
Christian Hesse 2024-01-29 21:50:32 +01:00
parent b6367b5df3
commit abfc8e9191

View file

@ -18,6 +18,7 @@
:global TelegramChatOffset;
:global TelegramChatRunTime;
:global TelegramMessageIDs;
:global TelegramRandomDelay;
:global TelegramTokenId;
:global CertificateAvailable;
@ -26,8 +27,11 @@
:global GetRandom20CharAlNum;
:global IfThenElse;
:global LogPrintExit2;
:global MAX;
:global MIN;
:global MkDir;
:global ParseJson;
:global RandomDelay;
:global ScriptLock;
:global SendTelegram2;
:global SymbolForNotification;
@ -42,11 +46,16 @@ $WaitFullyConnected;
:if ([ :typeof $TelegramChatOffset ] != "array") do={
:set TelegramChatOffset { 0; 0; 0 };
}
:if ([ :typeof $TelegramRandomDelay ] != "num") do={
:set TelegramRandomDelay 0;
}
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
$LogPrintExit2 warning $0 ("Downloading required certificate failed.") true;
}
$RandomDelay $TelegramRandomDelay;
:local Data false;
:for I from=1 to=4 do={
:if ($Data = false) do={
@ -54,9 +63,11 @@ $WaitFullyConnected;
:set Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
("https://api.telegram.org/bot" . $TelegramTokenId . "/getUpdates?offset=" . \
$TelegramChatOffset->0 . "&allowed_updates=%5B%22message%22%5D") as-value ]->"data");
:set TelegramRandomDelay [ $MAX 0 ($TelegramRandomDelay - 1) ];
} on-error={
:if ($I < 4) do={
$LogPrintExit2 debug $0 ("Fetch failed, " . $I . ". try.") false;
:set TelegramRandomDelay [ $MIN 15 ($TelegramRandomDelay + 5) ];
:delay (($I * $I) . "s");
}
}