implement TaskManager

This commit is contained in:
2017-06-17 16:56:33 +02:00
parent cf1b31d635
commit e1424ea0fb
28 changed files with 592 additions and 284 deletions

View File

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

View File

@ -19,7 +19,7 @@ export class Task {
idle: boolean = true
private _state: string = 'ready'
constructor(optionsArg: {
constructor (optionsArg: {
taskFunction: ITaskFunction,
preTask?: Task,
afterTask?: Task,

View File

@ -1,3 +1,6 @@
// TaskChain chains tasks
// and extends Task
import * as plugins from './taskbuffer.plugins'
import { Task } from './taskbuffer.classes.task'
import helpers = require('./taskbuffer.classes.helpers')

View File

@ -1 +1,89 @@
import * as plugins from './taskbuffer.plugins'
import { Task } from './taskbuffer.classes.task'
// interfaces
import { Objectmap } from 'lik'
export class TaskManager {
taskMap = new plugins.lik.Objectmap<Task>()
private cronJobArray: ICronJob[] = []
constructor () {
// nothing here
}
/**
* checks if a task is already present
* @param taskNameArg
*/
getTaskByName (taskNameArg): Task {
return this.taskMap.find((itemArg) => {
return itemArg.name === taskNameArg
})
}
/**
* adds a Task to the TaskManager
* @param taskArg
*/
addTask (taskArg: Task): void {
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: Task, cronStringArg: string) {
this.addTask(taskArg)
let taskName = taskArg.name
this.scheduleTaskByName(taskName, cronStringArg)
}
/**
* triggers a task in the TaskManagerByName
* @param taskNameArg
*/
triggerTaskByName (taskNameArg: string) {
let taskToTrigger = this.getTaskByName(taskNameArg)
if (!taskToTrigger) {
throw new Error(`There is no task with the name of ${taskNameArg}`)
}
taskToTrigger.trigger()
}
/**
* schedules the task by name
* @param taskNameArg
*/
scheduleTaskByName (taskNameArg: string, cronStringArg: string) {
let taskToSchedule = this.getTaskByName(taskNameArg)
let job = new plugins.cron.CronJob({
cronTime: cronStringArg,
onTick: taskToSchedule.trigger,
start: true
})
this.cronJobArray.push({
taskNameArg: taskToSchedule.name,
cronString: cronStringArg,
job: job
})
}
/**
* returns all schedules of a specific task
* @param taskNameArg
*/
getSchedulesForTaskName (taskNameArg: string) {
}
}
export interface ICronJob {
cronString: string
taskNameArg: string
job: any
}

View File

@ -1,29 +1,28 @@
import * as plugins from "./taskbuffer.plugins"
import * as helpers from "./taskbuffer.classes.helpers"
import { Task } from "./taskbuffer.classes.task"
import * as plugins from './taskbuffer.plugins'
import * as helpers from './taskbuffer.classes.helpers'
import { Task } from './taskbuffer.classes.task'
export class Taskparallel extends Task {
taskArray: Task[];
constructor(optionsArg: {
taskArray: Task[]
}){
let options = plugins.lodash.merge(
optionsArg,
{
taskFunction: () => {
let done = plugins.q.defer();
let promiseArray: Promise<any>[] = []; // stores promises of all tasks, since they run in parallel
this.taskArray.forEach(function (taskArg) {
promiseArray.push(taskArg.trigger());
})
Promise.all(promiseArray)
.then(done.resolve);
return done.promise;
}
}
);
super(options);
this.taskArray = optionsArg.taskArray;
}
taskArray: Task[]
constructor (optionsArg: {
taskArray: Task[]
}) {
let options = plugins.lodash.merge(
optionsArg,
{
taskFunction: () => {
let done = plugins.q.defer()
let promiseArray: Promise<any>[] = [] // stores promises of all tasks, since they run in parallel
this.taskArray.forEach(function (taskArg) {
promiseArray.push(taskArg.trigger())
})
Promise.all(promiseArray)
.then(done.resolve)
return done.promise
}
}
)
super(options)
this.taskArray = optionsArg.taskArray
}
}

View File

@ -1,6 +0,0 @@
import * as plugins from "./taskbuffer.plugins"
import * as helpers from "./taskbuffer.classes.helpers"
export class Taskspace {
}

View File

@ -1,12 +1,18 @@
import 'typings-global'
import * as beautylog from 'beautylog'
let cron = require('cron')
import * as lik from 'lik'
import * as lodash from 'lodash'
import * as rxjs from 'rxjs'
import * as q from 'smartq'
import * as smartdelay from 'smartdelay'
export {
beautylog,
cron,
lik,
lodash,
rxjs,
q
}
q,
smartdelay
}