From c9b47031ef0f56fa9f9da00a7bebeca307e31b49 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Thu, 14 Sep 2023 19:38:05 +0700 Subject: [PATCH] UBER-854: More proper upgrade notification Signed-off-by: Andrey Sobolev --- plugins/workbench-resources/src/connect.ts | 47 +++++++++++++++++++--- server/ws/src/server_http.ts | 9 +++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/plugins/workbench-resources/src/connect.ts b/plugins/workbench-resources/src/connect.ts index a13a3a04269..3efd9373eb2 100644 --- a/plugins/workbench-resources/src/connect.ts +++ b/plugins/workbench-resources/src/connect.ts @@ -97,6 +97,15 @@ export async function connect (title: string): Promise { // It seems upgrade happened location.reload() } + const serverVersion: { version: string } = await (await fetch(serverEndpoint + '/api/v1/version', {})).json() + + console.log('Server version', serverVersion.version) + if (serverVersion.version !== '' && serverVersion.version !== currentVersionStr) { + versionError = `${currentVersionStr} => ${serverVersion.version}` + setTimeout(() => { + window.location.reload() + }, 5000) + } })() } catch (err) { console.error(err) @@ -123,6 +132,11 @@ export async function connect (title: string): Promise { return } + let serverEndpoint = endpoint.replace(/^ws/g, 'http') + if (serverEndpoint.endsWith('/')) { + serverEndpoint = serverEndpoint.substring(0, serverEndpoint.length - 1) + } + try { version = await _client.findOne(core.class.Version, {}) console.log('Model version', version) @@ -134,9 +148,36 @@ export async function connect (title: string): Promise { if (version === undefined || requiredVersion !== versionStr) { versionError = `${versionStr} => ${requiredVersion}` + setTimeout(() => { + window.location.reload() + }, 5000) return undefined } } + + try { + const serverVersion: { version: string } = await (await fetch(serverEndpoint + '/api/v1/version', {})).json() + + console.log('Server version', serverVersion.version) + if ( + serverVersion.version !== '' && + (version === undefined || serverVersion.version !== versionToString(version)) + ) { + const versionStr = version !== undefined ? versionToString(version) : 'unknown' + versionError = `${versionStr} => ${serverVersion.version}` + + setTimeout(() => { + window.location.reload() + }, 5000) + return + } + } catch (err: any) { + versionError = 'server version not available' + setTimeout(() => { + window.location.reload() + }, 5000) + return + } } catch (err: any) { console.log(err) const requirdVersion = getMetadata(presentation.metadata.RequiredVersion) @@ -152,10 +193,6 @@ export async function connect (title: string): Promise { await setClient(_client) if (me.role === AccountRole.Owner) { - let ep = endpoint.replace(/^ws/g, 'http') - if (ep.endsWith('/')) { - ep = ep.substring(0, ep.length - 1) - } setMetadata(ui.metadata.ShowNetwork, (evt: MouseEvent) => { if (getMetadata(presentation.metadata.Token) == null) { return @@ -164,7 +201,7 @@ export async function connect (title: string): Promise { showPopup( ServerManager, { - endpoint: ep, + endpoint: serverEndpoint, token }, 'content' diff --git a/server/ws/src/server_http.ts b/server/ws/src/server_http.ts index 024e27b7d26..9efb5f4ab61 100644 --- a/server/ws/src/server_http.ts +++ b/server/ws/src/server_http.ts @@ -61,6 +61,15 @@ export function startHttpServer ( const getUsers = (): any => Array.from(sessions.sessions.entries()).map(([k, v]) => v.session.getUser()) + app.get('/api/v1/version', (req, res) => { + res.writeHead(200, { 'Content-Type': 'application/json' }) + res.end( + JSON.stringify({ + version: process.env.MODEL_VERSION + }) + ) + }) + app.get('/api/v1/statistics', (req, res) => { try { const token = req.query.token as string