This commit is contained in:
2023-06-11 08:45:06 +08:00
parent dcf59bb995
commit 28387eda1c
4 changed files with 19 additions and 10 deletions

View File

@@ -10,7 +10,9 @@ export const handler: Handlers<Task[]> = {
GET(req, _ctx) {
const t = get_task_manager();
const { socket, response } = Deno.upgradeWebSocket(req);
const handle = (e: CustomEvent<Task | TaskProgress>) => {
const handle = (
e: CustomEvent<Task | TaskProgress | { task: Task; error: string }>,
) => {
socket.send(JSON.stringify({ type: e.type, detail: e.detail }));
};
const removeListener = () => {
@@ -18,6 +20,7 @@ export const handler: Handlers<Task[]> = {
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<Task[]> = {
t.addEventListener("task_started", handle);
t.addEventListener("task_finished", handle);
t.addEventListener("task_progress", handle);
t.addEventListener("task_error", handle);
};
return response;
},

View File

@@ -14,6 +14,7 @@ export type Task<T extends TaskType = TaskType> = {
export type TaskDownloadProgess = {
downloaded_page: number;
failed_page: number;
total_page: number;
};

View File

@@ -25,6 +25,7 @@ type EventMap = {
task_started: Task;
task_finished: Task;
task_progress: TaskProgress;
task_error: { task: Task; error: string };
};
type Detail<T extends Record<PropertyKey, unknown>> = {
@@ -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);
}
}

View File

@@ -20,7 +20,6 @@ class DownloadManager {
#force_abort: AbortSignal;
#max_download_count;
#running_tasks: Promise<unknown>[];
#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<unknown>) {
@@ -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) {