diff --git a/apps/api/src/sports/providers.ts b/apps/api/src/sports/providers.ts index 6d50ede..de9c0ff 100644 --- a/apps/api/src/sports/providers.ts +++ b/apps/api/src/sports/providers.ts @@ -70,8 +70,8 @@ export interface OfficialData { const TIMEOUT_MS = 20_000; -async function get(fetcher: Fetch, url: string, headers: Record, as: "json" | "text") { - const res = await fetcher(url, { headers, signal: AbortSignal.timeout(TIMEOUT_MS) }); +async function get(fetcher: Fetch, url: string, headers: Record, as: "json" | "text", timeoutMs = TIMEOUT_MS) { + const res = await fetcher(url, { headers, signal: AbortSignal.timeout(timeoutMs) }); if (!res.ok) throw new Error(`${new URL(url).host} ${new URL(url).pathname} ${res.status}`); return as === "json" ? res.json() : res.text(); } @@ -130,7 +130,8 @@ export class LigaProvider { private async api(path: string, params: Record): Promise { const u = new URL(`${LigaProvider.BASE}/${path}`); for (const [k, v] of Object.entries(params)) u.searchParams.set(k, v); - return get(this.fetcher, u.toString(), LigaProvider.HEADERS, "json"); + // The calendar is slow from the archive host. A short timeout used to abort a whole season after one month. + return get(this.fetcher, u.toString(), LigaProvider.HEADERS, "json", 45_000); } async teams(competition: string, season: string): Promise { diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index c68bd06..8894597 100644 --- a/apps/api/src/sports/service.ts +++ b/apps/api/src/sports/service.ts @@ -164,9 +164,19 @@ export class SportsService { const asked = season.replace(/\D/g, ""); let saved = 0; for (const month of [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]) { - for (const f of await this.liga.month(month, season)) { - // Only a season Liga itself named. A fixture id is updated in place, so guessing the season we asked - // for would move another year's game onto this one. + // One slow month must not drop the rest of the year. It is tried twice; the months already read stay. + let fixtures: Awaited> = []; + for (let attempt = 0; attempt < 2; attempt++) { + try { + fixtures = await this.liga.month(month, season); + break; + } catch (err) { + fixtures = []; + this.deps.log.warn({ err, season, month, attempt }, "sports: liga month not read"); + } + } + for (const f of fixtures) { + // Only a season Liga itself named. The fixture id includes that season, so another year is another row. if (!f.seasonNamed) continue; const theirs = f.season.replace(/\D/g, ""); if (theirs !== asked) continue;