From 0efec3b0cd510d416d908d5016f2caee12a0fc6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20X=2E=20F=C3=BCrst?= Date: Tue, 19 May 2026 10:32:50 +0200 Subject: [PATCH] =?UTF-8?q?V=201.2.2=20=20Retry=20bei=20429=20f=C3=BCr=20A?= =?UTF-8?q?rchive-POSTs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- wetter.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/wetter.js b/wetter.js index baf90a9..c9f9855 100644 --- a/wetter.js +++ b/wetter.js @@ -30,18 +30,30 @@ function log(msg) { console.log (`[${fmt24h(new Date())}] ${msg}`); } function warn(msg) { console.warn(`[${fmt24h(new Date())}] WARN ${msg}`); } function err(msg) { console.error(`[${fmt24h(new Date())}] ERROR ${msg}`); } -function postData(data, source) { +async function postData(data, source) { if (!POST_URL) return; const headers = { "Content-Type": "application/json" }; if (COLLECTOR_API_KEY) headers["X-API-Key"] = COLLECTOR_API_KEY; - fetch(POST_URL, { method: "POST", headers, body: JSON.stringify({ ...data, source }) }) - .then(async res => { - if (!res.ok) { - const text = await res.text().catch(() => ""); - warn(`POST ${res.status} ${res.statusText}: ${text.slice(0, 200)}`); - } - }) - .catch(e => warn("POST fehlgeschlagen: " + e.message)); + const body = JSON.stringify({ ...data, source }); + + for (let attempt = 1; attempt <= 5; attempt++) { + let res; + try { + res = await fetch(POST_URL, { method: "POST", headers, body }); + } catch (e) { + warn("POST fehlgeschlagen: " + e.message); + return; + } + if (res.ok) return; + if (res.status === 429 && attempt < 5) { + const delay = Number(res.headers.get("Retry-After") ?? 2) * 1000; + await new Promise(r => setTimeout(r, delay)); + continue; + } + const text = await res.text().catch(() => ""); + warn(`POST ${res.status} ${res.statusText}: ${text.slice(0, 200)}`); + return; + } } // ── 5-Minuten-Aggregation ────────────────────────────────────────────────── @@ -106,7 +118,7 @@ async function catchUpArchive(db) { const inserted = insertRecords(db, records, "archive"); log(`Archiv: ${inserted} neue Datensätze gespeichert (${records.length} empfangen).`); - for (const r of records) postData(r, "archive"); + for (const r of records) await postData(r, "archive"); } // ── LOOP-Schleife ──────────────────────────────────────────────────────────