implement TaskManager
This commit is contained in:
@ -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'
|
||||
|
@ -19,7 +19,7 @@ export class Task {
|
||||
idle: boolean = true
|
||||
private _state: string = 'ready'
|
||||
|
||||
constructor(optionsArg: {
|
||||
constructor (optionsArg: {
|
||||
taskFunction: ITaskFunction,
|
||||
preTask?: Task,
|
||||
afterTask?: Task,
|
||||
|
@ -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')
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +0,0 @@
|
||||
import * as plugins from "./taskbuffer.plugins"
|
||||
import * as helpers from "./taskbuffer.classes.helpers"
|
||||
|
||||
export class Taskspace {
|
||||
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user