Tutorial: 4-Worker

4-Worker

A Worker allows to manage multiple tasks. This Worker covers only basic features and it is the base for the CloudWorker and the QueueWorker.

For example, having a simple task that answer to my messages:

//answer.js
process.on('message', function(d) {
  process.send(d)
})

My worker will handle a couple of tasks:

//worker.js
var Worker = require('relieve/workers/Worker')
var Task = require('relieve/tasks/ForkTask')

var task1 = new Task(fork('answer.js'))
task1.name = 'task1'
var task2 = new Task(fork('answer.js'))
task2.name = 'task2'

worker.add(task1).add(task2)

//send a message to every task
worker.send('message', 'hello world')
//Promise resolves when every message has reach the task
.then(function() {
  worker.kill()
})

The worker also allows to retreive tasks:

//by name
worker.task('task1').send('message', 'hello world')

//or through a Map
for(let t of worker.tasks.values()) {
  t.send('message', 'hello world')
}

See more advanced use cases with the QueueWorker or the CloudWorker.