jobster

Run simple tasks simultanously.

Consider using Promises.
Last updated 3 years ago by axelrindle .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install jobster 
SYNC missed versions from official npm registry.

jobster

Run simple tasks simultanously.

Install

npm install --save jobster

Usage

// require modules
const Queue = require('jobster')
const Task = Queue.Task
const chalk = require('chalk')

// define a Queue
let queue = new Queue()
queue.on('done', function () { // called when all tasks in the queue have finished
  console.log(chalk.magenta.bold('All Tasks in ' + queue.name + ' done for now!'))
})

// define a task
let task = new Task("My Task")
task.work(function (progress) {
  this.data.interval = setInterval(() => {
    progress(10)
  }, 400)
}).on('progress', (progress) => {
  console.log(chalk.green(task.name + " progressed: ") + chalk.green.bold(progress + "%"))
}).on('done', () => {
  clearInterval(task.data.interval)
  console.log(chalk.magenta(task.name + " finished!"))
})

// enqueue the task
queue.enqueue(t1)

API

Class: Queue

A Queue processes one or more tasks simultanously.

Queue is an EventEmitter.

new Queue([name = "Queue", concurrency = number_of_cpus])

Creates a new Queue.

  • name A name for your Queue. Defaults to "Queue".
  • concurrency How many tasks can run simultanously.

Queue.enqueue(task)

Enqueues a Task for execution.

  • task The Task to enqueue.

Class: Task

A Task is responsible for executing an ongoing action, like downloading a file.

Task is an EventEmitter.

new Task(name = "A Task", data = {})

Creates a new Task.

  • name Defaults to "A Task". Note: You should choose a proper name, so you can identify it later when doing console output.
  • data Default to an empty object. Used to store task data.

Task.work(worker)

Defines the worker function for this task.

  • worker A function with one parameter for the progress update function.

An example worker function:

myTask.work((update) => {
	downloadImage((progress) => {
    	update(progress)
    })
})

Calling the update function emits the progress event.

Events

Event listeners can be added to both Queue and Task objects by calling the on(eventName, listener) function inherited from EventEmitter.

Class: Queue

Event: 'done'

Emitted when all current Tasks have finished.

Class: Task

Event: 'start'

Emitted when the Task has started working.

Event: 'progress'

Returns:

  • progress Progress value (usually between 0 and 100)

Emitted when the Task has updated it's progress.

Event: 'done'

Emitted when the Task has finished working.

ToDo

  • Add support for Promises.

License

See LICENSE

Current Tags

  • 1.0.0                                ...           latest (3 years ago)

1 Versions

  • 1.0.0 [deprecated]           ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 2
Dependencies (2)
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |