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:
@@ -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 ──────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user