Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit bb1edef

Browse files
chore: handle failed download (#943)
1 parent 420a35d commit bb1edef

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

cortex-js/src/infrastructure/services/download-manager/download-manager.service.ts

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class DownloadManagerService {
136136
this.httpService.get(url, {
137137
responseType: 'stream',
138138
signal: controller.signal,
139-
}),
139+
})
140140
);
141141

142142
// check if response is success
@@ -164,6 +164,24 @@ export class DownloadManagerService {
164164

165165
console.log('Downloading', basename(destination));
166166

167+
const timeout = 20000; // Timeout period for receiving new data
168+
let timeoutId: NodeJS.Timeout;
169+
const resetTimeout = () => {
170+
if (timeoutId) clearTimeout(timeoutId);
171+
timeoutId = setTimeout(() => {
172+
try{
173+
this.handleError(
174+
new Error('Download timeout'),
175+
downloadId,
176+
destination,
177+
)
178+
} finally {
179+
bar.stop();
180+
resolve();
181+
}
182+
}, timeout);
183+
};
184+
167185
let transferredBytes = 0;
168186
const bar = new SingleBar({}, Presets.shades_classic);
169187
bar.start(100, 0);
@@ -190,38 +208,17 @@ export class DownloadManagerService {
190208
resolve();
191209
}
192210
});
193-
194211
writer.on('error', (error) => {
195212
try {
196-
delete this.abortControllers[downloadId][destination];
197-
const currentDownloadState = this.allDownloadStates.find(
198-
(downloadState) => downloadState.id === downloadId,
199-
);
200-
if (!currentDownloadState) return;
201-
202-
const downloadItem = currentDownloadState?.children.find(
203-
(downloadItem) => downloadItem.id === destination,
204-
);
205-
if (downloadItem) {
206-
downloadItem.status = DownloadStatus.Error;
207-
downloadItem.error = error.message;
208-
}
209-
210-
currentDownloadState.status = DownloadStatus.Error;
211-
currentDownloadState.error = error.message;
212-
213-
// remove download state if all children is downloaded
214-
this.allDownloadStates = this.allDownloadStates.filter(
215-
(downloadState) => downloadState.id !== downloadId,
216-
);
217-
this.eventEmitter.emit('download.event', this.allDownloadStates);
213+
this.handleError(error, downloadId, destination);
218214
} finally {
219215
bar.stop();
220216
resolve();
221217
}
222218
});
223219

224220
response.data.on('data', (chunk: any) => {
221+
resetTimeout();
225222
transferredBytes += chunk.length;
226223

227224
const currentDownloadState = this.allDownloadStates.find(
@@ -266,4 +263,31 @@ export class DownloadManagerService {
266263
getDownloadStates() {
267264
return this.allDownloadStates;
268265
}
266+
267+
private handleError(error: Error, downloadId: string, destination: string) {
268+
console.log(this.allDownloadStates, downloadId, destination)
269+
delete this.abortControllers[downloadId][destination];
270+
const currentDownloadState = this.allDownloadStates.find(
271+
(downloadState) => downloadState.id === downloadId,
272+
);
273+
if (!currentDownloadState) return;
274+
275+
const downloadItem = currentDownloadState?.children.find(
276+
(downloadItem) => downloadItem.id === destination,
277+
);
278+
if (downloadItem) {
279+
downloadItem.status = DownloadStatus.Error;
280+
downloadItem.error = error.message;
281+
}
282+
283+
currentDownloadState.status = DownloadStatus.Error;
284+
currentDownloadState.error = error.message;
285+
286+
// remove download state if all children is downloaded
287+
this.allDownloadStates = this.allDownloadStates.filter(
288+
(downloadState) => downloadState.id !== downloadId,
289+
);
290+
this.eventEmitter.emit('download.event', [currentDownloadState]);
291+
this.eventEmitter.emit('download.event', this.allDownloadStates);
292+
}
269293
}

cortex-js/src/utils/download-progress.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Presets, SingleBar } from "cli-progress";
22
import { Cortex } from "@cortexso/cortex.js";
33
import { exit, stdin, stdout } from 'node:process';
4-
import { DownloadState, DownloadType } from "@/domain/models/download.interface";
4+
import { DownloadState, DownloadStatus, DownloadType } from "@/domain/models/download.interface";
55

66
export const downloadProgress = async (cortex: Cortex, downloadId?: string, downloadType?: DownloadType) => {
77
const response = await cortex.events.downloadEvent();
@@ -31,7 +31,13 @@ export const downloadProgress = async (cortex: Cortex, downloadId?: string, down
3131
if (!data) continue;
3232
if (downloadType && data.type !== downloadType) continue;
3333

34-
if (data.status === 'downloaded') break;
34+
if (data.status === DownloadStatus.Downloaded) break;
35+
if(data.status === DownloadStatus.Error) {
36+
rl.close();
37+
progressBar.stop();
38+
console.log('\n Download failed: ', data.error);
39+
exit(1);
40+
}
3541

3642
let totalBytes = 0;
3743
let totalTransferred = 0;

0 commit comments

Comments
 (0)