BREAKING CHANGE(taskbuffer): Introduce constraint-based concurrency with TaskConstraintGroup and TaskManager integration; remove legacy TaskRunner and several Task APIs (breaking); add typed Task.data and update exports and tests.
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/taskbuffer',
|
||||
version: '4.2.1',
|
||||
version: '5.0.0',
|
||||
description: 'A flexible task management library supporting TypeScript, allowing for task buffering, scheduling, and execution with dependency management.'
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ export { Taskchain } from './taskbuffer.classes.taskchain.js';
|
||||
export { Taskparallel } from './taskbuffer.classes.taskparallel.js';
|
||||
export { TaskManager } from './taskbuffer.classes.taskmanager.js';
|
||||
export { TaskOnce } from './taskbuffer.classes.taskonce.js';
|
||||
export { TaskRunner } from './taskbuffer.classes.taskrunner.js';
|
||||
export { TaskDebounced } from './taskbuffer.classes.taskdebounced.js';
|
||||
export { TaskConstraintGroup } from './taskbuffer.classes.taskconstraintgroup.js';
|
||||
|
||||
// Task step system
|
||||
export { TaskStep } from './taskbuffer.classes.taskstep.js';
|
||||
export type { ITaskStep } from './taskbuffer.classes.taskstep.js';
|
||||
|
||||
// Metadata interfaces
|
||||
export type { ITaskMetadata, ITaskExecutionReport, IScheduledTaskInfo, ITaskEvent, TTaskEventType } from './taskbuffer.interfaces.js';
|
||||
export type { ITaskMetadata, ITaskExecutionReport, IScheduledTaskInfo, ITaskEvent, TTaskEventType, ITaskConstraintGroupOptions } from './taskbuffer.interfaces.js';
|
||||
|
||||
import * as distributedCoordination from './taskbuffer.classes.distributedcoordinator.js';
|
||||
export { distributedCoordination };
|
||||
|
||||
@@ -6,7 +6,7 @@ export class BufferRunner {
|
||||
// initialize by default
|
||||
public bufferCounter: number = 0;
|
||||
|
||||
constructor(taskArg: Task<any>) {
|
||||
constructor(taskArg: Task<any, any, any>) {
|
||||
this.task = taskArg;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ export interface ICycleObject {
|
||||
export class CycleCounter {
|
||||
public task: Task;
|
||||
public cycleObjectArray: ICycleObject[] = [];
|
||||
constructor(taskArg: Task<any>) {
|
||||
constructor(taskArg: Task<any, any, any>) {
|
||||
this.task = taskArg;
|
||||
}
|
||||
public getPromiseForCycle(cycleCountArg: number) {
|
||||
|
||||
@@ -19,18 +19,18 @@ export type TPreOrAfterTaskFunction = () => Task<any>;
|
||||
// Type helper to extract step names from array
|
||||
export type StepNames<T> = T extends ReadonlyArray<{ name: infer N }> ? N : never;
|
||||
|
||||
export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; description: string; percentage: number }> = []> {
|
||||
export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; description: string; percentage: number }> = [], TData extends Record<string, unknown> = Record<string, unknown>> {
|
||||
public static extractTask<T = undefined, TSteps extends ReadonlyArray<{ name: string; description: string; percentage: number }> = []>(
|
||||
preOrAfterTaskArg: Task<T, TSteps> | TPreOrAfterTaskFunction,
|
||||
): Task<T, TSteps> {
|
||||
preOrAfterTaskArg: Task<T, TSteps, any> | TPreOrAfterTaskFunction,
|
||||
): Task<T, TSteps, any> {
|
||||
switch (true) {
|
||||
case !preOrAfterTaskArg:
|
||||
return null;
|
||||
case preOrAfterTaskArg instanceof Task:
|
||||
return preOrAfterTaskArg as Task<T, TSteps>;
|
||||
return preOrAfterTaskArg as Task<T, TSteps, any>;
|
||||
case typeof preOrAfterTaskArg === 'function':
|
||||
const taskFunction = preOrAfterTaskArg as TPreOrAfterTaskFunction;
|
||||
return taskFunction() as unknown as Task<T, TSteps>;
|
||||
return taskFunction() as unknown as Task<T, TSteps, any>;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -42,7 +42,7 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
public static isTask = (taskArg: Task<any>): boolean => {
|
||||
public static isTask = (taskArg: Task<any, any, any>): boolean => {
|
||||
if (taskArg instanceof Task && typeof taskArg.taskFunction === 'function') {
|
||||
return true;
|
||||
} else {
|
||||
@@ -51,8 +51,8 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
};
|
||||
|
||||
public static isTaskTouched<T = undefined, TSteps extends ReadonlyArray<{ name: string; description: string; percentage: number }> = []>(
|
||||
taskArg: Task<T, TSteps> | TPreOrAfterTaskFunction,
|
||||
touchedTasksArray: Task<T, TSteps>[],
|
||||
taskArg: Task<T, TSteps, any> | TPreOrAfterTaskFunction,
|
||||
touchedTasksArray: Task<T, TSteps, any>[],
|
||||
): boolean {
|
||||
const taskToCheck = Task.extractTask(taskArg);
|
||||
let result = false;
|
||||
@@ -65,25 +65,16 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
}
|
||||
|
||||
public static runTask = async <T, TSteps extends ReadonlyArray<{ name: string; description: string; percentage: number }> = []>(
|
||||
taskArg: Task<T, TSteps> | TPreOrAfterTaskFunction,
|
||||
optionsArg: { x?: any; touchedTasksArray?: Task<T, TSteps>[] },
|
||||
taskArg: Task<T, TSteps, any> | TPreOrAfterTaskFunction,
|
||||
optionsArg: { x?: any; touchedTasksArray?: Task<T, TSteps, any>[] },
|
||||
) => {
|
||||
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();
|
||||
}
|
||||
|
||||
if (taskToRun.execDelay) {
|
||||
await plugins.smartdelay.delayFor(taskToRun.execDelay);
|
||||
}
|
||||
|
||||
taskToRun.running = true;
|
||||
taskToRun.runCount++;
|
||||
taskToRun.lastRun = new Date();
|
||||
@@ -100,26 +91,10 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
// Complete all steps when task finishes
|
||||
taskToRun.completeAllSteps();
|
||||
taskToRun.emitEvent(taskToRun.lastError ? 'failed' : 'completed');
|
||||
|
||||
// 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;
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
taskToRun.running = false;
|
||||
taskToRun.emitEvent('failed', { error: err instanceof Error ? err.message : String(err) });
|
||||
|
||||
// Resolve finished so blocking dependants don't hang
|
||||
taskToRun.resolveFinished();
|
||||
|
||||
// Create a new finished promise for the next run
|
||||
taskToRun.finished = new Promise((resolve) => {
|
||||
taskToRun.resolveFinished = resolve;
|
||||
});
|
||||
});
|
||||
|
||||
const options = {
|
||||
@@ -127,7 +102,7 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
...optionsArg,
|
||||
};
|
||||
const x = options.x;
|
||||
const touchedTasksArray: Task<T, TSteps>[] = options.touchedTasksArray;
|
||||
const touchedTasksArray: Task<T, TSteps, any>[] = options.touchedTasksArray;
|
||||
|
||||
touchedTasksArray.push(taskToRun);
|
||||
|
||||
@@ -198,18 +173,12 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
public cronJob: plugins.smarttime.CronJob;
|
||||
|
||||
public bufferMax: number;
|
||||
public execDelay: number;
|
||||
public timeout: number;
|
||||
|
||||
public preTask: Task<T, any> | TPreOrAfterTaskFunction;
|
||||
public afterTask: Task<T, any> | TPreOrAfterTaskFunction;
|
||||
public data: TData;
|
||||
|
||||
// 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 preTask: Task<T, any, any> | TPreOrAfterTaskFunction;
|
||||
public afterTask: Task<T, any, any> | TPreOrAfterTaskFunction;
|
||||
|
||||
public running: boolean = false;
|
||||
public bufferRunner = new BufferRunner(this);
|
||||
@@ -275,12 +244,12 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
|
||||
constructor(optionsArg: {
|
||||
taskFunction: ITaskFunction<T>;
|
||||
preTask?: Task<T, any> | TPreOrAfterTaskFunction;
|
||||
afterTask?: Task<T, any> | TPreOrAfterTaskFunction;
|
||||
preTask?: Task<T, any, any> | TPreOrAfterTaskFunction;
|
||||
afterTask?: Task<T, any, any> | TPreOrAfterTaskFunction;
|
||||
buffered?: boolean;
|
||||
bufferMax?: number;
|
||||
execDelay?: number;
|
||||
name?: string;
|
||||
data?: TData;
|
||||
taskSetup?: ITaskSetupFunction<T>;
|
||||
steps?: TSteps;
|
||||
catchErrors?: boolean;
|
||||
@@ -291,8 +260,8 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
this.afterTask = optionsArg.afterTask;
|
||||
this.buffered = optionsArg.buffered;
|
||||
this.bufferMax = optionsArg.bufferMax;
|
||||
this.execDelay = optionsArg.execDelay;
|
||||
this.name = optionsArg.name;
|
||||
this.data = optionsArg.data ?? ({} as TData);
|
||||
this.taskSetup = optionsArg.taskSetup;
|
||||
this.catchErrors = optionsArg.catchErrors ?? false;
|
||||
this.labels = optionsArg.labels ? { ...optionsArg.labels } : {};
|
||||
@@ -309,11 +278,6 @@ export class Task<T = undefined, TSteps extends ReadonlyArray<{ name: string; de
|
||||
this.steps.set(stepConfig.name, step);
|
||||
}
|
||||
}
|
||||
|
||||
// Create the finished promise
|
||||
this.finished = new Promise((resolve) => {
|
||||
this.resolveFinished = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
public trigger(x?: any): Promise<any> {
|
||||
|
||||
80
ts/taskbuffer.classes.taskconstraintgroup.ts
Normal file
80
ts/taskbuffer.classes.taskconstraintgroup.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import type { Task } from './taskbuffer.classes.task.js';
|
||||
import type { ITaskConstraintGroupOptions } from './taskbuffer.interfaces.js';
|
||||
|
||||
export class TaskConstraintGroup<TData extends Record<string, unknown> = Record<string, unknown>> {
|
||||
public name: string;
|
||||
public maxConcurrent: number;
|
||||
public cooldownMs: number;
|
||||
private constraintKeyForTask: (task: Task<any, any, TData>) => string | null | undefined;
|
||||
|
||||
private runningCounts = new Map<string, number>();
|
||||
private lastCompletionTimes = new Map<string, number>();
|
||||
|
||||
constructor(options: ITaskConstraintGroupOptions<TData>) {
|
||||
this.name = options.name;
|
||||
this.constraintKeyForTask = options.constraintKeyForTask;
|
||||
this.maxConcurrent = options.maxConcurrent ?? Infinity;
|
||||
this.cooldownMs = options.cooldownMs ?? 0;
|
||||
}
|
||||
|
||||
public getConstraintKey(task: Task<any, any, TData>): string | null {
|
||||
const key = this.constraintKeyForTask(task);
|
||||
return key ?? null;
|
||||
}
|
||||
|
||||
public canRun(subGroupKey: string): boolean {
|
||||
const running = this.runningCounts.get(subGroupKey) ?? 0;
|
||||
if (running >= this.maxConcurrent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.cooldownMs > 0) {
|
||||
const lastCompletion = this.lastCompletionTimes.get(subGroupKey);
|
||||
if (lastCompletion !== undefined) {
|
||||
const elapsed = Date.now() - lastCompletion;
|
||||
if (elapsed < this.cooldownMs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public acquireSlot(subGroupKey: string): void {
|
||||
const current = this.runningCounts.get(subGroupKey) ?? 0;
|
||||
this.runningCounts.set(subGroupKey, current + 1);
|
||||
}
|
||||
|
||||
public releaseSlot(subGroupKey: string): void {
|
||||
const current = this.runningCounts.get(subGroupKey) ?? 0;
|
||||
const next = Math.max(0, current - 1);
|
||||
if (next === 0) {
|
||||
this.runningCounts.delete(subGroupKey);
|
||||
} else {
|
||||
this.runningCounts.set(subGroupKey, next);
|
||||
}
|
||||
this.lastCompletionTimes.set(subGroupKey, Date.now());
|
||||
}
|
||||
|
||||
public getCooldownRemaining(subGroupKey: string): number {
|
||||
if (this.cooldownMs <= 0) {
|
||||
return 0;
|
||||
}
|
||||
const lastCompletion = this.lastCompletionTimes.get(subGroupKey);
|
||||
if (lastCompletion === undefined) {
|
||||
return 0;
|
||||
}
|
||||
const elapsed = Date.now() - lastCompletion;
|
||||
return Math.max(0, this.cooldownMs - elapsed);
|
||||
}
|
||||
|
||||
public getRunningCount(subGroupKey: string): number {
|
||||
return this.runningCounts.get(subGroupKey) ?? 0;
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.runningCounts.clear();
|
||||
this.lastCompletionTimes.clear();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import * as plugins from './taskbuffer.plugins.js';
|
||||
import { Task } from './taskbuffer.classes.task.js';
|
||||
import { TaskConstraintGroup } from './taskbuffer.classes.taskconstraintgroup.js';
|
||||
import {
|
||||
AbstractDistributedCoordinator,
|
||||
type IDistributedTaskRequestResult,
|
||||
} from './taskbuffer.classes.distributedcoordinator.js';
|
||||
import type { ITaskMetadata, ITaskExecutionReport, IScheduledTaskInfo, ITaskEvent } from './taskbuffer.interfaces.js';
|
||||
import type { ITaskMetadata, ITaskExecutionReport, IScheduledTaskInfo, ITaskEvent, IConstrainedTaskEntry } from './taskbuffer.interfaces.js';
|
||||
import { logger } from './taskbuffer.logging.js';
|
||||
|
||||
export interface ICronJob {
|
||||
@@ -19,23 +20,28 @@ export interface ITaskManagerConstructorOptions {
|
||||
|
||||
export class TaskManager {
|
||||
public randomId = plugins.smartunique.shortId();
|
||||
public taskMap = new plugins.lik.ObjectMap<Task<any, any>>();
|
||||
public taskMap = new plugins.lik.ObjectMap<Task<any, any, any>>();
|
||||
public readonly taskSubject = new plugins.smartrx.rxjs.Subject<ITaskEvent>();
|
||||
private taskSubscriptions = new Map<Task<any, any>, plugins.smartrx.rxjs.Subscription>();
|
||||
private taskSubscriptions = new Map<Task<any, any, any>, plugins.smartrx.rxjs.Subscription>();
|
||||
private cronJobManager = new plugins.smarttime.CronManager();
|
||||
public options: ITaskManagerConstructorOptions = {
|
||||
distributedCoordinator: null,
|
||||
};
|
||||
|
||||
// Constraint system
|
||||
public constraintGroups: TaskConstraintGroup<any>[] = [];
|
||||
private constraintQueue: IConstrainedTaskEntry[] = [];
|
||||
private drainTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
constructor(options: ITaskManagerConstructorOptions = {}) {
|
||||
this.options = Object.assign(this.options, options);
|
||||
}
|
||||
|
||||
public getTaskByName(taskName: string): Task<any, any> {
|
||||
public getTaskByName(taskName: string): Task<any, any, any> {
|
||||
return this.taskMap.findSync((task) => task.name === taskName);
|
||||
}
|
||||
|
||||
public addTask(task: Task<any, any>): void {
|
||||
public addTask(task: Task<any, any, any>): void {
|
||||
if (!task.name) {
|
||||
throw new Error('Task must have a name to be added to taskManager');
|
||||
}
|
||||
@@ -46,7 +52,7 @@ export class TaskManager {
|
||||
this.taskSubscriptions.set(task, subscription);
|
||||
}
|
||||
|
||||
public removeTask(task: Task<any, any>): void {
|
||||
public removeTask(task: Task<any, any, any>): void {
|
||||
this.taskMap.remove(task);
|
||||
const subscription = this.taskSubscriptions.get(task);
|
||||
if (subscription) {
|
||||
@@ -55,21 +61,134 @@ export class TaskManager {
|
||||
}
|
||||
}
|
||||
|
||||
public addAndScheduleTask(task: Task<any, any>, cronString: string) {
|
||||
public addAndScheduleTask(task: Task<any, any, any>, cronString: string) {
|
||||
this.addTask(task);
|
||||
this.scheduleTaskByName(task.name, cronString);
|
||||
}
|
||||
|
||||
// Constraint group management
|
||||
public addConstraintGroup(group: TaskConstraintGroup<any>): void {
|
||||
this.constraintGroups.push(group);
|
||||
}
|
||||
|
||||
public removeConstraintGroup(name: string): void {
|
||||
this.constraintGroups = this.constraintGroups.filter((g) => g.name !== name);
|
||||
}
|
||||
|
||||
// Core constraint evaluation
|
||||
public async triggerTaskConstrained(task: Task<any, any, any>, input?: any): Promise<any> {
|
||||
// Gather applicable constraints
|
||||
const applicableGroups: Array<{ group: TaskConstraintGroup<any>; key: string }> = [];
|
||||
for (const group of this.constraintGroups) {
|
||||
const key = group.getConstraintKey(task);
|
||||
if (key !== null) {
|
||||
applicableGroups.push({ group, key });
|
||||
}
|
||||
}
|
||||
|
||||
// No constraints apply → trigger directly
|
||||
if (applicableGroups.length === 0) {
|
||||
return task.trigger(input);
|
||||
}
|
||||
|
||||
// Check if all constraints allow running
|
||||
const allCanRun = applicableGroups.every(({ group, key }) => group.canRun(key));
|
||||
if (allCanRun) {
|
||||
return this.executeWithConstraintTracking(task, input, applicableGroups);
|
||||
}
|
||||
|
||||
// Blocked → enqueue with deferred promise
|
||||
const deferred = plugins.smartpromise.defer<any>();
|
||||
this.constraintQueue.push({ task, input, deferred });
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
private async executeWithConstraintTracking(
|
||||
task: Task<any, any, any>,
|
||||
input: any,
|
||||
groups: Array<{ group: TaskConstraintGroup<any>; key: string }>,
|
||||
): Promise<any> {
|
||||
// Acquire slots
|
||||
for (const { group, key } of groups) {
|
||||
group.acquireSlot(key);
|
||||
}
|
||||
|
||||
try {
|
||||
return await task.trigger(input);
|
||||
} finally {
|
||||
// Release slots
|
||||
for (const { group, key } of groups) {
|
||||
group.releaseSlot(key);
|
||||
}
|
||||
this.drainConstraintQueue();
|
||||
}
|
||||
}
|
||||
|
||||
private drainConstraintQueue(): void {
|
||||
let shortestCooldown = Infinity;
|
||||
const stillQueued: IConstrainedTaskEntry[] = [];
|
||||
|
||||
for (const entry of this.constraintQueue) {
|
||||
const applicableGroups: Array<{ group: TaskConstraintGroup<any>; key: string }> = [];
|
||||
for (const group of this.constraintGroups) {
|
||||
const key = group.getConstraintKey(entry.task);
|
||||
if (key !== null) {
|
||||
applicableGroups.push({ group, key });
|
||||
}
|
||||
}
|
||||
|
||||
// No constraints apply anymore (group removed?) → run directly
|
||||
if (applicableGroups.length === 0) {
|
||||
entry.task.trigger(entry.input).then(
|
||||
(result) => entry.deferred.resolve(result),
|
||||
(err) => entry.deferred.reject(err),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
const allCanRun = applicableGroups.every(({ group, key }) => group.canRun(key));
|
||||
if (allCanRun) {
|
||||
this.executeWithConstraintTracking(entry.task, entry.input, applicableGroups).then(
|
||||
(result) => entry.deferred.resolve(result),
|
||||
(err) => entry.deferred.reject(err),
|
||||
);
|
||||
} else {
|
||||
stillQueued.push(entry);
|
||||
// Track shortest cooldown for timer scheduling
|
||||
for (const { group, key } of applicableGroups) {
|
||||
const remaining = group.getCooldownRemaining(key);
|
||||
if (remaining > 0 && remaining < shortestCooldown) {
|
||||
shortestCooldown = remaining;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.constraintQueue = stillQueued;
|
||||
|
||||
// Schedule next drain if there are cooldown-blocked entries
|
||||
if (this.drainTimer) {
|
||||
clearTimeout(this.drainTimer);
|
||||
this.drainTimer = null;
|
||||
}
|
||||
if (stillQueued.length > 0 && shortestCooldown < Infinity) {
|
||||
this.drainTimer = setTimeout(() => {
|
||||
this.drainTimer = null;
|
||||
this.drainConstraintQueue();
|
||||
}, shortestCooldown + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public async triggerTaskByName(taskName: string): Promise<any> {
|
||||
const taskToTrigger = this.getTaskByName(taskName);
|
||||
if (!taskToTrigger) {
|
||||
throw new Error(`No task with the name ${taskName} found.`);
|
||||
}
|
||||
return taskToTrigger.trigger();
|
||||
return this.triggerTaskConstrained(taskToTrigger);
|
||||
}
|
||||
|
||||
public async triggerTask(task: Task<any, any>) {
|
||||
return task.trigger();
|
||||
public async triggerTask(task: Task<any, any, any>) {
|
||||
return this.triggerTaskConstrained(task);
|
||||
}
|
||||
|
||||
public scheduleTaskByName(taskName: string, cronString: string) {
|
||||
@@ -80,7 +199,7 @@ export class TaskManager {
|
||||
this.handleTaskScheduling(taskToSchedule, cronString);
|
||||
}
|
||||
|
||||
private handleTaskScheduling(task: Task<any, any>, cronString: string) {
|
||||
private handleTaskScheduling(task: Task<any, any, any>, cronString: string) {
|
||||
const cronJob = this.cronJobManager.addCronjob(
|
||||
cronString,
|
||||
async (triggerTime: number) => {
|
||||
@@ -98,7 +217,7 @@ export class TaskManager {
|
||||
}
|
||||
}
|
||||
try {
|
||||
await task.trigger();
|
||||
await this.triggerTaskConstrained(task);
|
||||
} catch (err) {
|
||||
logger.log('error', `TaskManager: scheduled task "${task.name || 'unnamed'}" failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
@@ -107,7 +226,7 @@ export class TaskManager {
|
||||
task.cronJob = cronJob;
|
||||
}
|
||||
|
||||
private logTaskState(task: Task<any, any>) {
|
||||
private logTaskState(task: Task<any, any, any>) {
|
||||
logger.log('info', `Taskbuffer schedule triggered task >>${task.name}<<`);
|
||||
const bufferState = task.buffered
|
||||
? `buffered with max ${task.bufferMax} buffered calls`
|
||||
@@ -116,7 +235,7 @@ export class TaskManager {
|
||||
}
|
||||
|
||||
private async performDistributedConsultation(
|
||||
task: Task<any, any>,
|
||||
task: Task<any, any, any>,
|
||||
triggerTime: number,
|
||||
): Promise<IDistributedTaskRequestResult> {
|
||||
logger.log('info', 'Found a distributed coordinator, performing consultation.');
|
||||
@@ -144,7 +263,7 @@ export class TaskManager {
|
||||
}
|
||||
}
|
||||
|
||||
public async descheduleTask(task: Task<any, any>) {
|
||||
public async descheduleTask(task: Task<any, any, any>) {
|
||||
await this.descheduleTaskByName(task.name);
|
||||
}
|
||||
|
||||
@@ -169,6 +288,10 @@ export class TaskManager {
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
this.taskSubscriptions.clear();
|
||||
if (this.drainTimer) {
|
||||
clearTimeout(this.drainTimer);
|
||||
this.drainTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Get metadata for a specific task
|
||||
@@ -186,7 +309,7 @@ export class TaskManager {
|
||||
// Get scheduled tasks with their schedules and next run times
|
||||
public getScheduledTasks(): IScheduledTaskInfo[] {
|
||||
const scheduledTasks: IScheduledTaskInfo[] = [];
|
||||
|
||||
|
||||
for (const task of this.taskMap.getArray()) {
|
||||
if (task.cronJob) {
|
||||
scheduledTasks.push({
|
||||
@@ -199,7 +322,7 @@ export class TaskManager {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return scheduledTasks;
|
||||
}
|
||||
|
||||
@@ -213,11 +336,11 @@ export class TaskManager {
|
||||
}))
|
||||
.sort((a, b) => a.nextRun.getTime() - b.nextRun.getTime())
|
||||
.slice(0, limit);
|
||||
|
||||
|
||||
return scheduledRuns;
|
||||
}
|
||||
|
||||
public getTasksByLabel(key: string, value: string): Task<any, any>[] {
|
||||
public getTasksByLabel(key: string, value: string): Task<any, any, any>[] {
|
||||
return this.taskMap.getArray().filter(task => task.labels[key] === value);
|
||||
}
|
||||
|
||||
@@ -227,7 +350,7 @@ export class TaskManager {
|
||||
|
||||
// Add, execute, and remove a task while collecting metadata
|
||||
public async addExecuteRemoveTask<T, TSteps extends ReadonlyArray<{ name: string; description: string; percentage: number }>>(
|
||||
task: Task<T, TSteps>,
|
||||
task: Task<T, TSteps, any>,
|
||||
options?: {
|
||||
schedule?: string;
|
||||
trackProgress?: boolean;
|
||||
@@ -235,19 +358,18 @@ export class TaskManager {
|
||||
): Promise<ITaskExecutionReport> {
|
||||
// Add task to manager
|
||||
this.addTask(task);
|
||||
|
||||
|
||||
// Optionally schedule it
|
||||
if (options?.schedule) {
|
||||
this.scheduleTaskByName(task.name!, options.schedule);
|
||||
}
|
||||
|
||||
|
||||
const startTime = Date.now();
|
||||
const progressUpdates: Array<{ stepName: string; timestamp: number }> = [];
|
||||
|
||||
|
||||
try {
|
||||
// Execute the task
|
||||
const result = await task.trigger();
|
||||
|
||||
// Execute the task through constraints
|
||||
const result = await this.triggerTaskConstrained(task);
|
||||
|
||||
// Collect execution report
|
||||
const report: ITaskExecutionReport = {
|
||||
taskName: task.name || 'unnamed',
|
||||
@@ -261,15 +383,15 @@ export class TaskManager {
|
||||
progress: task.getProgress(),
|
||||
result,
|
||||
};
|
||||
|
||||
|
||||
// Remove task from manager
|
||||
this.removeTask(task);
|
||||
|
||||
|
||||
// Deschedule if it was scheduled
|
||||
if (options?.schedule && task.name) {
|
||||
this.descheduleTaskByName(task.name);
|
||||
}
|
||||
|
||||
|
||||
return report;
|
||||
} catch (error) {
|
||||
// Create error report
|
||||
@@ -285,15 +407,15 @@ export class TaskManager {
|
||||
progress: task.getProgress(),
|
||||
error: error as Error,
|
||||
};
|
||||
|
||||
|
||||
// Remove task from manager even on error
|
||||
this.removeTask(task);
|
||||
|
||||
|
||||
// Deschedule if it was scheduled
|
||||
if (options?.schedule && task.name) {
|
||||
this.descheduleTaskByName(task.name);
|
||||
}
|
||||
|
||||
|
||||
throw errorReport;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
import * as plugins from './taskbuffer.plugins.js';
|
||||
|
||||
import { Task } from './taskbuffer.classes.task.js';
|
||||
import { logger } from './taskbuffer.logging.js';
|
||||
|
||||
export class TaskRunner {
|
||||
public maxParallelJobs: number = 1;
|
||||
public status: 'stopped' | 'running' = 'stopped';
|
||||
public runningTasks: plugins.lik.ObjectMap<Task> =
|
||||
new plugins.lik.ObjectMap<Task>();
|
||||
public queuedTasks: Task[] = [];
|
||||
|
||||
constructor() {
|
||||
this.runningTasks.eventSubject.subscribe(async (eventArg) => {
|
||||
this.checkExecution();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a task to the queue
|
||||
*/
|
||||
public addTask(taskArg: Task) {
|
||||
this.queuedTasks.push(taskArg);
|
||||
this.checkExecution();
|
||||
}
|
||||
|
||||
/**
|
||||
* set amount of parallel tasks
|
||||
* be careful, you might lose dependability of tasks
|
||||
*/
|
||||
public setMaxParallelJobs(maxParallelJobsArg: number) {
|
||||
this.maxParallelJobs = maxParallelJobsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* starts the task queue
|
||||
*/
|
||||
public async start() {
|
||||
this.status = 'running';
|
||||
}
|
||||
|
||||
/**
|
||||
* checks whether execution is on point
|
||||
*/
|
||||
public async checkExecution() {
|
||||
if (
|
||||
this.runningTasks.getArray().length < this.maxParallelJobs &&
|
||||
this.status === 'running' &&
|
||||
this.queuedTasks.length > 0
|
||||
) {
|
||||
const nextJob = this.queuedTasks.shift();
|
||||
this.runningTasks.add(nextJob);
|
||||
try {
|
||||
await nextJob.trigger();
|
||||
} catch (err) {
|
||||
logger.log('error', `TaskRunner: task "${nextJob.name || 'unnamed'}" failed: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
this.runningTasks.remove(nextJob);
|
||||
this.checkExecution();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* stops the task queue
|
||||
*/
|
||||
public async stop() {
|
||||
this.status = 'stopped';
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,18 @@
|
||||
import type { ITaskStep } from './taskbuffer.classes.taskstep.js';
|
||||
import type { Task } from './taskbuffer.classes.task.js';
|
||||
|
||||
export interface ITaskConstraintGroupOptions<TData extends Record<string, unknown> = Record<string, unknown>> {
|
||||
name: string;
|
||||
constraintKeyForTask: (task: Task<any, any, TData>) => string | null | undefined;
|
||||
maxConcurrent?: number; // default: Infinity
|
||||
cooldownMs?: number; // default: 0
|
||||
}
|
||||
|
||||
export interface IConstrainedTaskEntry {
|
||||
task: Task<any, any, any>;
|
||||
input: any;
|
||||
deferred: import('@push.rocks/smartpromise').Deferred<any>;
|
||||
}
|
||||
|
||||
export interface ITaskMetadata {
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user