Commit 4e099262 authored by Tristan Cavelier's avatar Tristan Cavelier

Redesigning job rules in order to add some rules at any time.

parent 979fca4a
......@@ -1115,135 +1115,21 @@ var jobRules = (function(spec, my) {
var that = {};
// Attributes //
var priv = {};
priv.compare = {};
priv.action = {};
that.eliminate = function() { return 'eliminate'; };
that.update = function() { return 'update'; };
that.dontAccept = function() { return 'dont accept'; };
that.wait = function() { return 'wait'; };
that.none = function() { return 'none'; };
priv.compare = {
};
priv.default_compare = function(job1,job2) {
that.default_action = that.none;
that.default_compare = function(job1,job2) {
return (job1.getCommand().getPath() === job2.getCommand().getPath() &&
JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized()));
};
priv.action = {
/*
LEGEND:
- s: storage
- m: method
- n: name
- c: content
- o: options
- =: are equal
- !: are not equal
select ALL s= n=
removefailordone fail|done
/ elim repl nacc wait
Remove !ongoing Save 1 x x x
Save !ongoing Remove 1 x x x
GetList !ongoing GetList 0 1 x x
Remove !ongoing Remove 0 1 x x
Load !ongoing Load 0 1 x x
Save c= !ongoing Save 0 1 x x
Save c! !ongoing Save 0 1 x x
GetList ongoing GetList 0 0 1 x
Remove ongoing Remove 0 0 1 x
Remove ongoing Load 0 0 1 x
Remove !ongoing Load 0 0 1 x
Load ongoing Load 0 0 1 x
Save c= ongoing Save 0 0 1 x
Remove ongoing Save 0 0 0 1
Load ongoing Remove 0 0 0 1
Load ongoing Save 0 0 0 1
Load !ongoing Remove 0 0 0 1
Load !ongoing Save 0 0 0 1
Save ongoing Remove 0 0 0 1
Save ongoing Load 0 0 0 1
Save c! ongoing Save 0 0 0 1
Save !ongoing Load 0 0 0 1
GetList ongoing Remove 0 0 0 0
GetList ongoing Load 0 0 0 0
GetList ongoing Save 0 0 0 0
GetList !ongoing Remove 0 0 0 0
GetList !ongoing Load 0 0 0 0
GetList !ongoing Save 0 0 0 0
Remove ongoing GetList 0 0 0 0
Remove !ongoing GetList 0 0 0 0
Load ongoing GetList 0 0 0 0
Load !ongoing GetList 0 0 0 0
Save ongoing GetList 0 0 0 0
Save !ongoing GetList 0 0 0 0
For more information, see documentation
*/
'saveDocument':{
'on going':{
'saveDocument' :function(job1,job2){
if (job1.getCommand().getContent() ===
job2.getCommand().getContent()) {
return that.dontAccept();
} else {
return that.wait();
}
},
'loadDocument' : that.wait,
'removeDocument' : that.wait,
'getDocumentList' : that.none
},
'not on going':{
'saveDocument' : that.update,
'loadDocument' : that.wait,
'removeDocument' : that.eliminate,
'getDocumentList' : that.none
}
},
'loadDocument':{
'on going':{
'saveDocument' : that.wait,
'loadDocument' : that.dontAccept,
'removeDocument' : that.wait,
'getDocumentList' : that.none
},
'not on going':{
'saveDocument' : that.wait,
'loadDocument' : that.update,
'removeDocument' : that.wait,
'getDocumentList' : that.none
}
},
'removeDocument':{
'on going':{
'saveDocument' : that.wait,
'loadDocument' : that.dontAccept,
'removeDocument' : that.dontAccept,
'getDocumentList' : that.none
},
'not on going':{
'saveDocument' : that.eliminate,
'loadDocument' : that.dontAccept,
'removeDocument' : that.update,
'getDocumentList' : that.none
}
},
'getDocumentList':{
'on going':{
'saveDocument' : that.none,
'loadDocument' : that.none,
'removeDocument' : that.none,
'getDocumentList' : that.dontAccept
},
'not on going':{
'saveDocument' : that.none,
'loadDocument' : that.none,
'removeDocument' : that.none,
'getDocumentList' : that.update
}
}
};
priv.default_action = that.none;
// Methods //
priv.getAction = function(job1,job2) {
var j1label, j2label, j1status;
......@@ -1256,7 +1142,7 @@ var jobRules = (function(spec, my) {
priv.action[j1label][j1status][j2label]) {
return priv.action[j1label][j1status][j2label](job1,job2);
} else {
return priv.default_action(job1,job2);
return that.default_action(job1,job2);
}
};
priv.canCompare = function(job1,job2) {
......@@ -1266,16 +1152,132 @@ var jobRules = (function(spec, my) {
priv.compare[job2label]) {
return priv.compare[job1label][job2label](job1,job2);
} else {
return priv.default_compare(job1,job2);
return that.default_compare(job1,job2);
}
};
/**
* Returns an action string to show what to do if we want to add a job.
* @method validateJobAccordingToJob
* @param job1 {object} The current job.
* @param job2 {object} The new job.
* @return {string} The action string.
*/
that.validateJobAccordingToJob = function(job1,job2) {
if (priv.canCompare(job1,job2)) {
return {action:priv.getAction(job1,job2),job:job1};
}
return {action:priv.default_action(job1,job2),job:job1};
return {action:that.default_action(job1,job2),job:job1};
};
/**
* Adds a rule the action rules.
* @method addActionRule
* @param method1 {string} The action label from the current job.
* @param ongoing {boolean} Is this action is on going or not?
* @param method2 {string} The action label from the new job.
* @param rule {function} The rule that return an action string.
*/
that.addActionRule = function(method1,ongoing,method2,rule) {
var ongoing_s = (ongoing?'on going':'not on going');
priv.action[method1] = priv.action[method1] || {};
priv.action[method1][ongoing_s] = priv.action[method1][ongoing_s] || {};
priv.action[method1][ongoing_s][method2] = rule;
};
/**
* Adds a rule the compare rules.
* @method addCompareRule
* @param method1 {string} The action label from the current job.
* @param method2 {string} The action label from the new job.
* @param rule {function} The rule that return a boolean
* - true if job1 and job2 can be compared, else false.
*/
that.addCompareRule = function(method1,method2,rule) {
priv.compare[method1] = priv.compare[method1] || {};
priv.compare[method1][method2] = rule;
};
/*
LEGEND:
- s: storage
- m: method
- n: name
- c: content
- o: options
- =: are equal
- !: are not equal
select ALL s= n=
removefailordone fail|done
/ elim repl nacc wait
Remove !ongoing Save 1 x x x
Save !ongoing Remove 1 x x x
GetList !ongoing GetList 0 1 x x
Remove !ongoing Remove 0 1 x x
Load !ongoing Load 0 1 x x
Save c= !ongoing Save 0 1 x x
Save c! !ongoing Save 0 1 x x
GetList ongoing GetList 0 0 1 x
Remove ongoing Remove 0 0 1 x
Remove ongoing Load 0 0 1 x
Remove !ongoing Load 0 0 1 x
Load ongoing Load 0 0 1 x
Save c= ongoing Save 0 0 1 x
Remove ongoing Save 0 0 0 1
Load ongoing Remove 0 0 0 1
Load ongoing Save 0 0 0 1
Load !ongoing Remove 0 0 0 1
Load !ongoing Save 0 0 0 1
Save ongoing Remove 0 0 0 1
Save ongoing Load 0 0 0 1
Save c! ongoing Save 0 0 0 1
Save !ongoing Load 0 0 0 1
GetList ongoing Remove 0 0 0 0
GetList ongoing Load 0 0 0 0
GetList ongoing Save 0 0 0 0
GetList !ongoing Remove 0 0 0 0
GetList !ongoing Load 0 0 0 0
GetList !ongoing Save 0 0 0 0
Remove ongoing GetList 0 0 0 0
Remove !ongoing GetList 0 0 0 0
Load ongoing GetList 0 0 0 0
Load !ongoing GetList 0 0 0 0
Save ongoing GetList 0 0 0 0
Save !ongoing GetList 0 0 0 0
For more information, see documentation
*/
that.addActionRule ('saveDocument',true,'saveDocument',
function(job1,job2){
if (job1.getCommand().getContent() ===
job2.getCommand().getContent()) {
return that.dontAccept();
} else {
return that.wait();
}
});
that.addActionRule('saveDocument' ,true ,'loadDocument' ,that.wait);
that.addActionRule('saveDocument' ,true ,'removeDocument' ,that.wait);
that.addActionRule('saveDocument' ,false,'saveDocument' ,that.update);
that.addActionRule('saveDocument' ,false,'loadDocument' ,that.wait);
that.addActionRule('saveDocument' ,false,'removeDocument' ,that.eliminate);
that.addActionRule('loadDocument' ,true ,'saveDocument' ,that.wait);
that.addActionRule('loadDocument' ,true ,'loadDocument' ,that.dontAccept);
that.addActionRule('loadDocument' ,true ,'removeDocument' ,that.wait);
that.addActionRule('loadDocument' ,false,'saveDocument' ,that.wait);
that.addActionRule('loadDocument' ,false,'loadDocument' ,that.update);
that.addActionRule('loadDocument' ,false,'removeDocument' ,that.wait);
that.addActionRule('removeDocument' ,true ,'loadDocument' ,that.dontAccept);
that.addActionRule('removeDocument' ,true ,'removeDocument' ,that.dontAccept);
that.addActionRule('removeDocument' ,false,'saveDocument' ,that.eliminate);
that.addActionRule('removeDocument' ,false,'loadDocument' ,that.dontAccept);
that.addActionRule('removeDocument' ,false,'removeDocument' ,that.update);
that.addActionRule('getDocumentList',true ,'getDocumentList',that.dontAccept);
that.addActionRule('getDocumentList',false,'getDocumentList',that.update);
return that;
}());
......@@ -1333,6 +1335,15 @@ var jobRules = (function(spec, my) {
return priv.id;
};
/**
* Returns the jio job rules object used by the job manager.
* @method getJobRules
* @return {object} The job rules object
*/
that.getJobRules = function() {
return jobRules;
};
/**
* Checks if the storage description is valid or not.
* @method validateStorageDescription
......
/*! JIO - v0.1.0 - 2012-06-13
* Copyright (c) 2012 Nexedi; Licensed */
var jio=function(){var a=function(a,b){var c={};return a=a||{},b=b||{},c.name="jioException",c.message=a.message||"Unknown Reason.",c.toString=function(){return c.name+": "+c.message},c},b=function(b,c){var d=a(b,c);b=b||{};var e=b.command;return d.name="invalidCommandState",d.toString=function(){return d.name+": "+e.getLabel()+", "+d.message},d},c=function(b,c){var d=a(b,c);b=b||{};var e=b.storage.getType();return d.name="invalidStorage",d.toString=function(){return d.name+": "+'Type "'+e+'", '+d.message},d},d=function(b,c){var d=a(b,c),e=b.type;return d.name="invalidStorageType",d.toString=function(){return d.name+": "+e+", "+d.message},d},e=function(b,c){var d=a(b,c);return d.name="jobNotReadyException",d},f=function(b,c){var d=a(b,c);return d.name="tooMuchTriesJobException",d},g=function(b,c){var d=a(b,c);return d.name="invalidJobException",d},h=function(a,b){var d={};a=a||{},b=b||{};var e={};return e.type=a.type||"",d.getType=function(){return e.type},d.setType=function(a){e.type=a},d.execute=function(a){d.validate(a),a.executeOn(d)},d.isValid=function(){return!0},d.validate=function(a){var b=d.validateState();if(b)throw c({storage:d,message:b});a.validate(d)},d.serialized=function(){return{type:d.getType()}},d.saveDocument=function(a){throw c({storage:d,message:"Unknown storage."})},d.loadDocument=function(a){d.saveDocument()},d.removeDocument=function(a){d.saveDocument()},d.getDocumentList=function(a){d.saveDocument()},d.validateState=function(){return""},d},i=function(a,b){var c=h(a,b);a=a||{},b=b||{};var d={};return d.storage_a=a.storagelist||[],c.beforeExecute=function(a){},c.execute=function(a){var b;c.validate(a),c.beforeExecute(a);for(b=0;b<d.storage_a.length;b++)d.storage_a[b].execute(a);c.afterExecute(a)},c.afterExecute=function(a){a.done()},c.serialized=function(){return{type:d.type,storagelist:d.storagelist}},c},j=function(a,c){var d=function(a,c){var d={};a=a||{},c=c||{};var e={};return e.commandlist={saveDocument:l,loadDocument:i,removeDocument:j,getDocumentList:h},a.label&&e.commandlist[a.label]?(e.label=a.label,delete a.label,e.commandlist[e.label](a,c)):(e.path=a.path||"",e.tried=0,e.option=a.option||{},e.respond=e.option.onResponse||function(){},e.done=e.option.onDone||function(){},e.fail=e.option.onFail||function(){},e.retry=function(){d.setMaxRetry(-1),d.fail({status:0,statusText:"Fail Retry",message:"Impossible to retry."})},e.end=function(){},d.getLabel=function(){return"command"},d.getPath=function(){return e.path},d.getOption=function(a){return e.option[a]},d.validate=function(a){d.validateState()},d.getTried=function(){return e.tried},d.setMaxRetry=function(a){e.option.max_retry=a},d.execute=function(a){d.validate(a),e.tried++,a.execute(d)},d.executeOn=function(a){},d.validateState=function(){if(e.path==="")throw b({command:d,message:"Path is empty"})},d.done=function(a){e.done(a),e.respond({status:n(),value:a}),e.end()},d.fail=function(a){e.option.max_retry===0||e.tried<e.option.max_retry?e.retry():(e.fail(a),e.respond({status:o(),error:a}),e.end())},d.onEndDo=function(a){e.end=a},d.onRetryDo=function(a){e.retry=a},d.serialized=function(){return{label:d.getLabel(),tried:e.tried,max_retry:e.max_retry,path:e.path,option:e.option}},d.canBeRestored=function(){return!0},d)},h=function(a,b){var c=d(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"getDocumentList"},c.executeOn=function(a){a.getDocumentList(c)},c.canBeRestored=function(){return!1},c},i=function(a,b){var c=d(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"loadDocument"},c.executeOn=function(a){a.loadDocument(c)},c.canBeRestored=function(){return!1},c},j=function(a,b){var c=d(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"removeDocument"},c.executeOn=function(a){a.removeDocument(c)},c},l=function(a,c){var e=d(a,c);a=a||{},c=c||{};var f={};f.content=a.content,e.getLabel=function(){return"saveDocument"},e.getContent=function(){return f.content};var g=e.validate;e.validate=function(a){if(typeof f.content!="string")throw b({command:e,message:"No data to save"});g(a)},e.executeOn=function(a){a.saveDocument(e)};var h=e.serialized;return e.serialized=function(){var a=h();return a.content=f.content,a},e},m=function(a,b){var c={};return a=a||{},b=b||{},c.getLabel=function(){return"job status"},c.canStart=function(){},c.canRestart=function(){},c.serialized=function(){return{label:c.getLabel()}},c},n=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},o=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},p=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},q=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},r=function(a,b){var c=m(a,b);a=a||{},b=b||{};var d={};return d.job_id_a=a.job_id_array||[],d.threshold=0,c.getLabel=function(){return"wait"},d.refreshJobIdArray=function(){var a=[],b;for(b=0;b<d.job_id_a.length;b+=1)x.jobIdExists(d.job_id_a[b])&&a.push(d.job_id_a[b]);d.job_id_a=a},c.waitForJob=function(a){var b;for(b=0;b<d.job_id_a.length;b+=1)if(d.job_id_a[b]===a.getId())return;d.job_id_a.push(a.getId())},c.dontWaitForJob=function(a){var b,c=[];for(b=0;b<d.job_id_a.length;b+=1)d.job_id_a[b]!==a.getId()&&c.push(d.job_id_a[b]);d.job_id_a=c},c.waitForTime=function(a){d.threshold=Date.now()+a},c.stopWaitForTime=function(){d.threshold=0},c.canStart=function(){return d.refreshJobIdArray(),d.job_id_a.length===0&&Date.now()>=d.threshold},c.canRestart=function(){return c.canStart()},c.serialized=function(){return{label:c.getLabel(),waitfortime:d.threshold,waitforjob:d.job_id_a}},c},s=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=w.nextId(),d.command=a.command,d.storage=a.storage,d.status=p(),d.date=new Date,function(){if(!d.storage)throw g({job:c,message:"No storage set"});if(!d.command)throw g({job:c,message:"No command set"})}(),c.getCommand=function(){return d.command},c.getStatus=function(){return d.status},c.getId=function(){return d.id},c.getStorage=function(){return d.storage},c.getDate=function(){return d.date},c.isReady=function(){return d.tried===0?d.status.canStart():d.status.canRestart()},c.serialized=function(){return{id:d.id,date:d.date.getTime(),status:d.status.serialized(),command:d.command.serialized(),storage:d.storage.serialized()}},c.waitForJob=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForJob(a)},c.dontWaitFor=function(a){d.status.getLabel()==="wait"&&d.status.dontWaitForJob(a)},c.waitForTime=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForTime(a)},c.stopWaitForTime=function(){d.status.getLabel()==="wait"&&d.status.stopWaitForTime()},c.update=function(a){d.command.setMaxRetry(-1),d.command.fail({status:0,statusText:"Replaced",message:"Job has been replaced by another one."}),d.date=a.getDate(),d.command=a.getCommand(),d.status=a.getStatus()},c.execute=function(){if(d.max_retry!==0&&d.tried>=d.max_retry)throw f({job:c,message:"The job was invoked too much time."});if(!c.isReady())throw e({message:"Can not execute this job."});d.status=q(),d.command.onRetryDo(function(){var a=d.command.getTried();a=a*a*200,a>1e4&&(a=1e4),c.waitForTime(a)}),d.command.onEndDo(function(){x.terminateJob(c)}),d.command.execute(d.storage)},c},t=function(a,b){var c={};a=a||{},b=b||{};var d=[],e=a.name||"";return c.add=function(a){d.push(a)},c.remove=function(a){var b,c=[];for(b=0;b<d.length;b+=1)d[b]!==a&&c.push(d[b]);d=c},c.register=function(){v.register(c)},c.unregister=function(){v.unregister(c)},c.trigger=function(a){var b;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},u=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=a.id||0,d.interval=400,d.interval_id=null,d.touch=function(){LocalOrCookieStorage.setItem("jio/id/"+d.id,Date.now())},c.setId=function(a){d.id=a},c.setIntervalDelay=function(a){d.interval=a},c.getIntervalDelay=function(){return d.interval},c.start=function(){d.interval_id||(d.touch(),d.interval_id=setInterval(function(){d.touch()},d.interval))},c.stop=function(){d.interval_id!==null&&(clearInterval(d.interval_id),d.interval_id=null)},c}(),v=function(a,b){var c={};a=a||{},b=b||{};var d={};return c.register=function(a){d[a]||(d[a]=t())},c.unregister=function(a){d[a]&&delete d[a]},c.at=function(a){return d[a]},c.on=function(a,b){c.register(a),c.at(a).add(b)},c.trigger=function(a,b){c.at(a).trigger(b)},c}(),w=function(a,b){var c={};a=a||{},b=b||{};var d=0;return c.nextId=function(){return d=d+1,d},c}(),x=function(a,b){var c={};a=a||{},b=b||{};var e="jio/job_array",f={};return f.id=a.id,f.interval_id=null,f.interval=200,f.job_a=[],f.getJobArrayName=function(){return e+"/"+f.id},f.getJobArray=function(){return LocalOrCookieStorage.getItem(f.getJobArrayName())||[]},f.copyJobArrayToLocal=function(){var a=[],b;for(b=0;b<f.job_a.length;b+=1)a.push(f.job_a[b].serialized());LocalOrCookieStorage.setItem(f.getJobArrayName(),a)},f.removeJob=function(a){var b,c=[];for(b=0;b<f.job_a.length;b+=1)f.job_a[b]!==a&&c.push(f.job_a[b]);f.job_a=c,f.copyJobArrayToLocal()},c.setId=function(a){f.id=a},c.start=function(){var a;f.interval_id===null&&(f.interval_id=setInterval(function(){f.restoreOldJio();for(a=0;a<f.job_a.length;a+=1)c.execute(f.job_a[a])},f.interval))},c.stop=function(){f.interval_id!==null&&(clearInterval(f.interval_id),f.interval_id=null,f.job_a.length===0&&LocalOrCookieStorage.deleteItem(f.getJobArrayName()))},f.restoreOldJio=function(){var a,b;f.lastrestore=f.lastrestore||0;if(f.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)f.restoreOldJioId(b[a]);f.lastrestore=Date.now()},f.restoreOldJioId=function(a){var b;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,b<Date.now()-1e4&&(f.restoreOldJobFromJioId(a),f.removeOldJioId(a),f.removeJobArrayFromJioId(a))},f.restoreOldJobFromJioId=function(a){var b,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(b=0;b<e.length;b+=1){var f=d(e[b].command);f.canBeRestored()&&c.addJob(s({storage:k.storage(e[b].storage),command:f}))}},f.removeOldJioId=function(a){var b,c,d=[];c=LocalOrCookieStorage.getItem("jio/id_array")||[];for(b=0;b<c.length;b+=1)c[b]!==a&&d.push(c[b]);LocalOrCookieStorage.setItem("jio/id_array",d),LocalOrCookieStorage.deleteItem("jio/id/"+a)},f.removeJobArrayFromJioId=function(a){LocalOrCookieStorage.deleteItem("jio/job_array/"+a)},c.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throw b}}f.copyJobArrayToLocal()},c.jobIdExists=function(a){var b;for(b=0;b<f.job_a.length;b+=1)if(f.job_a[b].getId()===a)return!0;return!1},c.terminateJob=function(a){f.removeJob(a),f.copyJobArrayToLocal()},c.addJob=function(a){var b=c.validateJobAccordingToJobList(f.job_a,a);f.manage(a,b)},c.validateJobAccordingToJobList=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)d.push(y.validateJobAccordingToJob(a[c],b));return d},f.manage=function(a,b){var d;if(f.job_a.length!==b.length)throw new RangeError("Array out of bound");for(d=0;d<b.length;d+=1)if(b[d].action==="dont accept")return;for(d=0;d<b.length;d+=1)switch(b[d].action){case"eliminate":c.eliminate(b[d].job);break;case"update":b[d].job.update(a),f.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[d].job);break;default:}f.job_a.push(a),f.copyJobArrayToLocal()},c.eliminate=function(a){var b,c=[];for(b=0;b<f.job_a.length;b+=1)f.job_a[b].getId()!==a.getId()&&c.push(f.job_a[b]);f.job_a=c,f.copyJobArrayToLocal()},c}(),y=function(a,b){var c={},d={};return c.eliminate=function(){return"eliminate"},c.update=function(){return"update"},c.dontAccept=function(){return"dont accept"},c.wait=function(){return"wait"},c.none=function(){return"none"},d.compare={},d.default_compare=function(a,b){return a.getCommand().getPath()===b.getCommand().getPath()&&JSON.stringify(a.getStorage().serialized())===JSON.stringify(b.getStorage().serialized())},d.action={saveDocument:{"on going":{saveDocument:function(a,b){return a.getCommand().getContent()===b.getCommand().getContent()?c.dontAccept():c.wait()},loadDocument:c.wait,removeDocument:c.wait,getDocumentList:c.none},"not on going":{saveDocument:c.update,loadDocument:c.wait,removeDocument:c.eliminate,getDocumentList:c.none}},loadDocument:{"on going":{saveDocument:c.wait,loadDocument:c.dontAccept,removeDocument:c.wait,getDocumentList:c.none},"not on going":{saveDocument:c.wait,loadDocument:c.update,removeDocument:c.wait,getDocumentList:c.none}},removeDocument:{"on going":{saveDocument:c.wait,loadDocument:c.dontAccept,removeDocument:c.dontAccept,getDocumentList:c.none},"not on going":{saveDocument:c.eliminate,loadDocument:c.dontAccept,removeDocument:c.update,getDocumentList:c.none}},getDocumentList:{"on going":{saveDocument:c.none,loadDocument:c.none,removeDocument:c.none,getDocumentList:c.dontAccept},"not on going":{saveDocument:c.none,loadDocument:c.none,removeDocument:c.none,getDocumentList:c.update}}},d.default_action=c.none,d.getAction=function(a,b){var c,e,f;return c=a.getCommand().getLabel(),e=b.getCommand().getLabel(),f=a.getStatus().getLabel()==="on going"?"on going":"not on going",d.action[c]&&d.action[c][f]&&d.action[c][f][e]?d.action[c][f][e](a,b):d.default_action(a,b)},d.canCompare=function(a,b){var c=a.getCommand().getLabel(),e=b.getCommand().getLabel();return d.compare[c]&&d.compare[e]?d.compare[c][e](a,b):d.default_compare(a,b)},c.validateJobAccordingToJob=function(a,b){return d.canCompare(a,b)?{action:d.getAction(a,b),job:a}:{action:d.default_action(a,b),job:a}},c}(),z={};a=a||{},c=c||{};var A={},B="jio/id_array";return A.id=null,A.storage=k.storage(a),A.init=function(){if(A.id===null){var a,b=LocalOrCookieStorage.getItem(B)||[];A.id=1;for(a=0;a<b.length;a+=1)b[a]>=A.id&&(A.id=b[a]+1);b.push(A.id),LocalOrCookieStorage.setItem(B,b),u.setId(A.id),x.setId(A.id)}},z.start=function(){A.init(),u.start(),x.start()},z.stop=function(){x.stop()},z.close=function(){u.stop(),x.stop(),A.id=null},z.start(),z.getId=function(){return A.id},z.validateStorageDescription=function(a){return k.storage(a.type)(a).isValid()},z.saveDocument=function(a,b,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,x.addJob(s({storage:d?k.storage(d):A.storage,command:l({path:a,content:b,option:c})}))},z.loadDocument=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,b.metadata_only=b.metadata_only!==undefined?b.metadata_only:!1,x.addJob(s({storage:c?k.storage(c):A.storage,command:i({path:a,option:b})}))},z.removeDocument=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,x.addJob(s({storage:c?k.storage(c):A.storage,command:j({path:a,option:b})}))},z.getDocumentList=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,b.metadata_only=b.metadata_only!==undefined?b.metadata_only:!0,x.addJob(s({storage:c?k.storage(c):A.storage,command:h({path:a,option:b})}))},z},k=function(a,b){var c={};a=a||{},b=b||{};var e={base:h,handler:i};return c.storage=function(a,b,c){a=a||{};var f=c||a.type||"base";if(!e[f])throw d({type:f,message:"Storage does not exists."});return e[f](a,b)},c.newJio=function(a){var b=a;return typeof b=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},j(a)},c.addStorageType=function(a,b){b=b||function(){return null};if(e[a])throw d({type:a,message:"Already known."});e[a]=b},c}();return k}();
\ No newline at end of file
var jio=function(){var a=function(a,b){var c={};return a=a||{},b=b||{},c.name="jioException",c.message=a.message||"Unknown Reason.",c.toString=function(){return c.name+": "+c.message},c},b=function(b,c){var d=a(b,c);b=b||{};var e=b.command;return d.name="invalidCommandState",d.toString=function(){return d.name+": "+e.getLabel()+", "+d.message},d},c=function(b,c){var d=a(b,c);b=b||{};var e=b.storage.getType();return d.name="invalidStorage",d.toString=function(){return d.name+": "+'Type "'+e+'", '+d.message},d},d=function(b,c){var d=a(b,c),e=b.type;return d.name="invalidStorageType",d.toString=function(){return d.name+": "+e+", "+d.message},d},e=function(b,c){var d=a(b,c);return d.name="jobNotReadyException",d},f=function(b,c){var d=a(b,c);return d.name="tooMuchTriesJobException",d},g=function(b,c){var d=a(b,c);return d.name="invalidJobException",d},h=function(a,b){var d={};a=a||{},b=b||{};var e={};return e.type=a.type||"",d.getType=function(){return e.type},d.setType=function(a){e.type=a},d.execute=function(a){d.validate(a),a.executeOn(d)},d.isValid=function(){return!0},d.validate=function(a){var b=d.validateState();if(b)throw c({storage:d,message:b});a.validate(d)},d.serialized=function(){return{type:d.getType()}},d.saveDocument=function(a){throw c({storage:d,message:"Unknown storage."})},d.loadDocument=function(a){d.saveDocument()},d.removeDocument=function(a){d.saveDocument()},d.getDocumentList=function(a){d.saveDocument()},d.validateState=function(){return""},d},i=function(a,b){var c=h(a,b);a=a||{},b=b||{};var d={};return d.storage_a=a.storagelist||[],c.beforeExecute=function(a){},c.execute=function(a){var b;c.validate(a),c.beforeExecute(a);for(b=0;b<d.storage_a.length;b++)d.storage_a[b].execute(a);c.afterExecute(a)},c.afterExecute=function(a){a.done()},c.serialized=function(){return{type:d.type,storagelist:d.storagelist}},c},j=function(a,c){var d=function(a,c){var d={};a=a||{},c=c||{};var e={};return e.commandlist={saveDocument:l,loadDocument:i,removeDocument:j,getDocumentList:h},a.label&&e.commandlist[a.label]?(e.label=a.label,delete a.label,e.commandlist[e.label](a,c)):(e.path=a.path||"",e.tried=0,e.option=a.option||{},e.respond=e.option.onResponse||function(){},e.done=e.option.onDone||function(){},e.fail=e.option.onFail||function(){},e.retry=function(){d.setMaxRetry(-1),d.fail({status:0,statusText:"Fail Retry",message:"Impossible to retry."})},e.end=function(){},d.getLabel=function(){return"command"},d.getPath=function(){return e.path},d.getOption=function(a){return e.option[a]},d.validate=function(a){d.validateState()},d.getTried=function(){return e.tried},d.setMaxRetry=function(a){e.option.max_retry=a},d.execute=function(a){d.validate(a),e.tried++,a.execute(d)},d.executeOn=function(a){},d.validateState=function(){if(e.path==="")throw b({command:d,message:"Path is empty"})},d.done=function(a){e.done(a),e.respond({status:n(),value:a}),e.end()},d.fail=function(a){e.option.max_retry===0||e.tried<e.option.max_retry?e.retry():(e.fail(a),e.respond({status:o(),error:a}),e.end())},d.onEndDo=function(a){e.end=a},d.onRetryDo=function(a){e.retry=a},d.serialized=function(){return{label:d.getLabel(),tried:e.tried,max_retry:e.max_retry,path:e.path,option:e.option}},d.canBeRestored=function(){return!0},d)},h=function(a,b){var c=d(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"getDocumentList"},c.executeOn=function(a){a.getDocumentList(c)},c.canBeRestored=function(){return!1},c},i=function(a,b){var c=d(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"loadDocument"},c.executeOn=function(a){a.loadDocument(c)},c.canBeRestored=function(){return!1},c},j=function(a,b){var c=d(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"removeDocument"},c.executeOn=function(a){a.removeDocument(c)},c},l=function(a,c){var e=d(a,c);a=a||{},c=c||{};var f={};f.content=a.content,e.getLabel=function(){return"saveDocument"},e.getContent=function(){return f.content};var g=e.validate;e.validate=function(a){if(typeof f.content!="string")throw b({command:e,message:"No data to save"});g(a)},e.executeOn=function(a){a.saveDocument(e)};var h=e.serialized;return e.serialized=function(){var a=h();return a.content=f.content,a},e},m=function(a,b){var c={};return a=a||{},b=b||{},c.getLabel=function(){return"job status"},c.canStart=function(){},c.canRestart=function(){},c.serialized=function(){return{label:c.getLabel()}},c},n=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},o=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},p=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},q=function(a,b){var c=m(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},r=function(a,b){var c=m(a,b);a=a||{},b=b||{};var d={};return d.job_id_a=a.job_id_array||[],d.threshold=0,c.getLabel=function(){return"wait"},d.refreshJobIdArray=function(){var a=[],b;for(b=0;b<d.job_id_a.length;b+=1)x.jobIdExists(d.job_id_a[b])&&a.push(d.job_id_a[b]);d.job_id_a=a},c.waitForJob=function(a){var b;for(b=0;b<d.job_id_a.length;b+=1)if(d.job_id_a[b]===a.getId())return;d.job_id_a.push(a.getId())},c.dontWaitForJob=function(a){var b,c=[];for(b=0;b<d.job_id_a.length;b+=1)d.job_id_a[b]!==a.getId()&&c.push(d.job_id_a[b]);d.job_id_a=c},c.waitForTime=function(a){d.threshold=Date.now()+a},c.stopWaitForTime=function(){d.threshold=0},c.canStart=function(){return d.refreshJobIdArray(),d.job_id_a.length===0&&Date.now()>=d.threshold},c.canRestart=function(){return c.canStart()},c.serialized=function(){return{label:c.getLabel(),waitfortime:d.threshold,waitforjob:d.job_id_a}},c},s=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=w.nextId(),d.command=a.command,d.storage=a.storage,d.status=p(),d.date=new Date,function(){if(!d.storage)throw g({job:c,message:"No storage set"});if(!d.command)throw g({job:c,message:"No command set"})}(),c.getCommand=function(){return d.command},c.getStatus=function(){return d.status},c.getId=function(){return d.id},c.getStorage=function(){return d.storage},c.getDate=function(){return d.date},c.isReady=function(){return d.tried===0?d.status.canStart():d.status.canRestart()},c.serialized=function(){return{id:d.id,date:d.date.getTime(),status:d.status.serialized(),command:d.command.serialized(),storage:d.storage.serialized()}},c.waitForJob=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForJob(a)},c.dontWaitFor=function(a){d.status.getLabel()==="wait"&&d.status.dontWaitForJob(a)},c.waitForTime=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForTime(a)},c.stopWaitForTime=function(){d.status.getLabel()==="wait"&&d.status.stopWaitForTime()},c.update=function(a){d.command.setMaxRetry(-1),d.command.fail({status:0,statusText:"Replaced",message:"Job has been replaced by another one."}),d.date=a.getDate(),d.command=a.getCommand(),d.status=a.getStatus()},c.execute=function(){if(d.max_retry!==0&&d.tried>=d.max_retry)throw f({job:c,message:"The job was invoked too much time."});if(!c.isReady())throw e({message:"Can not execute this job."});d.status=q(),d.command.onRetryDo(function(){var a=d.command.getTried();a=a*a*200,a>1e4&&(a=1e4),c.waitForTime(a)}),d.command.onEndDo(function(){x.terminateJob(c)}),d.command.execute(d.storage)},c},t=function(a,b){var c={};a=a||{},b=b||{};var d=[],e=a.name||"";return c.add=function(a){d.push(a)},c.remove=function(a){var b,c=[];for(b=0;b<d.length;b+=1)d[b]!==a&&c.push(d[b]);d=c},c.register=function(){v.register(c)},c.unregister=function(){v.unregister(c)},c.trigger=function(a){var b;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},u=function(a,b){var c={};a=a||{},b=b||{};var d={};return d.id=a.id||0,d.interval=400,d.interval_id=null,d.touch=function(){LocalOrCookieStorage.setItem("jio/id/"+d.id,Date.now())},c.setId=function(a){d.id=a},c.setIntervalDelay=function(a){d.interval=a},c.getIntervalDelay=function(){return d.interval},c.start=function(){d.interval_id||(d.touch(),d.interval_id=setInterval(function(){d.touch()},d.interval))},c.stop=function(){d.interval_id!==null&&(clearInterval(d.interval_id),d.interval_id=null)},c}(),v=function(a,b){var c={};a=a||{},b=b||{};var d={};return c.register=function(a){d[a]||(d[a]=t())},c.unregister=function(a){d[a]&&delete d[a]},c.at=function(a){return d[a]},c.on=function(a,b){c.register(a),c.at(a).add(b)},c.trigger=function(a,b){c.at(a).trigger(b)},c}(),w=function(a,b){var c={};a=a||{},b=b||{};var d=0;return c.nextId=function(){return d=d+1,d},c}(),x=function(a,b){var c={};a=a||{},b=b||{};var e="jio/job_array",f={};return f.id=a.id,f.interval_id=null,f.interval=200,f.job_a=[],f.getJobArrayName=function(){return e+"/"+f.id},f.getJobArray=function(){return LocalOrCookieStorage.getItem(f.getJobArrayName())||[]},f.copyJobArrayToLocal=function(){var a=[],b;for(b=0;b<f.job_a.length;b+=1)a.push(f.job_a[b].serialized());LocalOrCookieStorage.setItem(f.getJobArrayName(),a)},f.removeJob=function(a){var b,c=[];for(b=0;b<f.job_a.length;b+=1)f.job_a[b]!==a&&c.push(f.job_a[b]);f.job_a=c,f.copyJobArrayToLocal()},c.setId=function(a){f.id=a},c.start=function(){var a;f.interval_id===null&&(f.interval_id=setInterval(function(){f.restoreOldJio();for(a=0;a<f.job_a.length;a+=1)c.execute(f.job_a[a])},f.interval))},c.stop=function(){f.interval_id!==null&&(clearInterval(f.interval_id),f.interval_id=null,f.job_a.length===0&&LocalOrCookieStorage.deleteItem(f.getJobArrayName()))},f.restoreOldJio=function(){var a,b;f.lastrestore=f.lastrestore||0;if(f.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)f.restoreOldJioId(b[a]);f.lastrestore=Date.now()},f.restoreOldJioId=function(a){var b;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,b<Date.now()-1e4&&(f.restoreOldJobFromJioId(a),f.removeOldJioId(a),f.removeJobArrayFromJioId(a))},f.restoreOldJobFromJioId=function(a){var b,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(b=0;b<e.length;b+=1){var f=d(e[b].command);f.canBeRestored()&&c.addJob(s({storage:k.storage(e[b].storage),command:f}))}},f.removeOldJioId=function(a){var b,c,d=[];c=LocalOrCookieStorage.getItem("jio/id_array")||[];for(b=0;b<c.length;b+=1)c[b]!==a&&d.push(c[b]);LocalOrCookieStorage.setItem("jio/id_array",d),LocalOrCookieStorage.deleteItem("jio/id/"+a)},f.removeJobArrayFromJioId=function(a){LocalOrCookieStorage.deleteItem("jio/job_array/"+a)},c.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throw b}}f.copyJobArrayToLocal()},c.jobIdExists=function(a){var b;for(b=0;b<f.job_a.length;b+=1)if(f.job_a[b].getId()===a)return!0;return!1},c.terminateJob=function(a){f.removeJob(a),f.copyJobArrayToLocal()},c.addJob=function(a){var b=c.validateJobAccordingToJobList(f.job_a,a);f.manage(a,b)},c.validateJobAccordingToJobList=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)d.push(y.validateJobAccordingToJob(a[c],b));return d},f.manage=function(a,b){var d;if(f.job_a.length!==b.length)throw new RangeError("Array out of bound");for(d=0;d<b.length;d+=1)if(b[d].action==="dont accept")return;for(d=0;d<b.length;d+=1)switch(b[d].action){case"eliminate":c.eliminate(b[d].job);break;case"update":b[d].job.update(a),f.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[d].job);break;default:}f.job_a.push(a),f.copyJobArrayToLocal()},c.eliminate=function(a){var b,c=[];for(b=0;b<f.job_a.length;b+=1)f.job_a[b].getId()!==a.getId()&&c.push(f.job_a[b]);f.job_a=c,f.copyJobArrayToLocal()},c}(),y=function(a,b){var c={},d={};return d.compare={},d.action={},c.eliminate=function(){return"eliminate"},c.update=function(){return"update"},c.dontAccept=function(){return"dont accept"},c.wait=function(){return"wait"},c.none=function(){return"none"},c.default_action=c.none,c.default_compare=function(a,b){return a.getCommand().getPath()===b.getCommand().getPath()&&JSON.stringify(a.getStorage().serialized())===JSON.stringify(b.getStorage().serialized())},d.getAction=function(a,b){var e,f,g;return e=a.getCommand().getLabel(),f=b.getCommand().getLabel(),g=a.getStatus().getLabel()==="on going"?"on going":"not on going",d.action[e]&&d.action[e][g]&&d.action[e][g][f]?d.action[e][g][f](a,b):c.default_action(a,b)},d.canCompare=function(a,b){var e=a.getCommand().getLabel(),f=b.getCommand().getLabel();return d.compare[e]&&d.compare[f]?d.compare[e][f](a,b):c.default_compare(a,b)},c.validateJobAccordingToJob=function(a,b){return d.canCompare(a,b)?{action:d.getAction(a,b),job:a}:{action:c.default_action(a,b),job:a}},c.addActionRule=function(a,b,c,e){var f=b?"on going":"not on going";d.action[a]=d.action[a]||{},d.action[a][f]=d.action[a][f]||{},d.action[a][f][c]=e},c.addCompareRule=function(a,b,c){d.compare[a]=d.compare[a]||{},d.compare[a][b]=c},c.addActionRule("saveDocument",!0,"saveDocument",function(a,b){return a.getCommand().getContent()===b.getCommand().getContent()?c.dontAccept():c.wait()}),c.addActionRule("saveDocument",!0,"loadDocument",c.wait),c.addActionRule("saveDocument",!0,"removeDocument",c.wait),c.addActionRule("saveDocument",!1,"saveDocument",c.update),c.addActionRule("saveDocument",!1,"loadDocument",c.wait),c.addActionRule("saveDocument",!1,"removeDocument",c.eliminate),c.addActionRule("loadDocument",!0,"saveDocument",c.wait),c.addActionRule("loadDocument",!0,"loadDocument",c.dontAccept),c.addActionRule("loadDocument",!0,"removeDocument",c.wait),c.addActionRule("loadDocument",!1,"saveDocument",c.wait),c.addActionRule("loadDocument",!1,"loadDocument",c.update),c.addActionRule("loadDocument",!1,"removeDocument",c.wait),c.addActionRule("removeDocument",!0,"loadDocument",c.dontAccept),c.addActionRule("removeDocument",!0,"removeDocument",c.dontAccept),c.addActionRule("removeDocument",!1,"saveDocument",c.eliminate),c.addActionRule("removeDocument",!1,"loadDocument",c.dontAccept),c.addActionRule("removeDocument",!1,"removeDocument",c.update),c.addActionRule("getDocumentList",!0,"getDocumentList",c.dontAccept),c.addActionRule("getDocumentList",!1,"getDocumentList",c.update),c}(),z={};a=a||{},c=c||{};var A={},B="jio/id_array";return A.id=null,A.storage=k.storage(a),A.init=function(){if(A.id===null){var a,b=LocalOrCookieStorage.getItem(B)||[];A.id=1;for(a=0;a<b.length;a+=1)b[a]>=A.id&&(A.id=b[a]+1);b.push(A.id),LocalOrCookieStorage.setItem(B,b),u.setId(A.id),x.setId(A.id)}},z.start=function(){A.init(),u.start(),x.start()},z.stop=function(){x.stop()},z.close=function(){u.stop(),x.stop(),A.id=null},z.start(),z.getId=function(){return A.id},z.getJobRules=function(){return y},z.validateStorageDescription=function(a){return k.storage(a.type)(a).isValid()},z.saveDocument=function(a,b,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,x.addJob(s({storage:d?k.storage(d):A.storage,command:l({path:a,content:b,option:c})}))},z.loadDocument=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,b.metadata_only=b.metadata_only!==undefined?b.metadata_only:!1,x.addJob(s({storage:c?k.storage(c):A.storage,command:i({path:a,option:b})}))},z.removeDocument=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,x.addJob(s({storage:c?k.storage(c):A.storage,command:j({path:a,option:b})}))},z.getDocumentList=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,b.metadata_only=b.metadata_only!==undefined?b.metadata_only:!0,x.addJob(s({storage:c?k.storage(c):A.storage,command:h({path:a,option:b})}))},z},k=function(a,b){var c={};a=a||{},b=b||{};var e={base:h,handler:i};return c.storage=function(a,b,c){a=a||{};var f=c||a.type||"base";if(!e[f])throw d({type:f,message:"Storage does not exists."});return e[f](a,b)},c.newJio=function(a){var b=a;return typeof b=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},j(a)},c.addStorageType=function(a,b){b=b||function(){return null};if(e[a])throw d({type:a,message:"Already known."});e[a]=b},c}();return k}();
\ No newline at end of file
......@@ -52,6 +52,15 @@
return priv.id;
};
/**
* Returns the jio job rules object used by the job manager.
* @method getJobRules
* @return {object} The job rules object
*/
that.getJobRules = function() {
return jobRules;
};
/**
* Checks if the storage description is valid or not.
* @method validateStorageDescription
......
......@@ -2,135 +2,21 @@ var jobRules = (function(spec, my) {
var that = {};
// Attributes //
var priv = {};
priv.compare = {};
priv.action = {};
that.eliminate = function() { return 'eliminate'; };
that.update = function() { return 'update'; };
that.dontAccept = function() { return 'dont accept'; };
that.wait = function() { return 'wait'; };
that.none = function() { return 'none'; };
priv.compare = {
};
priv.default_compare = function(job1,job2) {
that.default_action = that.none;
that.default_compare = function(job1,job2) {
return (job1.getCommand().getPath() === job2.getCommand().getPath() &&
JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized()));
};
priv.action = {
/*
LEGEND:
- s: storage
- m: method
- n: name
- c: content
- o: options
- =: are equal
- !: are not equal
select ALL s= n=
removefailordone fail|done
/ elim repl nacc wait
Remove !ongoing Save 1 x x x
Save !ongoing Remove 1 x x x
GetList !ongoing GetList 0 1 x x
Remove !ongoing Remove 0 1 x x
Load !ongoing Load 0 1 x x
Save c= !ongoing Save 0 1 x x
Save c! !ongoing Save 0 1 x x
GetList ongoing GetList 0 0 1 x
Remove ongoing Remove 0 0 1 x
Remove ongoing Load 0 0 1 x
Remove !ongoing Load 0 0 1 x
Load ongoing Load 0 0 1 x
Save c= ongoing Save 0 0 1 x
Remove ongoing Save 0 0 0 1
Load ongoing Remove 0 0 0 1
Load ongoing Save 0 0 0 1
Load !ongoing Remove 0 0 0 1
Load !ongoing Save 0 0 0 1
Save ongoing Remove 0 0 0 1
Save ongoing Load 0 0 0 1
Save c! ongoing Save 0 0 0 1
Save !ongoing Load 0 0 0 1
GetList ongoing Remove 0 0 0 0
GetList ongoing Load 0 0 0 0
GetList ongoing Save 0 0 0 0
GetList !ongoing Remove 0 0 0 0
GetList !ongoing Load 0 0 0 0
GetList !ongoing Save 0 0 0 0
Remove ongoing GetList 0 0 0 0
Remove !ongoing GetList 0 0 0 0
Load ongoing GetList 0 0 0 0
Load !ongoing GetList 0 0 0 0
Save ongoing GetList 0 0 0 0
Save !ongoing GetList 0 0 0 0
For more information, see documentation
*/
'saveDocument':{
'on going':{
'saveDocument' :function(job1,job2){
if (job1.getCommand().getContent() ===
job2.getCommand().getContent()) {
return that.dontAccept();
} else {
return that.wait();
}
},
'loadDocument' : that.wait,
'removeDocument' : that.wait,
'getDocumentList' : that.none
},
'not on going':{
'saveDocument' : that.update,
'loadDocument' : that.wait,
'removeDocument' : that.eliminate,
'getDocumentList' : that.none
}
},
'loadDocument':{
'on going':{
'saveDocument' : that.wait,
'loadDocument' : that.dontAccept,
'removeDocument' : that.wait,
'getDocumentList' : that.none
},
'not on going':{
'saveDocument' : that.wait,
'loadDocument' : that.update,
'removeDocument' : that.wait,
'getDocumentList' : that.none
}
},
'removeDocument':{
'on going':{
'saveDocument' : that.wait,
'loadDocument' : that.dontAccept,
'removeDocument' : that.dontAccept,
'getDocumentList' : that.none
},
'not on going':{
'saveDocument' : that.eliminate,
'loadDocument' : that.dontAccept,
'removeDocument' : that.update,
'getDocumentList' : that.none
}
},
'getDocumentList':{
'on going':{
'saveDocument' : that.none,
'loadDocument' : that.none,
'removeDocument' : that.none,
'getDocumentList' : that.dontAccept
},
'not on going':{
'saveDocument' : that.none,
'loadDocument' : that.none,
'removeDocument' : that.none,
'getDocumentList' : that.update
}
}
};
priv.default_action = that.none;
// Methods //
priv.getAction = function(job1,job2) {
var j1label, j2label, j1status;
......@@ -143,7 +29,7 @@ var jobRules = (function(spec, my) {
priv.action[j1label][j1status][j2label]) {
return priv.action[j1label][j1status][j2label](job1,job2);
} else {
return priv.default_action(job1,job2);
return that.default_action(job1,job2);
}
};
priv.canCompare = function(job1,job2) {
......@@ -153,15 +39,131 @@ var jobRules = (function(spec, my) {
priv.compare[job2label]) {
return priv.compare[job1label][job2label](job1,job2);
} else {
return priv.default_compare(job1,job2);
return that.default_compare(job1,job2);
}
};
/**
* Returns an action string to show what to do if we want to add a job.
* @method validateJobAccordingToJob
* @param job1 {object} The current job.
* @param job2 {object} The new job.
* @return {string} The action string.
*/
that.validateJobAccordingToJob = function(job1,job2) {
if (priv.canCompare(job1,job2)) {
return {action:priv.getAction(job1,job2),job:job1};
}
return {action:priv.default_action(job1,job2),job:job1};
return {action:that.default_action(job1,job2),job:job1};
};
/**
* Adds a rule the action rules.
* @method addActionRule
* @param method1 {string} The action label from the current job.
* @param ongoing {boolean} Is this action is on going or not?
* @param method2 {string} The action label from the new job.
* @param rule {function} The rule that return an action string.
*/
that.addActionRule = function(method1,ongoing,method2,rule) {
var ongoing_s = (ongoing?'on going':'not on going');
priv.action[method1] = priv.action[method1] || {};
priv.action[method1][ongoing_s] = priv.action[method1][ongoing_s] || {};
priv.action[method1][ongoing_s][method2] = rule;
};
/**
* Adds a rule the compare rules.
* @method addCompareRule
* @param method1 {string} The action label from the current job.
* @param method2 {string} The action label from the new job.
* @param rule {function} The rule that return a boolean
* - true if job1 and job2 can be compared, else false.
*/
that.addCompareRule = function(method1,method2,rule) {
priv.compare[method1] = priv.compare[method1] || {};
priv.compare[method1][method2] = rule;
};
/*
LEGEND:
- s: storage
- m: method
- n: name
- c: content
- o: options
- =: are equal
- !: are not equal
select ALL s= n=
removefailordone fail|done
/ elim repl nacc wait
Remove !ongoing Save 1 x x x
Save !ongoing Remove 1 x x x
GetList !ongoing GetList 0 1 x x
Remove !ongoing Remove 0 1 x x
Load !ongoing Load 0 1 x x
Save c= !ongoing Save 0 1 x x
Save c! !ongoing Save 0 1 x x
GetList ongoing GetList 0 0 1 x
Remove ongoing Remove 0 0 1 x
Remove ongoing Load 0 0 1 x
Remove !ongoing Load 0 0 1 x
Load ongoing Load 0 0 1 x
Save c= ongoing Save 0 0 1 x
Remove ongoing Save 0 0 0 1
Load ongoing Remove 0 0 0 1
Load ongoing Save 0 0 0 1
Load !ongoing Remove 0 0 0 1
Load !ongoing Save 0 0 0 1
Save ongoing Remove 0 0 0 1
Save ongoing Load 0 0 0 1
Save c! ongoing Save 0 0 0 1
Save !ongoing Load 0 0 0 1
GetList ongoing Remove 0 0 0 0
GetList ongoing Load 0 0 0 0
GetList ongoing Save 0 0 0 0
GetList !ongoing Remove 0 0 0 0
GetList !ongoing Load 0 0 0 0
GetList !ongoing Save 0 0 0 0
Remove ongoing GetList 0 0 0 0
Remove !ongoing GetList 0 0 0 0
Load ongoing GetList 0 0 0 0
Load !ongoing GetList 0 0 0 0
Save ongoing GetList 0 0 0 0
Save !ongoing GetList 0 0 0 0
For more information, see documentation
*/
that.addActionRule ('saveDocument',true,'saveDocument',
function(job1,job2){
if (job1.getCommand().getContent() ===
job2.getCommand().getContent()) {
return that.dontAccept();
} else {
return that.wait();
}
});
that.addActionRule('saveDocument' ,true ,'loadDocument' ,that.wait);
that.addActionRule('saveDocument' ,true ,'removeDocument' ,that.wait);
that.addActionRule('saveDocument' ,false,'saveDocument' ,that.update);
that.addActionRule('saveDocument' ,false,'loadDocument' ,that.wait);
that.addActionRule('saveDocument' ,false,'removeDocument' ,that.eliminate);
that.addActionRule('loadDocument' ,true ,'saveDocument' ,that.wait);
that.addActionRule('loadDocument' ,true ,'loadDocument' ,that.dontAccept);
that.addActionRule('loadDocument' ,true ,'removeDocument' ,that.wait);
that.addActionRule('loadDocument' ,false,'saveDocument' ,that.wait);
that.addActionRule('loadDocument' ,false,'loadDocument' ,that.update);
that.addActionRule('loadDocument' ,false,'removeDocument' ,that.wait);
that.addActionRule('removeDocument' ,true ,'loadDocument' ,that.dontAccept);
that.addActionRule('removeDocument' ,true ,'removeDocument' ,that.dontAccept);
that.addActionRule('removeDocument' ,false,'saveDocument' ,that.eliminate);
that.addActionRule('removeDocument' ,false,'loadDocument' ,that.dontAccept);
that.addActionRule('removeDocument' ,false,'removeDocument' ,that.update);
that.addActionRule('getDocumentList',true ,'getDocumentList',that.dontAccept);
that.addActionRule('getDocumentList',false,'getDocumentList',that.update);
return that;
}());
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment