task-hub

1.0.4 • Public • Published

task-hub

You know, task-hub help you run and manage your schedule running function.

NPM version

Installation

npm install task-hub

Quick start

const TaskHub = require('task-hub')
 
const taskHub = TaskHub.init()
const task = taskHub.addTask(function () {
  const { config } = this
  console.log(config.value)
  // 1
}, { value: 1 })
taskHub.start(task.id)

API

TaskHub.init()

Setup A Hub, this function will return the same instance even if you call multiple times.

addTask(function, options)

Add new task to hub

  • function: the task function
  • options: the config for task function
    • timeInterval: Time loop for task. Default is 5000
const taskConfig = {
  value: 1,
  timeInterval: 60000 // 1 minutes
}
const syncTask = function () {
  // You can access taskConfig from "this"
  const { config } = this
  console.log(config.value)
}
const asyncTask = function () {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log('Run')
      resolve()
    }, 3000)
  })
}
// add sync task
const task = taskHub.addTask(syncTask, taskConfig)
// add async task
const task = taskHub.addTask(asyncTask, taskConfig)

updateConfig(newConfig)

Update config of exist task in hub

  • newConfig: The config will be merged with exist config.
taskHub.updateConfig(task.id, {value: 2})

start(taskId)

Start the specific task in hub.

  • taskId: Id of task will be started.
taskHub.start(task.id)

stop(taskId)

Stop the specific task in hub.

  • taskId: Id of task will be stopped.
taskHub.stop(task.id)

stopAll()

Stop all task in hub.

taskHub.stopAll()

Note: If task is running, it can be stopped until it completes it's function

Events

start

Event when task start.

task.on('start', function () {
  console.log('Now, task will be executed.')
})

completed

Event when task run completed.

task.on('completed', function(res) {
  console.log('Result:', res)
})

error

Event when an error raised while execute task.

task.on('error', function(error) {
  console.log('An error has been occurred.', error)
})

Debug

To enable log of process, set

TaskHub.debug = true

Develop Path

  • Set limit for hub.
  • Limit quantity of tasks can be run at same time.
  • Stats for hub.

Package Sidebar

Install

npm i task-hub

Weekly Downloads

1

Version

1.0.4

License

ISC

Unpacked Size

6.23 kB

Total Files

4

Last publish

Collaborators

  • bepolite