taskbuffer/ts/taskbuffer.classes.taskmanager.ts

120 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-08-04 15:53:22 +00:00
import * as plugins from './taskbuffer.plugins';
import { Task } from './taskbuffer.classes.task';
2020-07-12 00:48:51 +00:00
import { threadId } from 'worker_threads';
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
export class TaskManager {
2020-07-12 00:48:51 +00:00
public taskMap = new plugins.lik.ObjectMap<Task>();
private cronJobManager = new plugins.smarttime.CronManager();
2018-08-04 15:53:22 +00:00
constructor() {
2017-06-17 14:56:33 +00:00
// nothing here
}
/**
* checks if a task is already present
* @param taskNameArg
*/
2019-09-05 09:35:26 +00:00
public getTaskByName(taskNameArg): Task {
2020-07-12 00:48:51 +00:00
return this.taskMap.find((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);
2020-07-12 00:48:51 +00:00
const cronJob = this.cronJobManager.addCronjob(cronStringArg, async () => {
await taskToSchedule.triggerBuffered();
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
}