taskbuffer/ts/taskbuffer.classes.taskmanager.ts

168 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-03-25 11:14:49 +00:00
import * as plugins from './taskbuffer.plugins.js';
import { Task } from './taskbuffer.classes.task.js';
2023-01-09 17:40:38 +00:00
import { AbstractDistributedCoordinator } from './taskbuffer.classes.distributedcoordinator.js';
2017-06-17 14:56:33 +00:00
2019-09-08 20:45:36 +00:00
export interface ICronJob {
cronString: string;
taskNameArg: string;
job: any;
}
2017-06-17 14:56:33 +00:00
2022-11-14 13:54:26 +00:00
export interface ITaskManagerConstructorOptions {
2023-07-26 12:16:33 +00:00
distributedCoordinator?: AbstractDistributedCoordinator;
2022-11-14 13:54:26 +00:00
}
2017-06-17 14:56:33 +00:00
export class TaskManager {
2023-01-07 18:05:29 +00:00
public randomId = plugins.isounique.uni();
2020-07-12 00:48:51 +00:00
public taskMap = new plugins.lik.ObjectMap<Task>();
private cronJobManager = new plugins.smarttime.CronManager();
2022-11-14 13:54:26 +00:00
public options: ITaskManagerConstructorOptions = {
2023-07-26 12:16:33 +00:00
distributedCoordinator: null,
2022-11-14 13:54:26 +00:00
};
2023-08-04 09:58:53 +00:00
constructor(optionsArg: ITaskManagerConstructorOptions = {}) {
this.options = Object.assign(this.options, optionsArg);
2017-06-17 14:56:33 +00:00
}
/**
* checks if a task is already present
* @param taskNameArg
*/
2021-09-26 12:45:02 +00:00
public getTaskByName(taskNameArg: string): Task {
return this.taskMap.findSync((itemArg) => {
2018-08-04 15:53:22 +00:00
return itemArg.name === taskNameArg;
});
2017-06-17 14:56:33 +00:00
}
/**
* adds a Task to the TaskManager
* @param taskArg
*/
2019-09-05 09:35:26 +00:00
public addTask(taskArg: Task): void {
2017-06-17 14:56:33 +00:00
if (!taskArg.name) {
2018-08-04 15:53:22 +00:00
throw new Error('taskArg needs a name to be added to taskManager');
2017-06-17 14:56:33 +00:00
}
2018-08-04 15:53:22 +00:00
this.taskMap.add(taskArg);
2017-06-17 14:56:33 +00:00
}
/**
* adds and schedules a task at once
* @param taskArg
* @param cronStringArg
*/
2019-09-05 09:35:26 +00:00
public addAndScheduleTask(taskArg: Task, cronStringArg: string) {
2018-08-04 15:53:22 +00:00
this.addTask(taskArg);
2020-09-02 13:46:26 +00:00
this.scheduleTaskByName(taskArg.name, cronStringArg);
2017-06-17 14:56:33 +00:00
}
/**
* triggers a task in the TaskManagerByName
* @param taskNameArg
*/
2019-09-05 09:35:26 +00:00
public triggerTaskByName(taskNameArg: string): Promise<any> {
const taskToTrigger = this.getTaskByName(taskNameArg);
2017-06-17 14:56:33 +00:00
if (!taskToTrigger) {
2018-08-04 15:53:22 +00:00
throw new Error(`There is no task with the name of ${taskNameArg}`);
2017-06-17 14:56:33 +00:00
}
2018-08-04 15:53:22 +00:00
return taskToTrigger.trigger();
2017-06-17 14:56:33 +00:00
}
2019-09-08 20:45:36 +00:00
public async triggerTask(task: Task) {
return task.trigger();
}
2017-06-17 14:56:33 +00:00
/**
* schedules the task by name
* @param taskNameArg
*/
2019-09-05 09:35:26 +00:00
public scheduleTaskByName(taskNameArg: string, cronStringArg: string) {
const taskToSchedule = this.getTaskByName(taskNameArg);
2023-07-26 12:16:33 +00:00
const cronJob = this.cronJobManager.addCronjob(
cronStringArg,
async (triggerTimeArg: number) => {
console.log(`taskbuffer schedule triggered task >>${taskToSchedule.name}<<`);
console.log(
`task >>${taskToSchedule.name}<< is ${
taskToSchedule.buffered
? `buffered with max ${taskToSchedule.bufferMax} buffered calls`
: `unbuffered`
}`
);
if (this.options.distributedCoordinator) {
console.log(`Found a distrubuted coordinator, performing distributed consultation.`);
const announcementResult =
await this.options.distributedCoordinator.fireDistributedTaskRequest({
submitterRandomId: this.randomId,
status: 'requesting',
taskExecutionParallel: 1,
taskExecutionTime: triggerTimeArg,
taskExecutionTimeout: taskToSchedule.timeout,
taskName: taskToSchedule.name,
taskVersion: taskToSchedule.version,
});
if (!announcementResult.shouldTrigger) {
console.log('distributed coordinator result: NOT EXECUTING');
return;
} else {
console.log('distributed coordinator result: CHOSEN AND EXECUTING');
}
2023-01-07 18:05:29 +00:00
}
2023-07-26 12:16:33 +00:00
await taskToSchedule.trigger();
2022-11-14 13:54:26 +00:00
}
2023-07-26 12:16:33 +00:00
);
2020-07-12 00:48:51 +00:00
taskToSchedule.cronJob = cronJob;
2017-06-17 14:56:33 +00:00
}
2019-09-08 20:45:36 +00:00
/**
* deschedules a task by name
* @param taskNameArg
2019-09-08 20:45:36 +00:00
*/
2019-09-05 09:35:26 +00:00
public descheduleTaskByName(taskNameArg: string) {
2020-07-12 00:48:51 +00:00
const taskToDeSchedule = this.getTaskByName(taskNameArg);
if (taskToDeSchedule.cronJob) {
this.cronJobManager.removeCronjob(taskToDeSchedule.cronJob);
taskToDeSchedule.cronJob = null;
}
if (this.cronJobManager.cronjobs.isEmpty) {
this.cronJobManager.stop();
}
2017-06-18 11:25:09 +00:00
}
2019-09-08 20:45:36 +00:00
/**
* deschedules a task
* @param task
*/
public async descheduleTask(task: Task) {
await this.descheduleTaskByName(task.name);
}
2023-08-04 09:58:53 +00:00
2017-06-17 14:56:33 +00:00
/**
2023-08-04 09:58:53 +00:00
* 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;
}
2020-09-02 13:04:12 +00:00
/**
* starts the taskmanager
*/
public start() {
this.cronJobManager.start();
}
/**
* stops the taskmanager
*/
public stop() {
this.cronJobManager.stop();
}
2017-06-17 14:56:33 +00:00
}