Created
November 28, 2013 23:55
-
-
Save fspot/7699673 to your computer and use it in GitHub Desktop.
Simple way of making a worker manager with node.js, so you can limit concurrent tasks (cpu bound).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EventEmitter = require('events').EventEmitter | |
print = console.log | |
class MyManager extends EventEmitter | |
constructor: (@max_w) -> | |
@nb_w = 0 | |
@queue = [] | |
@on 'died_worker', (arg) -> | |
@on_died_worker(arg) | |
on_died_worker: (arg) -> | |
print 'Looking for stuff to start in queue...' | |
if @canHazMore() and @queue.length > 0 | |
[func, arg] = @queue.shift() | |
print '...Wee ! ' + arg + ' found in queue !' | |
@wannaStart(func, arg) | |
else | |
print '...ahem, nope, nothing is waiting !' | |
start_worker: (arg) -> | |
@nb_w++ | |
@emit 'new_worker', arg | |
stop_worker: (arg) -> | |
@nb_w-- | |
@emit 'died_worker', arg | |
canHazMore: () -> | |
@nb_w < @max_w | |
wannaStart: (func, arg) -> | |
if @canHazMore() | |
print '? Okay, I can start task to process: ' + arg.toString() | |
@start_worker(arg) | |
func arg, => | |
@stop_worker(arg) | |
else | |
print '? Nope, can\'t start right now, will wait to process: ' + arg | |
@queue.push [func, arg] | |
task = (arg, callback) -> | |
print '>>> Doing: ' + arg.text + ' ...' | |
continuation = (a, callback) -> | |
print '>>> ... Done: ' + a.text | |
callback() | |
setTimeout continuation, 5000, arg, callback | |
# set maximum number of concurrent tasks to 1 | |
manager = new MyManager 1 | |
manager.on 'new_worker', (arg) -> | |
print '**new worker** (' + arg + ')' | |
manager.on 'died_worker', (arg) -> | |
print '**worker died** (' + arg + ')' | |
process.openStdin().addListener 'data', (d) -> | |
d = d.toString().substring(0, d.length-1) | |
arg = text: d, num: 42 | |
manager.wannaStart(task, arg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment