aboutsummaryrefslogtreecommitdiffstats
path: root/telegram-chat.rsc
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2024-01-29 21:50:32 +0100
committerGravatar Christian Hesse <mail@eworm.de>2024-01-30 00:02:08 +0100
commitabfc8e9191b23731c2ff8977cf7a46c9ca34a62b (patch)
tree72a8de44ba5c484f1b07a13cc367261cfb5193c7 /telegram-chat.rsc
parentb6367b5df35a23365c8a2086e8f90a372845db4c (diff)
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...
Diffstat (limited to 'telegram-chat.rsc')
-rw-r--r--telegram-chat.rsc11
1 files changed, 11 insertions, 0 deletions
diff --git a/telegram-chat.rsc b/telegram-chat.rsc
index 292ae5b..986df03 100644
--- a/telegram-chat.rsc
+++ b/telegram-chat.rsc
@@ -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");
}
}