2022-03-25 12:14:49 +01:00
|
|
|
import * as plugins from './taskbuffer.plugins.js';
|
2019-11-27 23:34:45 +00:00
|
|
|
|
2022-03-25 12:14:49 +01:00
|
|
|
import { Task } from './taskbuffer.classes.task.js';
|
2026-01-25 23:29:00 +00:00
|
|
|
import { logger } from './taskbuffer.logging.js';
|
2019-11-27 23:34:45 +00:00
|
|
|
|
|
|
|
|
export class TaskRunner {
|
2026-01-25 23:29:00 +00:00
|
|
|
public maxParallelJobs: number = 1;
|
2019-11-27 23:34:45 +00:00
|
|
|
public status: 'stopped' | 'running' = 'stopped';
|
2025-08-26 20:16:47 +00:00
|
|
|
public runningTasks: plugins.lik.ObjectMap<Task> =
|
|
|
|
|
new plugins.lik.ObjectMap<Task>();
|
2026-01-25 23:29:00 +00:00
|
|
|
public queuedTasks: Task[] = [];
|
2019-11-27 23:34:45 +00:00
|
|
|
|
2019-11-28 11:33:26 +00:00
|
|
|
constructor() {
|
2020-07-12 00:48:51 +00:00
|
|
|
this.runningTasks.eventSubject.subscribe(async (eventArg) => {
|
2019-11-28 11:33:26 +00:00
|
|
|
this.checkExecution();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 23:34:45 +00:00
|
|
|
/**
|
2026-01-25 23:29:00 +00:00
|
|
|
* adds a task to the queue
|
2019-11-27 23:34:45 +00:00
|
|
|
*/
|
|
|
|
|
public addTask(taskArg: Task) {
|
2026-01-25 23:29:00 +00:00
|
|
|
this.queuedTasks.push(taskArg);
|
2019-11-28 11:33:26 +00:00
|
|
|
this.checkExecution();
|
2019-11-27 23:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set amount of parallel tasks
|
2026-01-25 23:29:00 +00:00
|
|
|
* be careful, you might lose dependability of tasks
|
2019-11-27 23:34:45 +00:00
|
|
|
*/
|
2026-01-25 23:29:00 +00:00
|
|
|
public setMaxParallelJobs(maxParallelJobsArg: number) {
|
|
|
|
|
this.maxParallelJobs = maxParallelJobsArg;
|
2019-11-27 23:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* starts the task queue
|
|
|
|
|
*/
|
2019-11-28 14:42:28 +00:00
|
|
|
public async start() {
|
2019-11-27 23:34:45 +00:00
|
|
|
this.status = 'running';
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 11:33:26 +00:00
|
|
|
/**
|
2026-01-25 23:29:00 +00:00
|
|
|
* checks whether execution is on point
|
2019-11-28 11:33:26 +00:00
|
|
|
*/
|
2019-11-27 23:34:45 +00:00
|
|
|
public async checkExecution() {
|
2019-11-28 11:33:26 +00:00
|
|
|
if (
|
2026-01-25 23:29:00 +00:00
|
|
|
this.runningTasks.getArray().length < this.maxParallelJobs &&
|
2019-11-28 11:33:26 +00:00
|
|
|
this.status === 'running' &&
|
2026-01-25 23:29:00 +00:00
|
|
|
this.queuedTasks.length > 0
|
2019-11-28 11:33:26 +00:00
|
|
|
) {
|
2026-01-25 23:29:00 +00:00
|
|
|
const nextJob = this.queuedTasks.shift();
|
2019-11-27 23:34:45 +00:00
|
|
|
this.runningTasks.add(nextJob);
|
2026-01-25 23:29:00 +00:00
|
|
|
try {
|
|
|
|
|
await nextJob.trigger();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.log('error', `TaskRunner: task "${nextJob.name || 'unnamed'}" failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
|
|
|
}
|
2019-11-28 11:33:26 +00:00
|
|
|
this.runningTasks.remove(nextJob);
|
|
|
|
|
this.checkExecution();
|
2019-11-27 23:34:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* stops the task queue
|
|
|
|
|
*/
|
2019-11-28 14:42:28 +00:00
|
|
|
public async stop() {
|
2019-11-27 23:34:45 +00:00
|
|
|
this.status = 'stopped';
|
2019-11-28 11:33:26 +00:00
|
|
|
}
|
2019-11-27 23:34:45 +00:00
|
|
|
}
|