Skip to content

datapartyjs/tasker

Repository files navigation

Tasker

stablelicense

Tasker is a parallel task runner with dependency resolution and results collection.

Design

Tasker provides a Runner class which manages depedencies, tasks and results. The runner class utilizes the dependency-solver npm package. When possible upto Runner.parallel foreground tasks will be run at the same time. When background tasks are added to the Runner they are started immeditaly and do not count against the parallel limit.

Tasks are added to the Runner by calling Runner.addTask(task). Every Runner.planningIntervalMs added tasks have their dependencies reveiwed and the schedule is updated. Tasks are scheduled in order of:

  1. Order of calls to Runner.addTask(task)
  2. Task with no dependencies
  3. Task with depenedencies, in order after solving usage graph

As tasks are completed they can resolve with a result (or not). Any task that has defined dependencies will receive a reference to the task they depend upon in the depends argument passed to Task.exec({task, depends})

Consumers of the library are expected to extend the Task class to later instantiate and add instances to a runner. Tasks are added by calling Runner.addTask(task).

For more details see documentation:

Foreground Tasks

By default tasks are in the foreground. Tasks can be defined either with a function or by subclassing. See a complete tutorial.

Define task using function

let sleepThirty = async ()=>{
  return new Promise((resolve,reject)=>{
    setTimeout(resolve, 30*1000)
  })
}

let myTask = Tasker.Task({
  name: 'sleep-30',
  exec: sleepThirty
})

runner.addTask(myTask)

Define task with subclass

class SleepTask extends Tasker.Task {
  constructor(durationMs){
    this.duration = durationMs
    this.timeout = null
  }

  async exec(){
    return new Promise((resolve,reject)=>{

        this.timeout = setTimeout(this.onTimeout.bind(this), 30*1000)

      })
    }
  }

  onTimeout(){
    this.timeout = null
    console.log('sleep complete')
  }

  stop(){
    if(this.timeout !== null){
      clearTimeout(this.timeout)
      this.timeout = null
    }
  }
}


let sleepThirty = new SleepTaks(30*1000)

runner.addTask(sleepThirty)
runner.start()

Background Tasks

Background tasks do not count against the parallel task limit. On failure background tasks are restarted immediatly and will be kept running indefinitly. Background tasks only ever stop if they are explicitly cancelled.

How to implement a background task