taskbuffer/ts/taskbuffer.classes.taskmanager.ts

110 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2018-08-04 15:53:22 +00:00
import * as plugins from './taskbuffer.plugins';
import { Task } from './taskbuffer.classes.task';
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 {
2019-09-05 09:35:26 +00:00
public taskMap = new plugins.lik.Objectmap<Task>();
2018-08-04 15:53:22 +00:00
private cronJobMap = new plugins.lik.Objectmap<ICronJob>();
2019-09-08 20:45:36 +00:00
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 {
2018-08-04 15:53:22 +00:00
return this.taskMap.find(itemArg => {
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);
2019-09-05 09:35:26 +00:00
const taskName = taskArg.name;
2018-08-04 15:53:22 +00:00
this.scheduleTaskByName(taskName, 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);
const job = new plugins.cron.CronJob({
2017-06-17 14:56:33 +00:00
cronTime: cronStringArg,
2017-06-18 11:25:09 +00:00
onTick: () => {
2018-08-04 15:53:22 +00:00
this.triggerTaskByName(taskNameArg);
2017-06-18 11:25:09 +00:00
},
2017-06-17 14:56:33 +00:00
start: true
2018-08-04 15:53:22 +00:00
});
2017-06-18 11:25:09 +00:00
this.cronJobMap.add({
2017-06-17 14:56:33 +00:00
taskNameArg: taskToSchedule.name,
cronString: cronStringArg,
2019-09-08 20:45:36 +00:00
job
2018-08-04 15:53:22 +00:00
});
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-05 09:35:26 +00:00
public descheduleTaskByName(taskNameArg: string) {
const descheduledCron = this.cronJobMap.findOneAndRemove(itemArg => {
2018-08-04 15:53:22 +00:00
return itemArg.taskNameArg === taskNameArg;
});
descheduledCron.job.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) {}
2017-06-17 14:56:33 +00:00
}