V 1.2.2 Retry bei 429 für Archive-POSTs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-19 10:32:50 +02:00
parent 4798e5f9c9
commit 0efec3b0cd
+22 -10
View File
@@ -30,18 +30,30 @@ function log(msg) { console.log (`[${fmt24h(new Date())}] ${msg}`); }
function warn(msg) { console.warn(`[${fmt24h(new Date())}] WARN ${msg}`); } function warn(msg) { console.warn(`[${fmt24h(new Date())}] WARN ${msg}`); }
function err(msg) { console.error(`[${fmt24h(new Date())}] ERROR ${msg}`); } function err(msg) { console.error(`[${fmt24h(new Date())}] ERROR ${msg}`); }
function postData(data, source) { async function postData(data, source) {
if (!POST_URL) return; if (!POST_URL) return;
const headers = { "Content-Type": "application/json" }; const headers = { "Content-Type": "application/json" };
if (COLLECTOR_API_KEY) headers["X-API-Key"] = COLLECTOR_API_KEY; if (COLLECTOR_API_KEY) headers["X-API-Key"] = COLLECTOR_API_KEY;
fetch(POST_URL, { method: "POST", headers, body: JSON.stringify({ ...data, source }) }) const body = JSON.stringify({ ...data, source });
.then(async res => {
if (!res.ok) { for (let attempt = 1; attempt <= 5; attempt++) {
const text = await res.text().catch(() => ""); let res;
warn(`POST ${res.status} ${res.statusText}: ${text.slice(0, 200)}`); try {
} res = await fetch(POST_URL, { method: "POST", headers, body });
}) } catch (e) {
.catch(e => warn("POST fehlgeschlagen: " + e.message)); 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 ────────────────────────────────────────────────── // ── 5-Minuten-Aggregation ──────────────────────────────────────────────────
@@ -106,7 +118,7 @@ async function catchUpArchive(db) {
const inserted = insertRecords(db, records, "archive"); const inserted = insertRecords(db, records, "archive");
log(`Archiv: ${inserted} neue Datensätze gespeichert (${records.length} empfangen).`); 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 ────────────────────────────────────────────────────────── // ── LOOP-Schleife ──────────────────────────────────────────────────────────