A simple framework for producing and distributing tasks
Uses redis to distribute tasks among any number of workers. Workers have the ability to communicate results back to the initiator.
Create an NPM package.
see the examples directory
create your tasks file (testTasks.js)
var Tasker = require'./tasker.js'Tasker;var tasker = ;taskerregisterTask'add'/* The first arg to done is any error* the second is the result of the task*/done'' datafirst + datasecond;;moduleexportstestTasks = tasker;
create your worker file (workerTest.js), running this file will create a worker process that polls the local redis server for task requests
#!/usr/bin/env nodevar Worker = require'./tasker.js'TaskWorker;var testTasks = require'./testTasks.js'testTasks;var worker = tasks: testTasks;
open a shell, nav to folder, do: (leave it going) node workerTest.js
produce some work for the worker (testProducer.js)
#!/usr/bin/env nodevar testTasks = require'./testTasks.js'testTasks;console.logres;/* Because we pass a callback, the callback will be* called with the result of our workers' work.* the delayed.add means that the task is pushed* to redis for distribution to workers*/testTaskstasksadddelayedfirst:1second:2 logIt;testTaskstasksadddelayedfirst:2second:3 logIt;/* of course we can run the task locally too */testTaskstasksaddnowfirst:1 second:2 logIt;/* if we pass no callback, then the result is discarded */testTaskstasksadddelayedfirst:7second:13;
open another shell, nav to examples folder: node testProducer.js
you should see some combination of 3, 5, and 12 being printed