taskbuffer/dist/taskbuffer.classes.taskmanager.js

83 lines
4.7 KiB
JavaScript
Raw Normal View History

2017-01-20 20:36:18 +00:00
"use strict";
2017-06-09 21:26:23 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2017-06-17 14:56:33 +00:00
const plugins = require("./taskbuffer.plugins");
class TaskManager {
constructor() {
this.taskMap = new plugins.lik.Objectmap();
2017-06-18 11:25:09 +00:00
this.cronJobMap = new plugins.lik.Objectmap();
2017-06-17 14:56:33 +00:00
// nothing here
}
/**
* checks if a task is already present
* @param taskNameArg
*/
getTaskByName(taskNameArg) {
return this.taskMap.find((itemArg) => {
return itemArg.name === taskNameArg;
});
}
/**
* adds a Task to the TaskManager
* @param taskArg
*/
addTask(taskArg) {
if (!taskArg.name) {
throw new Error('taskArg needs a name to be added to taskManager');
}
this.taskMap.add(taskArg);
}
/**
* adds and schedules a task at once
* @param taskArg
* @param cronStringArg
*/
addAndScheduleTask(taskArg, cronStringArg) {
this.addTask(taskArg);
let taskName = taskArg.name;
this.scheduleTaskByName(taskName, cronStringArg);
}
/**
* triggers a task in the TaskManagerByName
* @param taskNameArg
*/
triggerTaskByName(taskNameArg) {
let taskToTrigger = this.getTaskByName(taskNameArg);
if (!taskToTrigger) {
throw new Error(`There is no task with the name of ${taskNameArg}`);
}
2017-06-18 11:25:09 +00:00
return taskToTrigger.trigger();
2017-06-17 14:56:33 +00:00
}
/**
* schedules the task by name
* @param taskNameArg
*/
scheduleTaskByName(taskNameArg, cronStringArg) {
let taskToSchedule = this.getTaskByName(taskNameArg);
let job = new plugins.cron.CronJob({
cronTime: cronStringArg,
2017-06-18 11:25:09 +00:00
onTick: () => {
this.triggerTaskByName(taskNameArg);
},
2017-06-17 14:56:33 +00:00
start: true
});
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
});
}
2017-06-18 11:25:09 +00:00
descheduleTaskByName(taskNameArg) {
let descheduledCron = this.cronJobMap.findOneAndRemove((itemArg) => {
return itemArg.taskNameArg === taskNameArg;
});
descheduledCron.job.stop();
}
2017-06-17 14:56:33 +00:00
/**
* returns all schedules of a specific task
* @param taskNameArg
*/
getSchedulesForTaskName(taskNameArg) {
}
}
exports.TaskManager = TaskManager;
2017-06-18 11:25:09 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLnRhc2ttYW5hZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvdGFza2J1ZmZlci5jbGFzc2VzLnRhc2ttYW5hZ2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsZ0RBQStDO0FBTS9DO0lBR0U7UUFGQSxZQUFPLEdBQUcsSUFBSSxPQUFPLENBQUMsR0FBRyxDQUFDLFNBQVMsRUFBUSxDQUFBO1FBQ25DLGVBQVUsR0FBRyxJQUFJLE9BQU8sQ0FBQyxHQUFHLENBQUMsU0FBUyxFQUFZLENBQUE7UUFFeEQsZUFBZTtJQUNqQixDQUFDO0lBRUQ7OztPQUdHO0lBQ0gsYUFBYSxDQUFFLFdBQVc7UUFDeEIsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUMsT0FBTztZQUMvQixNQUFNLENBQUMsT0FBTyxDQUFDLElBQUksS0FBSyxXQUFXLENBQUE7UUFDckMsQ0FBQyxDQUFDLENBQUE7SUFDSixDQUFDO0lBRUQ7OztPQUdHO0lBQ0gsT0FBTyxDQUFFLE9BQWE7UUFDcEIsRUFBRSxDQUFDLENBQUMsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQztZQUNsQixNQUFNLElBQUksS0FBSyxDQUFDLGlEQUFpRCxDQUFDLENBQUE7UUFDcEUsQ0FBQztRQUNELElBQUksQ0FBQyxPQUFPLENBQUMsR0F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