now has working TaskManager

This commit is contained in:
Philipp Kunz 2017-06-18 13:25:09 +02:00
parent ca3bb38205
commit 2ed701040a
9 changed files with 78 additions and 17 deletions

1
dist/index.d.ts vendored
View File

@ -1,4 +1,5 @@
export { Task, ITaskFunction } from './taskbuffer.classes.task'; export { Task, ITaskFunction } from './taskbuffer.classes.task';
export { Taskchain } from './taskbuffer.classes.taskchain'; export { Taskchain } from './taskbuffer.classes.taskchain';
export { Taskparallel } from './taskbuffer.classes.taskparallel'; export { Taskparallel } from './taskbuffer.classes.taskparallel';
export { TaskManager } from './taskbuffer.classes.taskmanager';
import './taskbuffer.classes.helpers'; import './taskbuffer.classes.helpers';

4
dist/index.js vendored
View File

@ -6,6 +6,8 @@ var taskbuffer_classes_taskchain_1 = require("./taskbuffer.classes.taskchain");
exports.Taskchain = taskbuffer_classes_taskchain_1.Taskchain; exports.Taskchain = taskbuffer_classes_taskchain_1.Taskchain;
var taskbuffer_classes_taskparallel_1 = require("./taskbuffer.classes.taskparallel"); var taskbuffer_classes_taskparallel_1 = require("./taskbuffer.classes.taskparallel");
exports.Taskparallel = taskbuffer_classes_taskparallel_1.Taskparallel; exports.Taskparallel = taskbuffer_classes_taskparallel_1.Taskparallel;
var taskbuffer_classes_taskmanager_1 = require("./taskbuffer.classes.taskmanager");
exports.TaskManager = taskbuffer_classes_taskmanager_1.TaskManager;
// import for naming only // import for naming only
require("./taskbuffer.classes.helpers"); require("./taskbuffer.classes.helpers");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHFFQUE0RDtBQUFwRCx5Q0FBQSxJQUFJLENBQUE7QUFDWiwrRUFBd0Q7QUFBaEQsbURBQUEsU0FBUyxDQUFBO0FBQ2pCLHFGQUE4RDtBQUF0RCx5REFBQSxZQUFZLENBQUE7QUFFcEIseUJBQXlCO0FBQ3pCLHdDQUFxQyJ9 //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHFFQUE0RDtBQUFwRCx5Q0FBQSxJQUFJLENBQUE7QUFDWiwrRUFBd0Q7QUFBaEQsbURBQUEsU0FBUyxDQUFBO0FBQ2pCLHFGQUE4RDtBQUF0RCx5REFBQSxZQUFZLENBQUE7QUFDcEIsbUZBQThEO0FBQXJELHVEQUFBLFdBQVcsQ0FBQTtBQUVwQix5QkFBeUI7QUFDekIsd0NBQXFDIn0=

View File

@ -2,7 +2,7 @@ import { Task } from './taskbuffer.classes.task';
import { Objectmap } from 'lik'; import { Objectmap } from 'lik';
export declare class TaskManager { export declare class TaskManager {
taskMap: Objectmap<Task>; taskMap: Objectmap<Task>;
private cronJobArray; private cronJobMap;
constructor(); constructor();
/** /**
* checks if a task is already present * checks if a task is already present
@ -24,12 +24,13 @@ export declare class TaskManager {
* triggers a task in the TaskManagerByName * triggers a task in the TaskManagerByName
* @param taskNameArg * @param taskNameArg
*/ */
triggerTaskByName(taskNameArg: string): void; triggerTaskByName(taskNameArg: string): Promise<any>;
/** /**
* schedules the task by name * schedules the task by name
* @param taskNameArg * @param taskNameArg
*/ */
scheduleTaskByName(taskNameArg: string, cronStringArg: string): void; scheduleTaskByName(taskNameArg: string, cronStringArg: string): void;
descheduleTaskByName(taskNameArg: string): void;
/** /**
* returns all schedules of a specific task * returns all schedules of a specific task
* @param taskNameArg * @param taskNameArg

View File

@ -4,7 +4,7 @@ const plugins = require("./taskbuffer.plugins");
class TaskManager { class TaskManager {
constructor() { constructor() {
this.taskMap = new plugins.lik.Objectmap(); this.taskMap = new plugins.lik.Objectmap();
this.cronJobArray = []; this.cronJobMap = new plugins.lik.Objectmap();
// nothing here // nothing here
} }
/** /**
@ -45,7 +45,7 @@ class TaskManager {
if (!taskToTrigger) { if (!taskToTrigger) {
throw new Error(`There is no task with the name of ${taskNameArg}`); throw new Error(`There is no task with the name of ${taskNameArg}`);
} }
taskToTrigger.trigger(); return taskToTrigger.trigger();
} }
/** /**
* schedules the task by name * schedules the task by name
@ -55,15 +55,23 @@ class TaskManager {
let taskToSchedule = this.getTaskByName(taskNameArg); let taskToSchedule = this.getTaskByName(taskNameArg);
let job = new plugins.cron.CronJob({ let job = new plugins.cron.CronJob({
cronTime: cronStringArg, cronTime: cronStringArg,
onTick: taskToSchedule.trigger, onTick: () => {
this.triggerTaskByName(taskNameArg);
},
start: true start: true
}); });
this.cronJobArray.push({ this.cronJobMap.add({
taskNameArg: taskToSchedule.name, taskNameArg: taskToSchedule.name,
cronString: cronStringArg, cronString: cronStringArg,
job: job job: job
}); });
} }
descheduleTaskByName(taskNameArg) {
let descheduledCron = this.cronJobMap.findOneAndRemove((itemArg) => {
return itemArg.taskNameArg === taskNameArg;
});
descheduledCron.job.stop();
}
/** /**
* returns all schedules of a specific task * returns all schedules of a specific task
* @param taskNameArg * @param taskNameArg
@ -72,4 +80,4 @@ class TaskManager {
} }
} }
exports.TaskManager = TaskManager; exports.TaskManager = TaskManager;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLnRhc2ttYW5hZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvdGFza2J1ZmZlci5jbGFzc2VzLnRhc2ttYW5hZ2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsZ0RBQStDO0FBTS9DO0lBR0U7UUFGQSxZQUFPLEdBQUcsSUFBSSxPQUFPLENBQUMsR0FBRyxDQUFDLFNBQVMsRUFBUSxDQUFBO1FBQ25DLGlCQUFZLEdBQWUsRUFBRSxDQUFBO1FBRW5D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"^6.1.10",
"cron": "^1.2.1", "cron": "^1.2.1",
"lik": "^1.0.31", "lik": "^1.0.32",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"rxjs": "^5.4.1", "rxjs": "^5.4.1",
"smartdelay": "^1.0.3", "smartdelay": "^1.0.3",

View File

@ -0,0 +1,40 @@
import { expect, tap } from 'tapbundle'
import taskbuffer = require('../dist/index')
import * as smartq from 'smartq'
import * as smartdelay from 'smartdelay'
let myTaskManager: taskbuffer.TaskManager
let taskRunCounter = 0
let taskDone = smartq.defer()
tap.test('should create an instance of TaskManager', async () => {
myTaskManager = new taskbuffer.TaskManager()
expect(myTaskManager).to.be.instanceof(taskbuffer.TaskManager)
})
tap.test('should run the task as expected', async () => {
let referenceBoolean = false
myTaskManager.addTask(new taskbuffer.Task({
name: 'myTask',
taskFunction: async () => {
console.log('Task executed!')
referenceBoolean = true
taskRunCounter++
if (taskRunCounter === 10) {
taskDone.resolve()
}
}
}))
await myTaskManager.triggerTaskByName('myTask')
// tslint:disable-next-line:no-unused-expression
expect(referenceBoolean).to.be.true
})
tap.test('should schedule task', async () => {
myTaskManager.scheduleTaskByName('myTask', '* * * * * *')
await taskDone.promise
myTaskManager.descheduleTaskByName('myTask')
})
tap.start()

View File

@ -1,6 +1,7 @@
export {Task,ITaskFunction} from './taskbuffer.classes.task' export {Task,ITaskFunction} from './taskbuffer.classes.task'
export {Taskchain} from './taskbuffer.classes.taskchain' export {Taskchain} from './taskbuffer.classes.taskchain'
export {Taskparallel} from './taskbuffer.classes.taskparallel' export {Taskparallel} from './taskbuffer.classes.taskparallel'
export { TaskManager } from './taskbuffer.classes.taskmanager'
// import for naming only // import for naming only
import './taskbuffer.classes.helpers' import './taskbuffer.classes.helpers'

View File

@ -6,7 +6,7 @@ import { Objectmap } from 'lik'
export class TaskManager { export class TaskManager {
taskMap = new plugins.lik.Objectmap<Task>() taskMap = new plugins.lik.Objectmap<Task>()
private cronJobArray: ICronJob[] = [] private cronJobMap = new plugins.lik.Objectmap<ICronJob>()
constructor () { constructor () {
// nothing here // nothing here
} }
@ -47,12 +47,12 @@ export class TaskManager {
* triggers a task in the TaskManagerByName * triggers a task in the TaskManagerByName
* @param taskNameArg * @param taskNameArg
*/ */
triggerTaskByName (taskNameArg: string) { triggerTaskByName (taskNameArg: string): Promise<any> {
let taskToTrigger = this.getTaskByName(taskNameArg) let taskToTrigger = this.getTaskByName(taskNameArg)
if (!taskToTrigger) { if (!taskToTrigger) {
throw new Error(`There is no task with the name of ${taskNameArg}`) throw new Error(`There is no task with the name of ${taskNameArg}`)
} }
taskToTrigger.trigger() return taskToTrigger.trigger()
} }
/** /**
@ -63,16 +63,24 @@ export class TaskManager {
let taskToSchedule = this.getTaskByName(taskNameArg) let taskToSchedule = this.getTaskByName(taskNameArg)
let job = new plugins.cron.CronJob({ let job = new plugins.cron.CronJob({
cronTime: cronStringArg, cronTime: cronStringArg,
onTick: taskToSchedule.trigger, onTick: () => {
this.triggerTaskByName(taskNameArg)
},
start: true start: true
}) })
this.cronJobArray.push({ this.cronJobMap.add({
taskNameArg: taskToSchedule.name, taskNameArg: taskToSchedule.name,
cronString: cronStringArg, cronString: cronStringArg,
job: job job: job
}) })
} }
descheduleTaskByName (taskNameArg: string) {
let descheduledCron = this.cronJobMap.findOneAndRemove((itemArg) => {
return itemArg.taskNameArg === taskNameArg
})
descheduledCron.job.stop()
}
/** /**
* returns all schedules of a specific task * returns all schedules of a specific task
* @param taskNameArg * @param taskNameArg

View File

@ -236,9 +236,9 @@ left-pad@^1.1.3:
version "1.1.3" version "1.1.3"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a" resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a"
lik@^1.0.31: lik@^1.0.32:
version "1.0.31" version "1.0.32"
resolved "https://registry.yarnpkg.com/lik/-/lik-1.0.31.tgz#5c7635d880cdbb28aeb1259223fd948797616bcd" resolved "https://registry.yarnpkg.com/lik/-/lik-1.0.32.tgz#41ee6c8edd483eaa11bd089775263955f5555060"
dependencies: dependencies:
"@types/lodash" "^4.14.62" "@types/lodash" "^4.14.62"
"@types/minimatch" "2.x.x" "@types/minimatch" "2.x.x"