feat(task): add task labels and push-based task events
This commit is contained in:
@@ -4,7 +4,7 @@ import {
|
||||
AbstractDistributedCoordinator,
|
||||
type IDistributedTaskRequestResult,
|
||||
} from './taskbuffer.classes.distributedcoordinator.js';
|
||||
import type { ITaskMetadata, ITaskExecutionReport, IScheduledTaskInfo } from './taskbuffer.interfaces.js';
|
||||
import type { ITaskMetadata, ITaskExecutionReport, IScheduledTaskInfo, ITaskEvent } from './taskbuffer.interfaces.js';
|
||||
import { logger } from './taskbuffer.logging.js';
|
||||
|
||||
export interface ICronJob {
|
||||
@@ -20,6 +20,8 @@ export interface ITaskManagerConstructorOptions {
|
||||
export class TaskManager {
|
||||
public randomId = plugins.smartunique.shortId();
|
||||
public taskMap = new plugins.lik.ObjectMap<Task<any, any>>();
|
||||
public readonly taskSubject = new plugins.smartrx.rxjs.Subject<ITaskEvent>();
|
||||
private taskSubscriptions = new Map<Task<any, any>, plugins.smartrx.rxjs.Subscription>();
|
||||
private cronJobManager = new plugins.smarttime.CronManager();
|
||||
public options: ITaskManagerConstructorOptions = {
|
||||
distributedCoordinator: null,
|
||||
@@ -38,6 +40,19 @@ export class TaskManager {
|
||||
throw new Error('Task must have a name to be added to taskManager');
|
||||
}
|
||||
this.taskMap.add(task);
|
||||
const subscription = task.eventSubject.subscribe((event) => {
|
||||
this.taskSubject.next(event);
|
||||
});
|
||||
this.taskSubscriptions.set(task, subscription);
|
||||
}
|
||||
|
||||
public removeTask(task: Task<any, any>): void {
|
||||
this.taskMap.remove(task);
|
||||
const subscription = this.taskSubscriptions.get(task);
|
||||
if (subscription) {
|
||||
subscription.unsubscribe();
|
||||
this.taskSubscriptions.delete(task);
|
||||
}
|
||||
}
|
||||
|
||||
public addAndScheduleTask(task: Task<any, any>, cronString: string) {
|
||||
@@ -150,6 +165,10 @@ export class TaskManager {
|
||||
if (this.options.distributedCoordinator) {
|
||||
await this.options.distributedCoordinator.stop();
|
||||
}
|
||||
for (const [, subscription] of this.taskSubscriptions) {
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
this.taskSubscriptions.clear();
|
||||
}
|
||||
|
||||
// Get metadata for a specific task
|
||||
@@ -198,6 +217,14 @@ export class TaskManager {
|
||||
return scheduledRuns;
|
||||
}
|
||||
|
||||
public getTasksByLabel(key: string, value: string): Task<any, any>[] {
|
||||
return this.taskMap.getArray().filter(task => task.labels[key] === value);
|
||||
}
|
||||
|
||||
public getTasksMetadataByLabel(key: string, value: string): ITaskMetadata[] {
|
||||
return this.getTasksByLabel(key, value).map(task => task.getMetadata());
|
||||
}
|
||||
|
||||
// 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>,
|
||||
@@ -236,7 +263,7 @@ export class TaskManager {
|
||||
};
|
||||
|
||||
// Remove task from manager
|
||||
this.taskMap.remove(task);
|
||||
this.removeTask(task);
|
||||
|
||||
// Deschedule if it was scheduled
|
||||
if (options?.schedule && task.name) {
|
||||
@@ -260,7 +287,7 @@ export class TaskManager {
|
||||
};
|
||||
|
||||
// Remove task from manager even on error
|
||||
this.taskMap.remove(task);
|
||||
this.removeTask(task);
|
||||
|
||||
// Deschedule if it was scheduled
|
||||
if (options?.schedule && task.name) {
|
||||
|
||||
Reference in New Issue
Block a user