Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
c2ce669f0c | |||
05f91c3e35 | |||
94e327c722 | |||
57a27604a7 | |||
b077bd7a1b | |||
f2c2dab782 |
@ -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",
|
||||
|
@ -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 () => {},
|
||||
|
@ -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!'
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ export class BufferRunner {
|
||||
// initialize by default
|
||||
public bufferCounter: number = 0;
|
||||
|
||||
constructor(taskArg: Task) {
|
||||
constructor(taskArg: Task<any>) {
|
||||
this.task = taskArg;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user