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
|
|
|
|
|
|
|
// interfaces
|
2018-08-04 15:53:22 +00:00
|
|
|
import { Objectmap } from '@pushrocks/lik';
|
2017-06-17 14:56:33 +00:00
|
|
|
|
|
|
|
export class TaskManager {
|
2018-08-04 15:53:22 +00:00
|
|
|
taskMap = new plugins.lik.Objectmap<Task>();
|
|
|
|
private cronJobMap = new plugins.lik.Objectmap<ICronJob>();
|
|
|
|
constructor() {
|
2017-06-17 14:56:33 +00:00
|
|
|
// nothing here
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if a task is already present
|
|
|
|
* @param taskNameArg
|
|
|
|
*/
|
2018-08-04 15:53:22 +00:00
|
|
|
getTaskByName(taskNameArg): Task {
|
|
|
|
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
|
|
|
|
*/
|
2018-08-04 15:53:22 +00:00
|
|
|
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
|
|
|
|
*/
|
2018-08-04 15:53:22 +00:00
|
|
|
addAndScheduleTask(taskArg: Task, cronStringArg: string) {
|
|
|
|
this.addTask(taskArg);
|
|
|
|
let taskName = taskArg.name;
|
|
|
|
this.scheduleTaskByName(taskName, cronStringArg);
|
2017-06-17 14:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* triggers a task in the TaskManagerByName
|
|
|
|
* @param taskNameArg
|
|
|
|
*/
|
2018-08-04 15:53:22 +00:00
|
|
|
triggerTaskByName(taskNameArg: string): Promise<any> {
|
|
|
|
let 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* schedules the task by name
|
|
|
|
* @param taskNameArg
|
|
|
|
*/
|
2018-08-04 15:53:22 +00:00
|
|
|
scheduleTaskByName(taskNameArg: string, cronStringArg: string) {
|
|
|
|
let taskToSchedule = this.getTaskByName(taskNameArg);
|
2017-06-17 14:56:33 +00:00
|
|
|
let job = new plugins.cron.CronJob({
|
|
|
|
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,
|
|
|
|
job: job
|
2018-08-04 15:53:22 +00:00
|
|
|
});
|
2017-06-17 14:56:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 15:53:22 +00:00
|
|
|
descheduleTaskByName(taskNameArg: string) {
|
|
|
|
let descheduledCron = this.cronJobMap.findOneAndRemove(itemArg => {
|
|
|
|
return itemArg.taskNameArg === taskNameArg;
|
|
|
|
});
|
|
|
|
descheduledCron.job.stop();
|
2017-06-18 11:25:09 +00:00
|
|
|
}
|
2017-06-17 14:56:33 +00:00
|
|
|
/**
|
|
|
|
* returns all schedules of a specific task
|
|
|
|
* @param taskNameArg
|
|
|
|
*/
|
2018-08-04 15:53:22 +00:00
|
|
|
getSchedulesForTaskName(taskNameArg: string) {}
|
2017-06-17 14:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ICronJob {
|
2018-08-04 15:53:22 +00:00
|
|
|
cronString: string;
|
|
|
|
taskNameArg: string;
|
|
|
|
job: any;
|
2017-06-17 14:56:33 +00:00
|
|
|
}
|