From 7324f2ef54597425c907d0d61d2d0603fa7a5d47 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Tue, 27 Aug 2024 14:31:08 +0700 Subject: [PATCH] Retry for load of config.json (#6397) Signed-off-by: Andrey Sobolev --- dev/prod/src/platform.ts | 5 ++--- packages/presentation/src/utils.ts | 25 ++++++++++++++++++++++ plugins/guest-resources/src/connect.ts | 11 +++++++--- plugins/workbench-resources/src/connect.ts | 3 ++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/dev/prod/src/platform.ts b/dev/prod/src/platform.ts index c8724610ae..0ddfee20db 100644 --- a/dev/prod/src/platform.ts +++ b/dev/prod/src/platform.ts @@ -107,7 +107,7 @@ import github, { githubId } from '@hcengineering/github' import '@hcengineering/github-assets' import { coreId } from '@hcengineering/core' -import presentation, { parsePreviewConfig, presentationId } from '@hcengineering/presentation' +import presentation, { loadServerConfig, parsePreviewConfig, presentationId } from '@hcengineering/presentation' import { setMetadata } from '@hcengineering/platform' import { setDefaultLanguage } from '@hcengineering/theme' @@ -239,13 +239,12 @@ export async function configurePlatform() { }) configureI18n() - const config: Config = await (await fetch( + const config: Config = await loadServerConfig( devConfigHuly ? '/config-huly.json' : ( devConfigBold ? '/config-bold.json' : ( devConfig ? '/config-dev.json' : '/config.json')) ) - ).json() const branding: BrandingMap = config.BRANDING_URL !== undefined ? await (await fetch(config.BRANDING_URL)).json() : {} const myBranding = branding[window.location.host] ?? {} diff --git a/packages/presentation/src/utils.ts b/packages/presentation/src/utils.ts index 5224918cb9..cfa4590862 100644 --- a/packages/presentation/src/utils.ts +++ b/packages/presentation/src/utils.ts @@ -693,3 +693,28 @@ export function setDownloadProgress (percent: number): void { upgradeDownloadProgress.set(Math.round(percent)) } + +export async function loadServerConfig (url: string): Promise { + let retries = 5 + let res: Response | undefined + + do { + try { + res = await fetch(url) + break + } catch (e: any) { + retries-- + if (retries === 0) { + throw new Error(`Failed to load server config: ${e}`) + } + await new Promise((resolve) => setTimeout(resolve, 1000 * (5 - retries))) + } + } while (retries > 0) + + if (res === undefined) { + // In theory should never get here + throw new Error('Failed to load server config') + } + + return await res.json() +} diff --git a/plugins/guest-resources/src/connect.ts b/plugins/guest-resources/src/connect.ts index 53285fc166..0d8c9349e5 100644 --- a/plugins/guest-resources/src/connect.ts +++ b/plugins/guest-resources/src/connect.ts @@ -11,10 +11,15 @@ import core, { } from '@hcengineering/core' import login, { loginId } from '@hcengineering/login' import { getMetadata, getResource, setMetadata } from '@hcengineering/platform' -import presentation, { closeClient, refreshClient, setClient, setPresentationCookie } from '@hcengineering/presentation' +import presentation, { + closeClient, + loadServerConfig, + refreshClient, + setClient, + setPresentationCookie +} from '@hcengineering/presentation' import { fetchMetadataLocalStorage, getCurrentLocation, navigate, setMetadataLocalStorage } from '@hcengineering/ui' import { writable } from 'svelte/store' - export const versionError = writable(undefined) const versionStorageKey = 'last_server_version' @@ -113,7 +118,7 @@ export async function connect (title: string): Promise { const frontUrl = getMetadata(presentation.metadata.FrontUrl) ?? '' const currentFrontVersion = getMetadata(presentation.metadata.FrontVersion) if (currentFrontVersion !== undefined) { - const frontConfig = await (await fetch(concatLink(frontUrl, '/config.json'))).json() + const frontConfig = await loadServerConfig(concatLink(frontUrl, '/config.json')) if (frontConfig?.version !== undefined && frontConfig.version !== currentFrontVersion) { location.reload() } diff --git a/plugins/workbench-resources/src/connect.ts b/plugins/workbench-resources/src/connect.ts index 377ec30b8c..787ce42611 100644 --- a/plugins/workbench-resources/src/connect.ts +++ b/plugins/workbench-resources/src/connect.ts @@ -17,6 +17,7 @@ import login, { loginId } from '@hcengineering/login' import { broadcastEvent, getMetadata, getResource, setMetadata } from '@hcengineering/platform' import presentation, { closeClient, + loadServerConfig, purgeClient, refreshClient, setClient, @@ -221,7 +222,7 @@ export async function connect (title: string): Promise { const frontUrl = getMetadata(presentation.metadata.FrontUrl) ?? '' const currentFrontVersion = getMetadata(presentation.metadata.FrontVersion) if (currentFrontVersion !== undefined) { - const frontConfig = await (await fetch(concatLink(frontUrl, '/config.json'))).json() + const frontConfig = await loadServerConfig(concatLink(frontUrl, '/config.json')) if (frontConfig?.version !== undefined && frontConfig.version !== currentFrontVersion) { location.reload() }