Skip to content

Commit

Permalink
poc
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Aug 13, 2014
1 parent 402231f commit 7ad302f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
'use strict';

var events = require('events');
var util = require('util');

var bach = require('bach');
var createTimer = require('async-time');

var normalizeArgs = require('./lib/normalizeArgs');
var validateRegistry = require('./lib/validateRegistry');

var DefaultRegistry = require('./registry/Default');

function Orchestrator(registry){
events.EventEmitter.call(this);
this.registry = registry || new DefaultRegistry();
validateRegistry(this.registry);
this.asyncTime = createTimer(this);
}
util.inherits(Orchestrator, events.EventEmitter);

Orchestrator.prototype.task = function(taskName, fn){
var registry = this.registry;
var self = this;

if(typeof taskName === 'function'){
fn = taskName;
Expand All @@ -25,7 +33,9 @@ Orchestrator.prototype.task = function(taskName, fn){
}

if(fn){
return registry.set(taskName, fn);
return registry.set(taskName, function(cb){
self.asyncTime(fn, cb);
});
}

return registry.get(taskName);
Expand All @@ -41,12 +51,12 @@ Orchestrator.prototype.setRegistry = function(newRegistry){
};

Orchestrator.prototype.series = function(){
var args = normalizeArgs(this.registry, arguments);
var args = normalizeArgs(this, arguments);
return bach.series(args);
};

Orchestrator.prototype.parallel = function(){
var args = normalizeArgs(this.registry, arguments);
var args = normalizeArgs(this, arguments);
return bach.parallel(args);
};

Expand Down
8 changes: 5 additions & 3 deletions lib/normalizeArgs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

function normalizeArgs(registry, args){
function normalizeArgs(orchestrator, args){
return Array.prototype.slice.call(args).map(function(taskOrFn) {
return typeof taskOrFn === 'function' ? taskOrFn : registry.get(taskOrFn);
}, registry);
return typeof taskOrFn === 'function' ? function(cb) {
orchestrator.asyncTime(taskOrFn, cb);
} : orchestrator.registry.get(taskOrFn);
}, orchestrator.registry);
}

module.exports = normalizeArgs;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"dependencies": {
"bach": "0.0.1",
"async-done": "git://github.com/phated/async-done#master"
"async-done": "git://github.com/phated/async-done#master",
"async-time": "0.0.1"
},
"devDependencies": {
"lab": "^3.1.1",
Expand Down
48 changes: 48 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var Orchestrator = require('./index.js');

var orchestrator = new Orchestrator();

orchestrator.on('start', function (evt) {
console.log('start: ', JSON.stringify(evt, null, 2));
});

orchestrator.on('stop', function (evt) {
console.log('stop: ', JSON.stringify(evt, null, 2));
});

orchestrator.task('foo', function foo(done) {
setTimeout(function(){
console.log('finishing foo');
done(null, 'foo');
}, 1500);
});

orchestrator.task('bar', function bar(done) {
setTimeout(function(){
console.log('finishing bar');
done(null, 'bar');
}, 2000);
});

function baz(done) {
setTimeout(function(){
console.log('finishing baz');
done(null, 'baz');
}, 4000);
}


orchestrator.task('default', orchestrator.parallel('foo', 'bar', baz));


orchestrator.task('default')(function(err, result){
console.log(err, result);
});


//orchestrator.series('foo', 'bar')

/*
orchestrator.task('bar')(function(err, result){
console.log(err, result);
})*/;

0 comments on commit 7ad302f

Please sign in to comment.