ini
functionini()
Initiiert Subscription auf load_mp channel
var ini = function(cb){
mem.subscribe("load_mp", function(err){
if(!err){
log.info(ok
, "build subscribed to load_mp channel");
if(_.isFunction(cb)){
cb();
}
}
})
}
exports.ini = ini;
mem.on('message', function(ch, val){
if(ch == "load_mp"){
var i
, doc = val.Mp
, dc = doc.Container
, Ndc = dc ? dc.length : 0;
if(doc){
var docid = val._id
mem.publish("builddown", [docid], function(err){
if(!err){
mem.remove([docid], function(err){
if(!err){
log.info(ok
, "clean up: " + docid);
build_base([docid], val, function(path){
if(dc){
for(i = 0; i < Ndc; i++){
((function(j){
var cont = dc[j]
, path_c = path.concat([j])
, cb = function(res){
if(res.ok){
if(j == Ndc - 1){
mem.publish("buildup", [docid], function(err){
if(!err){
log.info(ok
, "mp builded, event published, exec callback");
}else{
log.error({error:err}
, "error on publishing build event")
}
});
} // last
}else{
log.error(res
, "error on attempt to build")
}
}
return function(){
if(_.isObject(cont)){
if(cont.Take && cont.From){
var con = net.container(cont)
, s = JSON.stringify({take:cont.Take, from:cont.From})
log.info(ok
, "found container reference, try to get from db");
// db request
request.exec(con, false, s, function(c){
if(_.isObject(c) && c.Definition){
// replace
if(cont.Replace && _.isObject(cont.Replace)){
log.info(ok
, "found replace key on container level"
+ "try to substitude");
c = utils.replace(c, cont);
} // if replace
log.info(ok
, "received container, try to build");
build_container(path_c, c, cb);
}
})
}else{
build_container(path_c, cont, cb);
}
} // is Object
}
})(i))();
} //for
}else{
log.warn({warn:"no container"}
, "mp has no container definition");
}
}); // build base
}else{
log.error({error:err}
, "error on attempt to clean up: " + docid);
}
}); // remove
}else{
log.error({error:err}
, "error on attempt to publish builddown: " + docid);
}
}); // publish builddown
}else{
log.error({error:"no doc"}
, "given value is not a mp-document");
}
} // if build
}); // on build