taskbuffer/ts/taskbuffer.classes.taskmanager.ts

156 lines
4.3 KiB
TypeScript
Raw Permalink 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-01-09 17:40:38 +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 = {
distributedCoordinator: null
};
2023-01-07 18:05:29 +00:00
constructor(optionosArg: ITaskManagerConstructorOptions = {}) {
2022-11-14 13:54:26 +00:00
this.options = Object.assign(this.options, optionosArg);
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);
2022-11-14 13:54:26 +00:00
const cronJob = this.cronJobManager.addCronjob(cronStringArg, async (triggerTimeArg: number) => {
2021-11-11 19:37:27 +00:00
console.log(`taskbuffer schedule triggered task >>${taskToSchedule.name}<<`);
console.log(
`task >>${taskToSchedule.name}<< is ${
taskToSchedule.buffered
? `buffered with max ${taskToSchedule.bufferMax} buffered calls`
: `unbuffered`
}`
);
2022-11-14 13:54:26 +00:00
if (this.options.distributedCoordinator) {
console.log(`Found a distrubuted coordinator, performing distributed consultation.`);
2023-01-19 13:20:44 +00:00
const announcementResult = await this.options.distributedCoordinator.fireDistributedTaskRequest({
2023-01-07 18:05:29 +00:00
submitterRandomId: this.randomId,
2022-11-14 13:54:26 +00:00
status: 'requesting',
taskExecutionParallel: 1,
taskExecutionTime: triggerTimeArg,
taskExecutionTimeout: taskToSchedule.timeout,
taskName: taskToSchedule.name,
taskVersion: taskToSchedule.version,
2023-01-07 18:05:29 +00:00
});
if (!announcementResult.shouldTrigger) {
console.log('distributed coordinator result: NOT EXECUTING')
return;
} else {
console.log('distributed coordinator result: CHOSEN AND EXECUTING')
}
2022-11-14 13:54:26 +00:00
}
2021-11-11 19:37:27 +00:00
await taskToSchedule.trigger();
2018-08-04 15:53:22 +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);
}
2017-06-17 14:56:33 +00:00
/**
* returns all schedules of a specific task
* @param taskNameArg
*/
2019-09-05 09:35:26 +00:00
public getSchedulesForTaskName(taskNameArg: string) {}
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
}