Last active
December 14, 2015 01:39
-
-
Save xat/5008385 to your computer and use it in GitHub Desktop.
A simple demonstrantion of a sheduler done in Node.JS. Tasks can be split up in multiple Queues. Tasks can be marked as "exclusive" if they should not be run concurrent with other tasks.
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
var http = require('http'), | |
async = require('async'), | |
queueBuilder, | |
taskBuilder, | |
counter, | |
maxConcurrent = Number.POSITIVE_INFINITY, | |
debug = true, | |
queues = {}, | |
tasks = {}; | |
// Some Helper functions, used for debugging / logging: | |
counter = (function() { | |
var current = 0; | |
return function() { | |
return ++current; | |
} | |
})(); | |
debugTaskBuilder = function(task, debugBefore, debugAfter) { | |
return function(cb) { | |
console.log(debugBefore); | |
task.call(this, function() { | |
console.log(debugAfter); | |
cb(); | |
}); | |
}; | |
}; | |
// Tasks which get executed by workers | |
// within the Queue | |
tasks.fakeTimeout = function(cb) { | |
setTimeout(function() { | |
cb(); | |
}, Math.floor((Math.random()*10000)+1)); | |
}; | |
tasks.httpRequest = function(cb) { | |
http.get("http://www.spiegel.de", function(res) { | |
cb(); | |
}); | |
}; | |
tasks.concurrencyOne = function(cb) { | |
this.concurrency = 1; | |
cb(); | |
}; | |
tasks.concurrencyMax = function(cb) { | |
this.concurrency = maxConcurrent; | |
cb(); | |
}; | |
// Queue factory | |
queueBuilder = function() { | |
var queue = async.queue(function(task, cb) { | |
task.call(queue, function() { | |
cb(); | |
}); | |
}, maxConcurrent); | |
return queue; | |
}; | |
// Build Queues | |
queues.cook = queueBuilder(); | |
queues.wash = queueBuilder(); | |
// Adding Tasks: | |
// Normal: http://127.0.0.1:9999/?queue=wash | |
// Exclusive: http://127.0.0.1:9999/?queue=wash&exclusive=1 | |
// Specific Task: http://127.0.0.1:9999/?queue=wash&task=httpRequest | |
http.createServer(function(req, res) { | |
var query = require('url').parse(req.url,true).query; | |
if (!(query.queue && queues[query.queue])) { | |
res.end('0'); | |
return; | |
} | |
var id = counter(), | |
queue = queues[query.queue], | |
task = (query.task && tasks[query.task])?query.task:'fakeTimeout'; | |
if (query.exclusive == 1) { | |
// Set Concurreny to 1 before running | |
// an exclusive task | |
queue.push(tasks.concurrencyOne); | |
// Add Exclusive Task | |
if (debug) { | |
queue.push(debugTaskBuilder(tasks[task], | |
'Start exclusive Task #'+id+' in Queue ' + query.queue, | |
'End exclusive Task #'+id | |
)); | |
} else { | |
queue.push(tasks[task]); | |
} | |
// Set Concurrency back to maxConcurrent | |
queue.push(tasks.concurrencyMax); | |
} else { | |
// Add Normal Task | |
if (debug) { | |
queue.push(debugTaskBuilder(tasks[task], | |
'Start Task #'+id+' in Queue ' + query.queue, | |
'End Task #'+id | |
)); | |
} else { | |
queue.push(tasks[task]); | |
} | |
} | |
res.end('1'); | |
}).listen(9999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AB Ergebnis. 1 Millionen Requests / 100 Parallel: