This commit is contained in:
SrGooglo 2025-05-15 13:04:09 +00:00
parent 2731fd7b4e
commit 79b8ecc98f

View File

@ -2,90 +2,95 @@ import { Core } from "@ragestudio/vessel"
import { Observable } from "object-observer" import { Observable } from "object-observer"
export default class TasksQueue extends Core { export default class TasksQueue extends Core {
static depends = ["settings"] static depends = ["settings"]
static namespace = "tasksQueue" static namespace = "tasksQueue"
static get maxRunningTasks() { static get maxRunningTasks() {
return app.cores.settings.get("tasks.maxRunningTasks") ?? 3 return app.cores.settings.get("tasks.maxRunningTasks") ?? 3
} }
public = { public = {
appendToQueue: this.appendToQueue.bind(this), appendToQueue: this.appendToQueue.bind(this),
processTasks: this.processTasks.bind(this), processTasks: this.processTasks.bind(this),
} }
taskQueue = Observable.from([]) taskQueue = Observable.from([])
runningTasksIds = Observable.from([]) runningTasksIds = Observable.from([])
processTasks() { processTasks() {
if (this.runningTasksIds.length >= TasksQueue.maxRunningTasks ?? 1) { if (this.runningTasksIds.length >= TasksQueue.maxRunningTasks ?? 1) {
this.console.log("We are already running the maximum number of tasks") this.console.log(
return false "We are already running the maximum number of tasks",
} )
return false
}
// check if there are new tasks in the queue and move them to the tasks array with the maximum number of tasks can be run // check if there are new tasks in the queue and move them to the tasks array with the maximum number of tasks can be run
if (this.taskQueue.length === 0) { if (this.taskQueue.length === 0) {
this.console.log("No tasks in the queue") this.console.log("No tasks in the queue")
return false return false
} }
let tasks = this.taskQueue.splice(0, TasksQueue.maxRunningTasks ?? 1) let tasks = this.taskQueue.splice(0, TasksQueue.maxRunningTasks ?? 1)
tasks = tasks.filter((task) => task) tasks = tasks.filter((task) => task)
const promises = tasks.map(async (task) => { const promises = tasks.map(async (task) => {
if (typeof task.fn !== "function") { if (typeof task.fn !== "function") {
throw new Error("Task must be a function") throw new Error("Task must be a function")
} }
if (typeof task.id === "undefined") { if (typeof task.id === "undefined") {
throw new Error("Task id is required") throw new Error("Task id is required")
} }
// add the task to the running tasks array // add the task to the running tasks array
this.runningTasksIds.push(task.id) this.runningTasksIds.push(task.id)
const taskResult = await task.fn() const taskResult = await task.fn().catch((error) => {
.catch((error) => { // delete the task from the running tasks array
// delete the task from the running tasks array this.runningTasksIds = this.runningTasksIds.filter(
this.runningTasksIds = this.runningTasksIds.filter((runningTaskId) => runningTaskId !== task.id) (runningTaskId) => runningTaskId !== task.id,
)
// propagate the error through an exception // propagate the error through an exception
throw error throw error
}) })
// delete the task from the running tasks array // delete the task from the running tasks array
this.runningTasksIds = this.runningTasksIds.filter((runningTaskId) => runningTaskId !== task.id) this.runningTasksIds = this.runningTasksIds.filter(
(runningTaskId) => runningTaskId !== task.id,
)
return taskResult return taskResult
}) })
Promise.all(promises) Promise.all(promises)
.then((res) => { .then((res) => {
this.processTasks() this.processTasks()
}) })
.catch((error) => { .catch((error) => {
this.console.error(error) this.console.error(error)
this.processTasks() this.processTasks()
}) })
} }
appendToQueue(taskId, task) { appendToQueue(taskId, task) {
if (!taskId) { if (!taskId) {
throw new Error("Task id is required") throw new Error("Task id is required")
} }
if (Array.isArray(task)) { if (Array.isArray(task)) {
throw new Error("Task must be a function") throw new Error("Task must be a function")
} }
this.taskQueue.unshift({ this.taskQueue.unshift({
id: taskId, id: taskId,
fn: task, fn: task,
}) })
this.processTasks() this.processTasks()
} }
} }