From b3ea7c258a4237c88db0119b669014843d17ab0f Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Tue, 23 Apr 2024 15:26:05 +0530 Subject: [PATCH] SDA-4489 - Invoke check for update & download before force install (#2132) --- src/app/auto-update-handler.ts | 38 ++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/app/auto-update-handler.ts b/src/app/auto-update-handler.ts index 555963bd..0794ecf9 100644 --- a/src/app/auto-update-handler.ts +++ b/src/app/auto-update-handler.ts @@ -36,8 +36,8 @@ export enum UpdateChannel { } const DOWNLOAD_PROGRESS_BANNER_DELAY = 1000 * 10; // 10 sec - const AUTO_UPDATE_REASON = 'autoUpdate'; +const FORCE_UPDATE_TIMEOUT = 1000 * 10; // 10 sec export class AutoUpdate { public isUpdateAvailable: boolean = false; @@ -49,6 +49,7 @@ export class AutoUpdate { private channelConfigLocation: ChannelConfigLocation = ChannelConfigLocation.LOCALFILE; private downloadProgressDelayTimer: NodeJS.Timeout | null = null; + private isForceUpdate: boolean = false; constructor() { this.getGenericServerOptions().then((opts) => { @@ -89,6 +90,14 @@ export class AutoUpdate { await this.updateEventHandler(info, 'download-progress'); }); this.autoUpdater.on('update-downloaded', async (info) => { + if (this.isForceUpdate) { + this.isForceUpdate = false; + logger.info( + 'auto-update-handler: update downloaded and isForceUpdate', + ); + this.autoUpdater?.quitAndInstall(); + return; + } await this.updateEventHandler(info, 'update-downloaded'); }); @@ -152,8 +161,9 @@ export class AutoUpdate { ); if (hasPendingInstaller) { logger.info('auto-update-handler: latest version found force installing'); - this.isUpdateAvailable = true; - await this.updateAndRestart(); + this.isForceUpdate = true; + await this.checkUpdates(AutoUpdateTrigger.AUTOMATED); + await this.downloadUpdate(); } }; @@ -178,6 +188,7 @@ export class AutoUpdate { if (isMac) { config.backupGlobalConfig(); } + logger.info('auto-update-handler: quitAndInstall'); this.autoUpdater.quitAndInstall(); } }); @@ -255,10 +266,25 @@ export class AutoUpdate { await this.setAutoUpdateChannel(); return new Promise((resolve) => { const url = this.getUpdateUrl(); - fetch(url) + const { autoUpdateChannel } = config.getConfigFields([ + 'autoUpdateChannel', + ]); + const endpoint = `${url}/${autoUpdateChannel}.yml`; + logger.info( + 'auto-update-handler: fetching latest version info from', + endpoint, + ); + const controller = new AbortController(); + const signal = controller.signal; + const timeoutId = setTimeout( + () => controller.abort(), + FORCE_UPDATE_TIMEOUT, + ); + fetch(endpoint, { signal }) .then((res) => res.blob()) .then((blob) => blob.text()) .then(async (response) => { + clearTimeout(timeoutId); logger.info( 'auto-update-handler: latest version info from server', response, @@ -276,11 +302,15 @@ export class AutoUpdate { error, ); resolve(); + }) + .finally(() => { + clearTimeout(timeoutId); }); }); }; private updateEventHandler = async (info, eventType: string) => { + logger.info('auto-update-handler: auto update events', info, eventType); const mainWebContents = windowHandler.mainWebContents; if (mainWebContents && !mainWebContents.isDestroyed()) { await this.setAutoUpdateChannel();