Skip to content

Instantly share code, notes, and snippets.

@apiv
Created September 5, 2015 20:51
Show Gist options
  • Save apiv/aa6190b7e009b5f4075a to your computer and use it in GitHub Desktop.
Save apiv/aa6190b7e009b5f4075a to your computer and use it in GitHub Desktop.
Experiment with running concurrent processes with async.js, and writing dynamic log information to the terminal
var async = require('async');
require('colors');
var CONCURRENCY = 5;
var S_BUILDING = ' Building '.bold;
var S_BUILT = ' Built '.green.bold;
function buildIt() {
console.log("Starting process with concurrency of ", CONCURRENCY);
// stream for building
// runs a build for a single bundleCfg object
var build$ = async.queue(function (bundleCfg, done) {
var builder = new jspm.Builder();
bundleCfg.status = S_BUILDING;
log$.push(bundleCfg);
setTimeout(function () {
bundleCfg.status = S_BUILT;
log$.push(bundleCfg);
done(null, bundleCfg);
}, Math.floor(Math.random()*10000));
}, CONCURRENCY);
// stream for log writing to console
// there can only be one of these, since it writes escape strings to move
// up to the line it needs to print on, and then prints that line
var logLine = 1;
var log$ = async.queue(function (bundleCfg, done) {
if (!bundleCfg._logLine) {
bundleCfg._logLine = logLine++;
process.stdout.write('\n');
}
// go up from current line to the line this bundle is on
for (var i = logLine; i!=bundleCfg._logLine;i--) {
process.stdout.write("\x1b[A\r");
}
process.stdout.write(bundleCfg.status + ' ' + bundleCfg.name);
// step back down to logline from the bundle's logline
for (;i<logLine;i++) {
process.stdout.write("\n");
}
done();
}, 1);
[
{name: 'aaa'},
{name: 'bbb'},
{name: 'ccc'},
{name: 'ddd'},
{name: 'eee'},
{name: 'fff'},
{name: 'ggg'},
{name: 'hhh'},
{name: 'iii'},
{name: 'jjj'},
{name: 'kkk'},
{name: 'lll'},
{name: 'mmm'},
{name: 'nnn'},
{name: 'ooo'},
{name: 'ppp'},
{name: 'qqq'},
{name: 'rrr'},
{name: 'sss'},
{name: 'ttt'},
{name: 'uuu'}
].forEach(function (bundleCfg) {
build$.push(bundleCfg, function (err, result) {
if (err) { throw err; }
});
});
}
gulp.task('do', function () {
buildIt();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment