2019-11-27 23:34:45 +00:00
|
|
|
import * as plugins from './taskbuffer.plugins';
|
|
|
|
|
|
|
|
import { Task } from './taskbuffer.classes.task';
|
|
|
|
|
|
|
|
export class TaskRunner {
|
|
|
|
public maxParrallelJobs: number = 1;
|
|
|
|
public status: 'stopped' | 'running' = 'stopped';
|
2020-07-12 00:48:51 +00:00
|
|
|
public runningTasks: plugins.lik.ObjectMap<Task> = new plugins.lik.ObjectMap<Task>();
|
2019-11-27 23:34:45 +00:00
|
|
|
public qeuedTasks: Task[] = [];
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* adds a task to the qeue
|
|
|
|
*/
|
|
|
|
public addTask(taskArg: Task) {
|
|
|
|
this.qeuedTasks.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
|
|
|
|
* be careful, you might loose dependability of tasks
|
|
|
|
*/
|
|
|
|
public setMaxParallelJobs(maxParrallelJobsArg: number) {
|
|
|
|
this.maxParrallelJobs = maxParrallelJobsArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* checks wether execution is on point
|
|
|
|
*/
|
2019-11-27 23:34:45 +00:00
|
|
|
public async checkExecution() {
|
2019-11-28 11:33:26 +00:00
|
|
|
if (
|
|
|
|
this.runningTasks.getArray().length < this.maxParrallelJobs &&
|
|
|
|
this.status === 'running' &&
|
|
|
|
this.qeuedTasks.length > 0
|
|
|
|
) {
|
2019-11-27 23:34:45 +00:00
|
|
|
const nextJob = this.qeuedTasks.shift();
|
|
|
|
this.runningTasks.add(nextJob);
|
|
|
|
await nextJob.trigger();
|
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
|
|
|
}
|