Skip to content

Commit

Permalink
pushing first set of changes to handle workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
truepattern committed Jan 1, 2012
1 parent 2025232 commit 1913a27
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ lib-cov
*.pid
*.swp
*.swo
*~
node_modules/
testing

1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.git*
examples/
test/
node_modules/
.DS_Store
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# cloudd
node.js cloud engine

## NOT USABLE YET


## prereq
* install node, redis

32 changes: 32 additions & 0 deletions examples/helloworld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var cloudd = require('../index');

// hello 'task' definition
var hello = {
task: function(job, done) {
job.log('hello task log :' + job.title);
console.log('hello');
done();
}
};

// world 'task' definition
var world = {
task: function(job, done) {
job.log('world task log :' + job.title);
console.log('world');
done();
},
done:function() {
console.log('world -- task successfuly completed : #' + this.id);
}
};

// workflow definition
var flow = [
{id:'hello', task: hello, parent:'root'},
{id:'world', task: world, parent:'root'}
];

cloudd.submit('test-one',flow);
cloudd.startapp(3000);

93 changes: 93 additions & 0 deletions lib/cloudd.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,96 @@
/**
* Cloudd - copyright(c) 2011 truepattern.
*/
var _ = require('underscore');
var kue = require('kue');

var jobs = kue.createQueue();
var pkgname = '[cloudd]';

// -------- todo -------------
// for now no graph is built, later we'll check for DAG
// preconditions to trigger next job etc.,
// http://www.graphdracula.net/, node-graph

/**
* function to submit a job.
* name : name of the job
* flow : [ tasks ]
* task : { id:task-id, task:task object, parent:parent-task-id }
* task object: functions for 'task' - required, 'done', 'failed'
*/
exports.submit = function (name, flow) {
var root = _.filter(flow, function(t) { return t.parent=='root'; });
var q = _.clone(root);
console.log(pkgname + " job added: '" + name + "'");
while(q.length > 0) {
var curr = q.shift();
//console.log('firing job:' + curr.id);
doJob(name,curr);
var nextLevel = _.filter(flow, function(t) { return t.parent==curr.id; });
q=q.concat(nextLevel);
}
};

/**
* express app exposed out of kue
*/
exports.startapp = function(port) {
kue.app.listen(port);
console.log(pkgname + " app started on " + port);
};


//jobs.types(function(err, types) { console.log(types); });
// states 'complete', 'failed', 'inactive', 'active', 'delayed'
// jobs.state(function(err, state { console.log(state); });

// ---------- internal functions

/**
* plug in the create/done/failed/progress/task methods
*/
function doJob(name,jconfig) {
//console.log(' loading task ... ' + jconfig.task);
var taskModule = jconfig.task;
if(taskModule instanceof String) {
var m = './' + jconfig.task ;
taskModule = require(m);
}
var fns=_.functions(taskModule);
//console.log(' ... ' +fns);
var taskFn = _.include(fns, 'task');
if(taskFn) {
var createFn = _.include(fns, 'create');
var doneFn = _.include(fns, 'done');
var failedFn = _.include(fns, 'failed');
var progressFn = _.include(fns, 'progress');

if(createFn) {
//console.log(' ... calling create ... ');
} else {
// create the job
var taskid = name+':'+jconfig.id;
var job = jobs.create(jconfig.id, {title:taskid, config:jconfig});
job.on('complete', doneFn?taskModule.done:taskComplete).
on('failed', failedFn?taskModule.failed:taskFailed).
on('progress', progressFn?taskModule.progress:taskProgress);
job.save();
console.log(pkgname + " new task : '" + job.data.title + "'");
}
jobs.process(jconfig.id, 1, taskModule.task);
}
}

// ------ default handlers for job

function taskComplete() {
console.log(pkgname + " task done #"+this.id + '[' + this.data.title + ']');
}
function taskFailed() {
console.log(pkgname + " task **FAILED** #"+this.id + '[' + this.data.title + ']');
}
function taskProgress(job, progress) {
console.log(pkgname + " task progress #"+this.id + '[' + this.data.title + ']' + " progress :" + progress);
}

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "cloudd",
"description": "node cloud engine",
"homepage": "http://truepattern.com/cloudd",
"version": "0.0.1",
"homepage": "https://github.com/truepattern/cloudd",
"version": "0.0.2",
"author": "truepattern <admin@truepattern.com>",
"dependencies": [],
"dependencies": {
"underscore": ">= 1.2.3",
"kue" : ">= 0.3.3"
},
"devDependencies": [],
"keywords": ["cloudd", "job", "scheduler", "workflow", "queue", "task"],
"repository": {"type":"git", "url": "git://github.com/truepattern/cloudd"},
Expand Down

0 comments on commit 1913a27

Please sign in to comment.