Compare commits

..

6 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
7 changed files with 51 additions and 37 deletions

View File

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

View File

@ -12,13 +12,6 @@ tap.test('new Task() should return a new task', async () => {
expect(testTask).toBeInstanceOf(taskbuffer.Task);
});
tap.test('should be able to get the task state', async () => {
const testTask = new taskbuffer.Task({
taskFunction: async () => {},
});
expect(testTask.state).toEqual('ready');
});
tap.test('should have bufferMax set to the provided value', async () => {
const task2 = new taskbuffer.Task({
taskFunction: async () => {},

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/taskbuffer',
version: '3.0.13',
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

@ -15,8 +15,9 @@ export interface ITaskSetupFunction<T = undefined> {
export type TPreOrAfterTaskFunction = () => Task<any>;
export class Task<T = undefined> {
// STATIC
public static extractTask<T = undefined>(preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction): Task<T> {
public static extractTask<T = undefined>(
preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction
): Task<T> {
switch (true) {
case !preOrAfterTaskArg:
return null;
@ -44,7 +45,7 @@ export class Task<T = undefined> {
}
};
public static isTaskTouched<T = undefined> (
public static isTaskTouched<T = undefined>(
taskArg: Task<T> | TPreOrAfterTaskFunction,
touchedTasksArray: Task<T>[]
): boolean {
@ -56,7 +57,7 @@ export class Task<T = undefined> {
}
}
return result;
};
}
public static runTask = async <T>(
taskArg: Task<T> | TPreOrAfterTaskFunction,
@ -65,6 +66,11 @@ export class Task<T = undefined> {
const taskToRun = Task.extractTask(taskArg);
const done = plugins.smartpromise.defer();
// 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();
}
@ -77,6 +83,14 @@ export class Task<T = undefined> {
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;
});
});
const options = {
@ -125,7 +139,6 @@ export class Task<T = undefined> {
return await done.promise;
};
// INSTANCE
public name: string;
public version: string;
public taskFunction: ITaskFunction<T>;
@ -139,12 +152,20 @@ export class Task<T = undefined> {
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;
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;
@ -162,12 +183,16 @@ export class Task<T = undefined> {
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;
});
}
public trigger(x?: any): Promise<any> {
@ -185,16 +210,4 @@ export class Task<T = undefined> {
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