Compare commits

..

8 Commits

Author SHA1 Message Date
c2ce669f0c 3.1.0 2023-08-04 13:03:29 +02:00
05f91c3e35 feat(Task): Tasks can now be blocked by other tasks. 2023-08-04 13:03:28 +02:00
94e327c722 3.0.15 2023-08-04 11:58:54 +02:00
57a27604a7 fix(core): update 2023-08-04 11:58:53 +02:00
b077bd7a1b 3.0.14 2023-08-02 02:30:14 +02:00
f2c2dab782 fix(core): update 2023-08-02 02:30:13 +02:00
53a67c0ebe 3.0.13 2023-08-02 00:51:44 +02:00
5240a80cb3 fix(core): update 2023-08-02 00:51:43 +02:00
7 changed files with 147 additions and 189 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@push.rocks/taskbuffer",
"version": "3.0.12",
"version": "3.1.0",
"private": false,
"description": "flexible task management. TypeScript ready!",
"main": "dist_ts/index.js",

View File

@ -1,116 +1,89 @@
import { expect, tap } from '@push.rocks/tapbundle';
import * as taskbuffer from '../ts/index.js';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartdelay from '@push.rocks/smartdelay';
// setup some testData to work with
let testTask: taskbuffer.Task;
let testPreTask = new taskbuffer.Task({
taskFunction: async () => {
console.log('preTask executed');
},
preTask: testTask,
});
// some more tasks to test with
let task1Counter = 0; // how often task 1 is being executed
let task1 = new taskbuffer.Task({
name: 'Task 1',
taskFunction: () => {
let done = smartpromise.defer();
console.log('Task1 started');
setTimeout(() => {
task1Counter++;
console.log('Task1 executed');
done.resolve();
}, 5000);
return done.promise;
},
});
let task2 = new taskbuffer.Task({
name: 'Task 1',
taskFunction: async () => {
const done = smartpromise.defer();
console.log('Task2 started');
setTimeout(() => {
console.log('Task2 executed');
done.resolve();
}, 5000);
await done.promise;
},
});
let task3 = new taskbuffer.Task({
name: 'Task 3',
taskFunction: () => {
let done = smartpromise.defer();
console.log('Task3 started');
setTimeout(() => {
console.log('Task3 executed');
done.resolve();
}, 5000);
return done.promise;
},
});
tap.test('new Task() should return a new task', async () => {
testTask = new taskbuffer.Task({
const testTask = new taskbuffer.Task({
taskFunction: async () => {
console.log('executed twice');
},
preTask: testPreTask,
});
});
tap.test('expect testTask to be an instance of Task', async () => {
expect(testTask).toBeInstanceOf(taskbuffer.Task);
});
tap.test('expect testTask.idle is true', async () => {
if (!testTask.idle) {
throw new Error('testTask.idle is not true');
}
tap.test('should have bufferMax set to the provided value', async () => {
const task2 = new taskbuffer.Task({
taskFunction: async () => {},
});
expect(task2.bufferMax).toBeUndefined(); // test for a task without bufferMax set
const bufferedTask = new taskbuffer.Task({
taskFunction: async () => {},
buffered: true,
bufferMax: 3,
});
expect(bufferedTask.bufferMax).toEqual(3);
});
tap.test('testTask.running should be of type boolean and initially false', async () => {
expect(testTask.running).toBeTypeofBoolean();
// tslint:disable-next-line:no-unused-expression
expect(testTask.running).toBeFalse();
});
tap.test('testTask.trigger() should return Promise', async () => {
expect(testTask.trigger()).toBeInstanceOf(Promise);
});
tap.test('testTask.trigger() returned Promise should be fullfilled', async () => {
await testTask.trigger();
});
tap.test('expect to run a task without pre and afterTask errorless', async () => {
let localTestTask = new taskbuffer.Task({
taskFunction: async () => {
console.log('only once');
tap.test('should be able to trigger tasks multiple times', async () => {
let task1Counter = 0;
const task1 = new taskbuffer.Task({
name: 'Task 1',
taskFunction: () => {
let done = smartpromise.defer();
console.log('Task1 started');
setTimeout(() => {
task1Counter++;
console.log('Task1 executed');
done.resolve();
}, 5000);
return done.promise;
},
});
await localTestTask.trigger();
await task1.trigger();
await task1.trigger();
expect(task1Counter).toEqual(2);
});
tap.test('expect task to run in buffered mode', async () => {
let localTestTask = new taskbuffer.Task({
tap.test('should execute setup function before the task function', async () => {
const task2 = new taskbuffer.Task({
name: 'Task 2',
taskSetup: async () => {
console.log('this is the setup function for task 2. It should only run once.')
return {
nice: 'yes',
}
},
taskFunction: async (before, setupArg) => {
expect(setupArg).toEqual({ nice: 'yes' });
const done = smartpromise.defer();
console.log('Task2 started');
setTimeout(() => {
console.log('Task2 executed');
done.resolve();
}, 5000);
await done.promise;
},
});
await task2.trigger();
});
tap.test('should not exceed bufferMax when task is buffered', async () => {
let counter = 0;
const bufferedTask = new taskbuffer.Task({
taskFunction: async () => {
await smartdelay.delayFor(3000);
counter++;
await smartdelay.delayFor(2000);
counter--;
},
buffered: true,
bufferMax: 2,
});
localTestTask.trigger();
localTestTask.trigger();
localTestTask.trigger();
await localTestTask.trigger();
bufferedTask.trigger();
bufferedTask.trigger();
bufferedTask.trigger();
await smartdelay.delayFor(100);
expect(counter <= bufferedTask.bufferMax).toBeTrue();
});
tap.start();

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/taskbuffer',
version: '3.0.12',
version: '3.1.0',
description: 'flexible task management. TypeScript ready!'
}

View File

@ -5,7 +5,7 @@ export class BufferRunner {
// initialize by default
public bufferCounter: number = 0;
constructor(taskArg: Task) {
constructor(taskArg: Task<any>) {
this.task = taskArg;
}

View File

@ -9,7 +9,7 @@ export interface ICycleObject {
export class CycleCounter {
public task: Task;
public cycleObjectArray: ICycleObject[] = [];
constructor(taskArg: Task) {
constructor(taskArg: Task<any>) {
this.task = taskArg;
}
public getPromiseForCycle(cycleCountArg: number) {

View File

@ -4,20 +4,25 @@ import { CycleCounter } from './taskbuffer.classes.cyclecounter.js';
import { logger } from './taskbuffer.logging.js';
export interface ITaskFunction {
(x?: any): PromiseLike<any>;
export interface ITaskFunction<T = undefined> {
(x?: any, setupValue?: T): PromiseLike<any>;
}
export type TPreOrAfterTaskFunction = () => Task;
export interface ITaskSetupFunction<T = undefined> {
(): Promise<T>;
}
export class Task {
// STATIC
public static extractTask(preOrAfterTaskArg: Task | TPreOrAfterTaskFunction): Task {
export type TPreOrAfterTaskFunction = () => Task<any>;
export class Task<T = undefined> {
public static extractTask<T = undefined>(
preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction
): Task<T> {
switch (true) {
case !preOrAfterTaskArg:
return null;
case preOrAfterTaskArg instanceof Task:
return preOrAfterTaskArg as Task;
return preOrAfterTaskArg as Task<T>;
case typeof preOrAfterTaskArg === 'function':
const taskFunction = preOrAfterTaskArg as TPreOrAfterTaskFunction;
return taskFunction();
@ -32,7 +37,7 @@ export class Task {
return done.promise;
};
public static isTask = (taskArg: Task): boolean => {
public static isTask = (taskArg: Task<any>): boolean => {
if (taskArg instanceof Task && typeof taskArg.taskFunction === 'function') {
return true;
} else {
@ -40,10 +45,10 @@ export class Task {
}
};
public static isTaskTouched = (
taskArg: Task | TPreOrAfterTaskFunction,
touchedTasksArray: Task[]
): boolean => {
public static isTaskTouched<T = undefined>(
taskArg: Task<T> | TPreOrAfterTaskFunction,
touchedTasksArray: Task<T>[]
): boolean {
const taskToCheck = Task.extractTask(taskArg);
let result = false;
for (const keyArg in touchedTasksArray) {
@ -52,44 +57,54 @@ export class Task {
}
}
return result;
};
}
public static runTask = async (
taskArg: Task | TPreOrAfterTaskFunction,
optionsArg: { x?: any; touchedTasksArray?: Task[] }
public static runTask = async <T>(
taskArg: Task<T> | TPreOrAfterTaskFunction,
optionsArg: { x?: any; touchedTasksArray?: Task<T>[] }
) => {
// extracts the task in case it is specified as a return value of a function
const taskToRun = Task.extractTask(taskArg);
const done = plugins.smartpromise.defer();
// pay respect to execDelay
// Wait for all blocking tasks to finish
for (const task of taskToRun.blockingTasks) {
await task.finished;
}
if (!taskToRun.setupValue && taskToRun.taskSetup) {
taskToRun.setupValue = await taskToRun.taskSetup();
}
if (taskToRun.execDelay) {
await plugins.smartdelay.delayFor(taskToRun.execDelay);
}
// set running params
taskToRun.running = true;
done.promise.then(async () => {
taskToRun.running = false;
// When the task has finished running, resolve the finished promise
taskToRun.resolveFinished();
// Create a new finished promise for the next run
taskToRun.finished = new Promise((resolve) => {
taskToRun.resolveFinished = resolve;
});
});
// handle options
const options = {
...{ x: undefined, touchedTasksArray: [] },
...optionsArg,
};
const x = options.x;
const touchedTasksArray: Task[] = options.touchedTasksArray;
const touchedTasksArray: Task<T>[] = 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 {
@ -99,9 +114,8 @@ export class Task {
}
})
.then(async (x) => {
// lets run the main task
try {
return await taskToRun.taskFunction(x);
return await taskToRun.taskFunction(x, taskToRun.setupValue);
} catch (e) {
console.log(e);
}
@ -125,16 +139,9 @@ export class Task {
return await done.promise;
};
// INSTANCE
// mandatory properties
public name: string;
/**
* the version of the task
* should follow semver
* might be important for DistributedCoordinator
*/
public version: string;
public taskFunction: ITaskFunction;
public taskFunction: ITaskFunction<T>;
public buffered: boolean;
public cronJob: plugins.smarttime.CronJob;
@ -142,62 +149,52 @@ export class Task {
public execDelay: number;
public timeout: number;
// tasks to run before and after
public preTask: Task | TPreOrAfterTaskFunction;
public afterTask: Task | TPreOrAfterTaskFunction;
public preTask: Task<T> | TPreOrAfterTaskFunction;
public afterTask: Task<T> | TPreOrAfterTaskFunction;
// Add a list to store the blocking tasks
public blockingTasks: Task[] = [];
// Add a promise that will resolve when the task has finished
private finished: Promise<void>;
private resolveFinished: () => void;
// initialize by default
public running: boolean = false;
public bufferRunner = new BufferRunner(this);
public cycleCounter = new CycleCounter(this);
public idle: boolean = true;
private _state: string = 'ready';
public get idle() {
return !this.running;
}
public taskSetup: ITaskSetupFunction<T>;
public setupValue: T;
constructor(optionsArg: {
/**
* the task function to run, must return promise
*/
taskFunction: ITaskFunction;
/**
* any other task to run before
*/
preTask?: Task | TPreOrAfterTaskFunction;
/**
* any other task to run after
*/
afterTask?: Task | TPreOrAfterTaskFunction;
/**
* wether this task should run buffered
*/
taskFunction: ITaskFunction<T>;
preTask?: Task<T> | TPreOrAfterTaskFunction;
afterTask?: Task<T> | TPreOrAfterTaskFunction;
buffered?: boolean;
/**
* the maximum buffer
*/
bufferMax?: number;
/**
* the execution delay, before the task is executed
* only makes sense when running in buffered mode
*/
execDelay?: number;
/**
* the name of the task
*/
name?: string;
taskSetup?: ITaskSetupFunction<T>;
}) {
this.taskFunction = optionsArg.taskFunction;
this.preTask = optionsArg.preTask;
this.afterTask = optionsArg.afterTask;
this.idle = !this.running;
this.buffered = optionsArg.buffered;
this.bufferMax = optionsArg.bufferMax;
this.execDelay = optionsArg.execDelay;
this.name = optionsArg.name;
this.taskSetup = optionsArg.taskSetup;
// Create the finished promise
this.finished = new Promise((resolve) => {
this.resolveFinished = resolve;
});
}
/**
* trigger the task. Will trigger buffered if this.buffered is true
*/
public trigger(x?: any): Promise<any> {
if (this.buffered) {
return this.triggerBuffered(x);
@ -206,31 +203,11 @@ export class Task {
}
}
/**
* trigger task unbuffered.
* will actually run the task, not considering any buffered limits.
*/
public triggerUnBuffered(x?: any): Promise<any> {
return Task.runTask(this, { x: x });
return Task.runTask<T>(this, { x: x });
}
/**
* trigger task buffered.
* note: .trigger() also calls this function
*/
public triggerBuffered(x?: any): Promise<any> {
return this.bufferRunner.trigger(x);
}
get state(): string {
return this._state;
}
set state(stateArg: string) {
if (stateArg === 'locked') {
this._state = 'locked';
} else {
logger.log('error', `state type ${stateArg} could not be set`);
}
}
}

View File

@ -21,8 +21,8 @@ export class TaskManager {
distributedCoordinator: null,
};
constructor(optionosArg: ITaskManagerConstructorOptions = {}) {
this.options = Object.assign(this.options, optionosArg);
constructor(optionsArg: ITaskManagerConstructorOptions = {}) {
this.options = Object.assign(this.options, optionsArg);
}
/**
@ -137,11 +137,19 @@ export class TaskManager {
public async descheduleTask(task: Task) {
await this.descheduleTaskByName(task.name);
}
/**
* returns all schedules of a specific task
* @param taskNameArg
*/
public getSchedulesForTaskName(taskNameArg: string) {}
* returns the schedule of a specific task
* @param taskNameArg
*/
public getScheduleForTaskName(taskNameArg: string): string | null {
const task = this.getTaskByName(taskNameArg);
if (!task || !task.cronJob) {
return null;
}
return task.cronJob.cronExpression;
}
/**
* starts the taskmanager