diff --git a/routes/api/task.ts b/routes/api/task.ts index 0d14a13..33ec08c 100644 --- a/routes/api/task.ts +++ b/routes/api/task.ts @@ -10,7 +10,9 @@ export const handler: Handlers = { GET(req, _ctx) { const t = get_task_manager(); const { socket, response } = Deno.upgradeWebSocket(req); - const handle = (e: CustomEvent) => { + const handle = ( + e: CustomEvent, + ) => { socket.send(JSON.stringify({ type: e.type, detail: e.detail })); }; const removeListener = () => { @@ -18,6 +20,7 @@ export const handler: Handlers = { t.removeEventListener("task_started", handle); t.removeEventListener("task_finished", handle); t.removeEventListener("task_progress", handle); + t.removeEventListener("task_error", handle); }; function sendMessage(mes: TaskServerSocketData) { socket.send(JSON.stringify(mes)); @@ -57,6 +60,7 @@ export const handler: Handlers = { t.addEventListener("task_started", handle); t.addEventListener("task_finished", handle); t.addEventListener("task_progress", handle); + t.addEventListener("task_error", handle); }; return response; }, diff --git a/task.ts b/task.ts index 820fec4..f9d04a5 100644 --- a/task.ts +++ b/task.ts @@ -14,6 +14,7 @@ export type Task = { export type TaskDownloadProgess = { downloaded_page: number; + failed_page: number; total_page: number; }; diff --git a/task_manager.ts b/task_manager.ts index 2f50556..91746fc 100644 --- a/task_manager.ts +++ b/task_manager.ts @@ -25,6 +25,7 @@ type EventMap = { task_started: Task; task_finished: Task; task_progress: TaskProgress; + task_error: { task: Task; error: string }; }; type Detail> = { @@ -142,7 +143,13 @@ export class TaskManager extends EventTarget { await this.db.delete_task(status.value); this.dispatchEvent("task_finished", status.value); } else if (status.status == PromiseStatus.Rejected) { - if (status.reason && !this.aborted) console.log(status.reason); + if (status.reason && !this.aborted) { + console.log(status.reason); + this.dispatchEvent("task_error", { + task: task.base, + error: status.reason.toString(), + }); + } removed_task.push(id); } } diff --git a/tasks/download.ts b/tasks/download.ts index 23d78e5..d88bbb9 100644 --- a/tasks/download.ts +++ b/tasks/download.ts @@ -20,7 +20,6 @@ class DownloadManager { #force_abort: AbortSignal; #max_download_count; #running_tasks: Promise[]; - #has_failed_task = false; #progress: TaskDownloadProgess; #task: Task; #manager: TaskManager; @@ -35,7 +34,7 @@ class DownloadManager { this.#running_tasks = []; this.#abort = abort; this.#force_abort = force_abort; - this.#progress = { total_page: 0, downloaded_page: 0 }; + this.#progress = { downloaded_page: 0, failed_page: 0, total_page: 0 }; this.#task = task; this.#manager = manager; } @@ -46,7 +45,8 @@ class DownloadManager { const s = await promiseState(t); if (s.status === PromiseStatus.Rejected) { if (!this.#force_abort.aborted) console.log(s.reason); - this.#has_failed_task = true; + this.#progress.failed_page += 1; + this.#sendEvent(); } else if (s.status === PromiseStatus.Fulfilled) { this.#progress.downloaded_page += 1; this.#sendEvent(); @@ -59,10 +59,7 @@ class DownloadManager { return this.#manager.dispatchTaskProgressEvent( TaskType.Download, this.#task.id, - { - downloaded_page: this.#progress.downloaded_page, - total_page: this.#progress.total_page, - }, + this.#progress, ); } async add_new_task(f: () => Promise) { @@ -77,7 +74,7 @@ class DownloadManager { } } get has_failed_task() { - return this.#has_failed_task; + return this.#progress.failed_page > 0; } async join() { while (1) {