Spaces:
Sleeping
Sleeping
fix: keep core sync successful when enrichment fetch fails
Browse files
scripts/worldcup/sync-worldcup-data.mjs
CHANGED
|
@@ -311,7 +311,13 @@ function closestHourlyIndex(times, kickoffIso) {
|
|
| 311 |
}
|
| 312 |
|
| 313 |
async function fetchJson(url, options = {}) {
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
if (!response.ok) {
|
| 316 |
const text = await response.text().catch(() => '');
|
| 317 |
throw new Error(`HTTP ${response.status} ${response.statusText} for ${url}: ${text.slice(0, 300)}`);
|
|
@@ -319,6 +325,13 @@ async function fetchJson(url, options = {}) {
|
|
| 319 |
return response.json();
|
| 320 |
}
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
async function fetchWeatherSnapshots(pool) {
|
| 323 |
const sourceId = await ensureNamedSource(pool, {
|
| 324 |
source_name: 'open-meteo',
|
|
@@ -695,22 +708,38 @@ async function main() {
|
|
| 695 |
console.log(`[${item.key}] fetched=${result.fetched} inserted=${result.inserted} updated=${result.updated}`);
|
| 696 |
}
|
| 697 |
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
inserted:
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
}
|
| 713 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 714 |
|
| 715 |
await finishRun(pool, runId, 'success', payload);
|
| 716 |
});
|
|
|
|
| 311 |
}
|
| 312 |
|
| 313 |
async function fetchJson(url, options = {}) {
|
| 314 |
+
let response;
|
| 315 |
+
try {
|
| 316 |
+
response = await fetch(url, options);
|
| 317 |
+
} catch (error) {
|
| 318 |
+
const cause = error.cause ? ` cause=${error.cause.code || error.cause.message || String(error.cause)}` : '';
|
| 319 |
+
throw new Error(`Fetch failed for ${url}: ${error.message || String(error)}${cause}`);
|
| 320 |
+
}
|
| 321 |
if (!response.ok) {
|
| 322 |
const text = await response.text().catch(() => '');
|
| 323 |
throw new Error(`HTTP ${response.status} ${response.statusText} for ${url}: ${text.slice(0, 300)}`);
|
|
|
|
| 325 |
return response.json();
|
| 326 |
}
|
| 327 |
|
| 328 |
+
function serializeError(error) {
|
| 329 |
+
return {
|
| 330 |
+
message: error.message || String(error),
|
| 331 |
+
cause: error.cause ? (error.cause.code || error.cause.message || String(error.cause)) : null,
|
| 332 |
+
};
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
async function fetchWeatherSnapshots(pool) {
|
| 336 |
const sourceId = await ensureNamedSource(pool, {
|
| 337 |
source_name: 'open-meteo',
|
|
|
|
| 708 |
console.log(`[${item.key}] fetched=${result.fetched} inserted=${result.inserted} updated=${result.updated}`);
|
| 709 |
}
|
| 710 |
|
| 711 |
+
try {
|
| 712 |
+
const weatherResult = await fetchWeatherSnapshots(pool);
|
| 713 |
+
payload.recordsFetched.weather = weatherResult.fetched;
|
| 714 |
+
payload.recordsUpserted.weather = {
|
| 715 |
+
inserted: weatherResult.inserted,
|
| 716 |
+
updated: weatherResult.updated,
|
| 717 |
+
};
|
| 718 |
+
console.log(`[weather] fetched=${weatherResult.fetched} inserted=${weatherResult.inserted} updated=${weatherResult.updated}`);
|
| 719 |
+
} catch (error) {
|
| 720 |
+
const serialized = serializeError(error);
|
| 721 |
+
payload.recordsFetched.weather = 0;
|
| 722 |
+
payload.recordsUpserted.weather = { inserted: 0, updated: 0, error: serialized.message, cause: serialized.cause };
|
| 723 |
+
payload.logText += `\n[weather warning]\n${error.stack || serialized.message}`;
|
| 724 |
+
console.warn(`[weather] skipped error=${serialized.message}`);
|
| 725 |
+
}
|
| 726 |
+
|
| 727 |
+
try {
|
| 728 |
+
const oddsResult = await fetchOddsSnapshots(pool);
|
| 729 |
+
payload.recordsFetched.odds = oddsResult.fetched;
|
| 730 |
+
payload.recordsUpserted.odds = {
|
| 731 |
+
inserted: oddsResult.inserted,
|
| 732 |
+
updated: oddsResult.updated,
|
| 733 |
+
skipped: oddsResult.skipped || false,
|
| 734 |
+
};
|
| 735 |
+
console.log(`[odds] fetched=${oddsResult.fetched} inserted=${oddsResult.inserted} updated=${oddsResult.updated}${oddsResult.skipped ? ' skipped=true' : ''}`);
|
| 736 |
+
} catch (error) {
|
| 737 |
+
const serialized = serializeError(error);
|
| 738 |
+
payload.recordsFetched.odds = 0;
|
| 739 |
+
payload.recordsUpserted.odds = { inserted: 0, updated: 0, error: serialized.message, cause: serialized.cause };
|
| 740 |
+
payload.logText += `\n[odds warning]\n${error.stack || serialized.message}`;
|
| 741 |
+
console.warn(`[odds] skipped error=${serialized.message}`);
|
| 742 |
+
}
|
| 743 |
|
| 744 |
await finishRun(pool, runId, 'success', payload);
|
| 745 |
});
|