feat(taskrunner): now has working taskrunner
This commit is contained in:
@ -3,6 +3,4 @@ export { Taskchain } from './taskbuffer.classes.taskchain';
|
||||
export { Taskparallel } from './taskbuffer.classes.taskparallel';
|
||||
export { TaskManager } from './taskbuffer.classes.taskmanager';
|
||||
export { TaskOnce } from './taskbuffer.classes.taskonce';
|
||||
|
||||
// import for naming only
|
||||
import './taskbuffer.classes.helpers';
|
||||
export { TaskRunner } from './taskbuffer.classes.taskrunner';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Task } from "./taskbuffer.classes.task";
|
||||
import { Task } from './taskbuffer.classes.task';
|
||||
|
||||
export class BufferRunner {
|
||||
public task: Task;
|
||||
@ -39,4 +39,4 @@ export class BufferRunner {
|
||||
};
|
||||
recursiveBufferRunner(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,2 @@
|
||||
import plugins = require('./taskbuffer.plugins');
|
||||
import { Task, ITaskFunction } from './taskbuffer.classes.task';
|
||||
|
||||
|
||||
|
||||
|
@ -12,12 +12,12 @@ export type TPreOrAfterTaskFunction = () => Task;
|
||||
export class Task {
|
||||
// STATIC
|
||||
public static extractTask(preOrAfterTaskArg: Task | TPreOrAfterTaskFunction): Task {
|
||||
switch(true) {
|
||||
case (!preOrAfterTaskArg):
|
||||
switch (true) {
|
||||
case !preOrAfterTaskArg:
|
||||
return null;
|
||||
case (preOrAfterTaskArg instanceof Task):
|
||||
case preOrAfterTaskArg instanceof Task:
|
||||
return preOrAfterTaskArg as Task;
|
||||
case typeof preOrAfterTaskArg === "function":
|
||||
case typeof preOrAfterTaskArg === 'function':
|
||||
const taskFunction = preOrAfterTaskArg as TPreOrAfterTaskFunction;
|
||||
return taskFunction();
|
||||
default:
|
||||
@ -25,13 +25,12 @@ export class Task {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static emptyTaskFunction: ITaskFunction = function(x) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
|
||||
public static isTask = (taskArg: Task): boolean => {
|
||||
if (taskArg instanceof Task && typeof taskArg.taskFunction === 'function') {
|
||||
return true;
|
||||
@ -39,8 +38,11 @@ export class Task {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
public static isTaskTouched = (taskArg: Task | TPreOrAfterTaskFunction, touchedTasksArray: Task[]): boolean => {
|
||||
|
||||
public static isTaskTouched = (
|
||||
taskArg: Task | TPreOrAfterTaskFunction,
|
||||
touchedTasksArray: Task[]
|
||||
): boolean => {
|
||||
const taskToCheck = Task.extractTask(taskArg);
|
||||
let result = false;
|
||||
for (const keyArg in touchedTasksArray) {
|
||||
@ -50,23 +52,26 @@ export class Task {
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
public static runTask = async (taskArg: Task | TPreOrAfterTaskFunction, optionsArg: { x?; touchedTasksArray?: Task[] }) => {
|
||||
|
||||
public static runTask = async (
|
||||
taskArg: Task | TPreOrAfterTaskFunction,
|
||||
optionsArg: { x?; touchedTasksArray?: Task[] }
|
||||
) => {
|
||||
const taskToRun = Task.extractTask(taskArg);
|
||||
const done = plugins.smartpromise.defer();
|
||||
|
||||
|
||||
// pay respect to execDelay
|
||||
if (taskToRun.execDelay) {
|
||||
await plugins.smartdelay.delayFor(taskToRun.execDelay);
|
||||
}
|
||||
|
||||
|
||||
// set running params
|
||||
taskToRun.running = true;
|
||||
|
||||
|
||||
done.promise.then(async () => {
|
||||
taskToRun.running = false;
|
||||
});
|
||||
|
||||
|
||||
// handle options
|
||||
const options = {
|
||||
...{ x: undefined, touchedTasksArray: [] },
|
||||
@ -74,15 +79,15 @@ export class Task {
|
||||
};
|
||||
const x = options.x;
|
||||
const touchedTasksArray: Task[] = options.touchedTasksArray;
|
||||
|
||||
|
||||
touchedTasksArray.push(taskToRun);
|
||||
|
||||
|
||||
// run the task cascade
|
||||
const localDeferred = plugins.smartpromise.defer();
|
||||
localDeferred.promise
|
||||
.then(() => {
|
||||
// lets run any preTask
|
||||
|
||||
|
||||
if (taskToRun.preTask && !Task.isTaskTouched(taskToRun.preTask, touchedTasksArray)) {
|
||||
return Task.runTask(taskToRun.preTask, { x, touchedTasksArray });
|
||||
} else {
|
||||
@ -116,7 +121,7 @@ export class Task {
|
||||
});
|
||||
localDeferred.resolve();
|
||||
return await done.promise;
|
||||
}
|
||||
};
|
||||
|
||||
// INSTANCE
|
||||
// man datory properties
|
||||
|
@ -10,7 +10,7 @@ export interface ICronJob {
|
||||
export class TaskManager {
|
||||
public taskMap = new plugins.lik.Objectmap<Task>();
|
||||
private cronJobMap = new plugins.lik.Objectmap<ICronJob>();
|
||||
|
||||
|
||||
constructor() {
|
||||
// nothing here
|
||||
}
|
||||
@ -85,7 +85,7 @@ export class TaskManager {
|
||||
|
||||
/**
|
||||
* deschedules a task by name
|
||||
* @param taskNameArg
|
||||
* @param taskNameArg
|
||||
*/
|
||||
public descheduleTaskByName(taskNameArg: string) {
|
||||
const descheduledCron = this.cronJobMap.findOneAndRemove(itemArg => {
|
||||
|
@ -8,11 +8,18 @@ export class TaskRunner {
|
||||
public runningTasks: plugins.lik.Objectmap<Task> = new plugins.lik.Objectmap<Task>();
|
||||
public qeuedTasks: Task[] = [];
|
||||
|
||||
constructor() {
|
||||
this.runningTasks.eventSubject.subscribe(async eventArg => {
|
||||
this.checkExecution();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a task to the qeue
|
||||
*/
|
||||
public addTask(taskArg: Task) {
|
||||
this.qeuedTasks.push(taskArg);
|
||||
this.checkExecution();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -30,12 +37,20 @@ export class TaskRunner {
|
||||
this.status = 'running';
|
||||
}
|
||||
|
||||
/**
|
||||
* checks wether execution is on point
|
||||
*/
|
||||
public async checkExecution() {
|
||||
if (this.runningTasks.getArray().length < this.maxParrallelJobs) {
|
||||
if (
|
||||
this.runningTasks.getArray().length < this.maxParrallelJobs &&
|
||||
this.status === 'running' &&
|
||||
this.qeuedTasks.length > 0
|
||||
) {
|
||||
const nextJob = this.qeuedTasks.shift();
|
||||
this.runningTasks.add(nextJob);
|
||||
await nextJob.trigger();
|
||||
|
||||
this.runningTasks.remove(nextJob);
|
||||
this.checkExecution();
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,5 +59,5 @@ export class TaskRunner {
|
||||
*/
|
||||
public stop() {
|
||||
this.status = 'stopped';
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user