From 167b64f5f1b0d1db2df35fb6b8a7d86c125b09de Mon Sep 17 00:00:00 2001 From: Theo Sanderson Date: Sun, 26 May 2024 20:31:23 +0100 Subject: [PATCH] handle lapis timeouts --- .../src/components/IndexPage/getOrganismStatistics.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/website/src/components/IndexPage/getOrganismStatistics.ts b/website/src/components/IndexPage/getOrganismStatistics.ts index 2743dc501..e8d106bc6 100644 --- a/website/src/components/IndexPage/getOrganismStatistics.ts +++ b/website/src/components/IndexPage/getOrganismStatistics.ts @@ -10,6 +10,8 @@ export type OrganismStatistics = { }; type OrganismStatisticsMap = Map; +const TIMEOUT_MS = 500; + export const getOrganismStatisticsMap = async ( organismNames: string[], numberDaysAgo: number, @@ -26,8 +28,8 @@ export const getOrganismStatisticsMap = async ( const getOrganismStatistics = async (organism: string, numberDaysAgo: number): Promise => { const [{ total, lastUpdatedAt }, recent] = await Promise.all([ - getTotalAndLastUpdatedAt(organism), - getRecent(organism, numberDaysAgo), + withTimeout(getTotalAndLastUpdatedAt(organism), TIMEOUT_MS, { total: -1, lastUpdatedAt: undefined }), + withTimeout(getRecent(organism, numberDaysAgo), TIMEOUT_MS, 0), ]); return { totalSequences: total, @@ -36,6 +38,11 @@ const getOrganismStatistics = async (organism: string, numberDaysAgo: number): P }; }; +const withTimeout = (promise: Promise, ms: number, defaultValue: T): Promise => { + const timeout = new Promise((resolve) => setTimeout(() => resolve(defaultValue), ms)); + return Promise.race([promise, timeout]); +}; + const getTotalAndLastUpdatedAt = async ( organism: string, ): Promise<{ total: number; lastUpdatedAt: DateTime | undefined }> => {