From e9f7427f58ef141beda9584a402a4ed43b7fe074 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Thu, 1 Aug 2024 13:50:02 +0700 Subject: [PATCH] fix: fix issue when re-download aborted model --- .../download-manager/download-manager.service.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts index 0a4ea4018..78e755484 100644 --- a/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts +++ b/cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts @@ -17,6 +17,7 @@ export class DownloadManagerService { private allDownloadStates: DownloadState[] = []; private abortControllers: Record> = {}; + private timeouts: Record = {}; constructor( private readonly httpService: HttpService, @@ -27,11 +28,12 @@ export class DownloadManagerService { if (!this.abortControllers[downloadId]) { return; } + clearTimeout(this.timeouts[downloadId]); Object.keys(this.abortControllers[downloadId]).forEach((destination) => { this.abortControllers[downloadId][destination].abort(); }); delete this.abortControllers[downloadId]; - + const currentDownloadState = this.allDownloadStates.find( (downloadState) => downloadState.id === downloadId, ); @@ -39,8 +41,8 @@ export class DownloadManagerService { (downloadState) => downloadState.id !== downloadId, ); - if (currentDownloadState){ - this.deleteDownloadStateFiles(currentDownloadState); + if (currentDownloadState) { + this.deleteDownloadStateFiles(currentDownloadState); } this.eventEmitter.emit('download.event', this.allDownloadStates); } @@ -175,7 +177,10 @@ export class DownloadManagerService { const timeout = 20000; // Timeout period for receiving new data let timeoutId: NodeJS.Timeout; const resetTimeout = () => { - if (timeoutId) clearTimeout(timeoutId); + if (timeoutId) { + clearTimeout(timeoutId); + delete this.timeouts[downloadId]; + } timeoutId = setTimeout(() => { try { this.handleError( @@ -188,6 +193,7 @@ export class DownloadManagerService { resolve(); } }, timeout); + this.timeouts[downloadId] = timeoutId; }; let transferredBytes = 0; @@ -302,7 +308,7 @@ export class DownloadManagerService { } private deleteDownloadStateFiles(downloadState: DownloadState) { - if(!downloadState.children?.length) return; + if (!downloadState.children?.length) return; downloadState.children.forEach((child) => { unlinkSync(child.id); });