1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2025-02-22 14:00:52 +00:00

Fix issues when trying to create background tasks

This commit is contained in:
LASER-Yi 2022-03-19 11:54:24 +08:00
parent af4af41a23
commit c1a26fd9eb
No known key found for this signature in database
GPG key ID: BB28903D50A1D408
3 changed files with 29 additions and 13 deletions

View file

@ -9,7 +9,7 @@ class TaskManager {
private onBeforeUnload(e: BeforeUnloadEvent) { private onBeforeUnload(e: BeforeUnloadEvent) {
const message = "Background tasks are still running"; const message = "Background tasks are still running";
if (this.tasks.some((t) => t.status !== "success")) { if (this.tasks.some((t) => t.status === "running")) {
e.preventDefault(); e.preventDefault();
e.returnValue = message; e.returnValue = message;
return; return;
@ -17,22 +17,29 @@ class TaskManager {
delete e["returnValue"]; delete e["returnValue"];
} }
private generateUniqueId(): string { private findTask(ref: Task.TaskRef): Task.Callable | undefined {
return "TODO-TODO"; return this.tasks.find((t) => t.id === ref.callableId);
} }
create<T extends Task.AnyCallable>(fn: T, parameters: Parameters<T>): string { create<T extends Task.AnyCallable>(fn: T, parameters: Parameters<T>): symbol {
const newTask = fn as Task.Callable<T>; // Clone this function
const newTask = fn.bind({}) as Task.Callable<T>;
newTask.status = "idle"; newTask.status = "idle";
newTask.parameters = parameters; newTask.parameters = parameters;
newTask.id = this.generateUniqueId(); newTask.id = Symbol(this.tasks.length);
this.tasks.push(newTask); this.tasks.push(newTask);
return newTask.id; return newTask.id;
} }
async run(task: Task.Callable) { async run(taskRef: Task.TaskRef) {
const task = this.findTask(taskRef);
if (task === undefined) {
throw new Error(`Task ${taskRef.name} not found`);
}
if (task.status !== "idle") { if (task.status !== "idle") {
return; return;
} }
@ -44,6 +51,7 @@ class TaskManager {
task.status = "success"; task.status = "success";
} catch (err) { } catch (err) {
task.status = "failure"; task.status = "failure";
throw err;
} }
} }
} }

View file

@ -5,13 +5,13 @@ declare namespace Task {
type AnyCallable = (...args: any[]) => Promise<void>; type AnyCallable = (...args: any[]) => Promise<void>;
export type Callable<T extends AnyCallable = AnyCallable> = T & { export type Callable<T extends AnyCallable = AnyCallable> = T & {
parameters: Parameters<T>; parameters: Parameters<T>;
id: string; id: symbol;
status: Status; status: Status;
}; };
export interface Task { export interface TaskRef {
name: string; name: string;
callableId: string; callableId: symbol;
description?: string; description?: string;
} }

View file

@ -1,3 +1,4 @@
import { LOG } from "@/utilities/console";
import taskManager from "."; import taskManager from ".";
import { GroupName } from "./group"; import { GroupName } from "./group";
@ -5,17 +6,24 @@ export function createTask<T extends Task.AnyCallable>(
name: string, name: string,
callable: T, callable: T,
...parameters: Parameters<T> ...parameters: Parameters<T>
): Task.Task { ): Task.TaskRef {
const callableId = taskManager.create(callable, parameters); const callableId = taskManager.create(callable, parameters);
LOG("info", "task created", name);
return { return {
name, name,
callableId, callableId,
}; };
} }
export function dispatchTask(task: Task.Task[], group: GroupName) { export function dispatchTask(tasks: Task.TaskRef[], group: GroupName) {
// TODO setTimeout(async () => {
for (const ref of tasks) {
LOG("info", "dispatching task", ref.name);
await taskManager.run(ref);
}
});
} }
export function createAndDispatchTask<T extends Task.AnyCallable>( export function createAndDispatchTask<T extends Task.AnyCallable>(