Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: handle failed download #943

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class DownloadManagerService {
this.httpService.get(url, {
responseType: 'stream',
signal: controller.signal,
}),
})
);

// check if response is success
Expand Down Expand Up @@ -164,6 +164,24 @@ export class DownloadManagerService {

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

const timeout = 20000; // Timeout period for receiving new data
let timeoutId: NodeJS.Timeout;
const resetTimeout = () => {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
try{
this.handleError(
new Error('Download timeout'),
downloadId,
destination,
)
} finally {
bar.stop();
resolve();
}
}, timeout);
};

let transferredBytes = 0;
const bar = new SingleBar({}, Presets.shades_classic);
bar.start(100, 0);
Expand All @@ -190,38 +208,17 @@ export class DownloadManagerService {
resolve();
}
});

writer.on('error', (error) => {
try {
delete this.abortControllers[downloadId][destination];
const currentDownloadState = this.allDownloadStates.find(
(downloadState) => downloadState.id === downloadId,
);
if (!currentDownloadState) return;

const downloadItem = currentDownloadState?.children.find(
(downloadItem) => downloadItem.id === destination,
);
if (downloadItem) {
downloadItem.status = DownloadStatus.Error;
downloadItem.error = error.message;
}

currentDownloadState.status = DownloadStatus.Error;
currentDownloadState.error = error.message;

// remove download state if all children is downloaded
this.allDownloadStates = this.allDownloadStates.filter(
(downloadState) => downloadState.id !== downloadId,
);
this.eventEmitter.emit('download.event', this.allDownloadStates);
this.handleError(error, downloadId, destination);
} finally {
bar.stop();
resolve();
}
});

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

const currentDownloadState = this.allDownloadStates.find(
Expand Down Expand Up @@ -266,4 +263,31 @@ export class DownloadManagerService {
getDownloadStates() {
return this.allDownloadStates;
}

private handleError(error: Error, downloadId: string, destination: string) {
console.log(this.allDownloadStates, downloadId, destination)
delete this.abortControllers[downloadId][destination];
const currentDownloadState = this.allDownloadStates.find(
(downloadState) => downloadState.id === downloadId,
);
if (!currentDownloadState) return;

const downloadItem = currentDownloadState?.children.find(
(downloadItem) => downloadItem.id === destination,
);
if (downloadItem) {
downloadItem.status = DownloadStatus.Error;
downloadItem.error = error.message;
}

currentDownloadState.status = DownloadStatus.Error;
currentDownloadState.error = error.message;

// remove download state if all children is downloaded
this.allDownloadStates = this.allDownloadStates.filter(
(downloadState) => downloadState.id !== downloadId,
);
this.eventEmitter.emit('download.event', [currentDownloadState]);
this.eventEmitter.emit('download.event', this.allDownloadStates);
}
}
10 changes: 8 additions & 2 deletions cortex-js/src/utils/download-progress.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Presets, SingleBar } from "cli-progress";
import { Cortex } from "@cortexso/cortex.js";
import { exit, stdin, stdout } from 'node:process';
import { DownloadState, DownloadType } from "@/domain/models/download.interface";
import { DownloadState, DownloadStatus, DownloadType } from "@/domain/models/download.interface";

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

if (data.status === 'downloaded') break;
if (data.status === DownloadStatus.Downloaded) break;
if(data.status === DownloadStatus.Error) {
rl.close();
progressBar.stop();
console.log('\n Download failed: ', data.error);
exit(1);
}

let totalBytes = 0;
let totalTransferred = 0;
Expand Down
Loading