jobster v1.0.0
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
TheTask
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 Task
s 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
7 years ago