Commit 184405dc authored by Tristan Cavelier's avatar Tristan Cavelier

Deleting some 'conosle.log'.

parent 6f1d98d0
/*! JIO - v0.1.0 - 2012-06-18
/*! JIO - v0.1.0 - 2012-06-20
* Copyright (c) 2012 Nexedi; Licensed */
var jio = (function () {
......@@ -883,7 +883,7 @@ var job = function(spec, my) {
log ('job update(job): ' + JSON.stringify (job.serialized()));
priv.command.setMaxRetry(-1);
priv.command.onEndDo(function (status) {
console.log ('job update on end' + status.getLabel());
log ('job update on end' + status.getLabel());
});
priv.command.fail({status:0,statusText:'Replaced',
message:'Job has been replaced by another one.'});
......@@ -1404,7 +1404,7 @@ var jobRules = (function(spec, my) {
if (job1.getCommand().getPath() === job2.getCommand().getPath() &&
JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized())) {
console.log ('same ! ' + job1.getCommand().getPath() + ', ' +
log ('same ! ' + job1.getCommand().getPath() + ', ' +
job2.getCommand().getPath() + ', ' +
JSON.stringify (job1.getStorage().serialized())+', '+
JSON.stringify (job2.getStorage().serialized()));
......
/*! JIO - v0.1.0 - 2012-06-18
/*! JIO - v0.1.0 - 2012-06-20
* Copyright (c) 2012 Nexedi; Licensed */
var jio=function(){var a=function(){},b=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},c=function(a,c){var d=b(a,c);a=a||{};var e=a.command;return d.name="invalidCommandState",d.toString=function(){return d.name+": "+e.getLabel()+", "+d.message},d},d=function(a,c){var d=b(a,c);a=a||{};var e=a.storage.getType();return d.name="invalidStorage",d.toString=function(){return d.name+": "+'Type "'+e+'", '+d.message},d},e=function(a,c){var d=b(a,c),e=a.type;return d.name="invalidStorageType",d.toString=function(){return d.name+": "+e+", "+d.message},d},f=function(a,c){var d=b(a,c);return d.name="jobNotReadyException",d},g=function(a,c){var d=b(a,c);return d.name="tooMuchTriesJobException",d},h=function(a,c){var d=b(a,c);return d.name="invalidJobException",d},i=function(b,c){var e={};b=b||{},c=c||{};var f={};return f.type=b.type||"",a("new storage spec: "+JSON.stringify(b)),e.getType=function(){return f.type},e.setType=function(a){f.type=a},e.execute=function(b){a("storage "+e.getType()+" execute(command): "+JSON.stringify(b.serialized())),e.validate(b),e.done=b.done,e.fail=b.fail,e.end=b.end,b.executeOn(e)},e.isValid=function(){return!0},e.validate=function(a){var b=e.validateState();if(b)throw d({storage:e,message:b});a.validate(e)},e.serialized=function(){return{type:e.getType()}},e.saveDocument=function(a){throw d({storage:e,message:"Unknown storage."})},e.loadDocument=function(a){e.saveDocument()},e.removeDocument=function(a){e.saveDocument()},e.getDocumentList=function(a){e.saveDocument()},e.validateState=function(){return""},e.done=function(){},e.fail=function(){},e.end=function(){},e},j=function(b,c){b=b||{},c=c||{};var d=i(b,c);return a("new storageHandler spec: "+JSON.stringify(b)),d.newCommand=function(a,b){var d=b||{};return d.label=a,k(d,c)},d.newStorage=function(a){var b=a||{};return y.storage(b,c)},d.addJob=function(b,e){a("storageHandler "+d.getType()+" addJob (storage, command): "+JSON.stringify(b.serialized())+", "+JSON.stringify(e.serialized())),c.jobManager.addJob(v({storage:b,command:e},c))},d},k=function(b,d){var e={};b=b||{},d=d||{};var f={};return f.commandlist={saveDocument:o,loadDocument:m,removeDocument:n,getDocumentList:l},b.label&&f.commandlist[b.label]?(f.label=b.label,delete b.label,f.commandlist[f.label](b,d)):(f.path=b.path||"",f.tried=0,f.option=b.option||{},f.respond=f.option.onResponse||function(){},f.done=f.option.onDone||function(){},f.fail=f.option.onFail||function(){},f.retry=function(){e.setMaxRetry(-1),e.fail({status:0,statusText:"Fail Retry",message:"Impossible to retry."})},f.end=function(){},e.getLabel=function(){return"command"},e.getPath=function(){return f.path},e.getOption=function(a){return f.option[a]},e.validate=function(a){e.validateState()},e.getTried=function(){return f.tried},e.setMaxRetry=function(a){f.option.max_retry=a},e.execute=function(a){e.validate(a),f.tried++,a.execute(e)},e.executeOn=function(a){},e.validateState=function(){if(f.path==="")throw c({command:e,message:"Path is empty"})},e.done=function(b){a("command done: "+JSON.stringify(b)),f.respond({status:q(),value:b}),f.done(b),f.end(q())},e.fail=function(b){a("command fail: "+JSON.stringify(b)),f.option.max_retry===0||f.tried<f.option.max_retry?f.retry():(f.respond({status:r(),error:b}),f.fail(b),f.end(r()))},e.end=function(){f.end(q())},e.onResponseDo=function(a){if(a)f.respond=a;else return f.respond},e.onDoneDo=function(a){if(a)f.done=a;else return f.done},e.onFailDo=function(a){if(a)f.fail=a;else return f.fail},e.onEndDo=function(a){f.end=a},e.onRetryDo=function(a){f.retry=a},e.serialized=function(){return{label:e.getLabel(),tried:f.tried,max_retry:f.max_retry,path:f.path,option:e.cloneOption()}},e.canBeRestored=function(){return!0},e.clone=function(){return k(e.serialized(),d)},e.cloneOption=function(){var a,b={};for(a in f.option)b[a]=f.option[a];return b},e)},l=function(a,b){var c=k(a,b);a=a||{},b=b||{},c.getLabel=function(){return"getDocumentList"},c.executeOn=function(a){a.getDocumentList(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){var b;if(a)for(b=0;b<a.length;b+=1)typeof a[b].last_modified!="number"&&(a[b].last_modified=(new Date(a[b].last_modified)).getTime()),typeof a[b].creation_date!="number"&&(a[b].creation_date=(new Date(a[b].creation_date)).getTime());d(a)},c},m=function(a,b){var c=k(a,b);a=a||{},b=b||{},c.getLabel=function(){return"loadDocument"},c.executeOn=function(a){a.loadDocument(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){a&&(typeof a.last_modified!="number"&&(a.last_modified=(new Date(a.last_modified)).getTime()),typeof a.creation_date!="number"&&(a.creation_date=(new Date(a.creation_date)).getTime())),d(a)},c},n=function(a,b){var c=k(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"removeDocument"},c.executeOn=function(a){a.removeDocument(c)},c},o=function(a,b){var d=k(a,b);a=a||{},b=b||{};var e={};e.content=a.content,d.getLabel=function(){return"saveDocument"},d.getContent=function(){return e.content};var f=d.validate;d.validate=function(a){if(typeof e.content!="string")throw c({command:d,message:"No data to save"});f(a)},d.executeOn=function(a){a.saveDocument(d)};var g=d.serialized;return d.serialized=function(){var a=g();return a.content=e.content,a},d},p=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.isWaitStatus=function(){return!1},c.isDone=function(){return!1},c},q=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c.isDone=function(){return!0},c},r=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},s=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},t=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},u=function(a,b){var c=p(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=[],c;for(c=0;c<d.job_id_a.length;c+=1)b.jobManager.jobIdExists(d.job_id_a[c])&&a.push(d.job_id_a[c]);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.isWaitStatus=function(){return!0},c},v=function(b,c){var d={};b=b||{},c=c||{};var e={};e.id=c.jobIdHandler.nextId(),e.command=b.command,e.storage=b.storage,e.status=s(),e.date=new Date,a("new job spec: "+JSON.stringify(b)+", priv: "+JSON.stringify(e));if(!e.storage)throw h({job:d,message:"No storage set"});if(!e.command)throw h({job:d,message:"No command set"});return d.getCommand=function(){return e.command},d.getStatus=function(){return e.status},d.getId=function(){return e.id},d.getStorage=function(){return e.storage},d.getDate=function(){return e.date},d.isReady=function(){return e.tried===0?e.status.canStart():e.status.canRestart()},d.serialized=function(){return{id:e.id,date:e.date.getTime(),status:e.status.serialized(),command:e.command.serialized(),storage:e.storage.serialized()}},d.waitForJob=function(b){a("job waitForJob(job): "+JSON.stringify(b.serialized())),e.status.getLabel()!=="wait"&&(e.status=u({},c)),e.status.waitForJob(b)},d.dontWaitFor=function(a){e.status.getLabel()==="wait"&&e.status.dontWaitForJob(a)},d.waitForTime=function(b){a("job waitForTime(ms): "+b),e.status.getLabel()!=="wait"&&(e.status=u({},c)),e.status.waitForTime(b)},d.stopWaitForTime=function(){e.status.getLabel()==="wait"&&e.status.stopWaitForTime()},d.eliminated=function(){e.command.setMaxRetry(-1),a("job eliminated(): "+JSON.stringify(d.serialized())),e.command.fail({status:0,statusText:"Stoped",message:"This job has been stoped by another one."})},d.notAccepted=function(){a("job notAccepted(): "+JSON.stringify(d.serialized())),e.command.setMaxRetry(-1),e.command.onEndDo(function(){e.status=r(),c.jobManager.terminateJob(d)}),e.command.fail({status:0,statusText:"Not Accepted",message:"This job is already running."})},d.update=function(b){a("job update(job): "+JSON.stringify(b.serialized())),e.command.setMaxRetry(-1),e.command.onEndDo(function(a){console.log("job update on end"+a.getLabel())}),e.command.fail({status:0,statusText:"Replaced",message:"Job has been replaced by another one."}),e.date=b.getDate(),e.command=b.getCommand(),e.status=b.getStatus()},d.execute=function(){a("job execute(): "+JSON.stringify(d.serialized()));if(e.max_retry!==0&&e.tried>=e.max_retry)throw g({job:d,message:"The job was invoked too much time."});if(!d.isReady())throw f({message:"Can not execute this job."});e.status=t(),e.command.onRetryDo(function(){a("command.retry job:"+JSON.stringify(d.serialized()));var b=e.command.getTried();b=b*b*200,b>1e4&&(b=1e4),d.waitForTime(b)}),e.command.onEndDo(function(b){e.status=b,a("command.end job:"+JSON.stringify(d.serialized())),c.jobManager.terminateJob(d)}),e.command.execute(e.storage)},d},w=function(a,b){var c={};a=a||{},b=b||{};var d=[],e=a.name||"",f=a.announcer||{};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(){f.register(c)},c.unregister=function(){f.unregister(c)},c.trigger=function(a){var b;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},x=function(a,b){var c=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}(),d=function(a,b){var c={};a=a||{},b=b||{};var d={};return c.register=function(a){d[a]||(d[a]=w())},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}(),e=function(a,b){var c={};a=a||{},b=b||{};var d=0;return c.nextId=function(){return d=d+1,d},c}(),f=function(a,b){var c={};a=a||{},b=b||{};var d="jio/job_array",f={};return f.id=a.id,f.interval_id=null,f.interval=200,f.job_array=[],b.jobManager=c,b.jobIdHandler=e,f.getJobArrayName=function(){return d+"/"+f.id},f.getJobArray=function(){return LocalOrCookieStorage.getItem(f.getJobArrayName())||[]},f.copyJobArrayToLocal=function(){var a=[],b;for(b=0;b<f.job_array.length;b+=1)a.push(f.job_array[b].serialized());LocalOrCookieStorage.setItem(f.getJobArrayName(),a)},f.removeJob=function(a){var b,c=[];for(b=0;b<f.job_array.length;b+=1)f.job_array[b]!==a&&c.push(f.job_array[b]);f.job_array=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_array.length;a+=1)c.execute(f.job_array[a])},f.interval))},c.stop=function(){f.interval_id!==null&&(clearInterval(f.interval_id),f.interval_id=null,f.job_array.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 d,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(d=0;d<e.length;d+=1){var f=k(e[d].command,b);f.canBeRestored()&&c.addJob(v({storage:y.storage(e[d].storage,b),command:f},b))}},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_array.length;b+=1)if(f.job_array[b].getId()===a)return!0;return!1},c.terminateJob=function(a){f.removeJob(a)},c.addJob=function(a){var b=c.validateJobAccordingToJobList(f.job_array,a);f.appendJob(a,b)},c.validateJobAccordingToJobList=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)d.push(g.validateJobAccordingToJob(a[c],b));return d},f.appendJob=function(a,b){var c;if(f.job_array.length!==b.length)throw new RangeError("Array out of bound");for(c=0;c<b.length;c+=1)if(b[c].action==="dont accept")return a.notAccepted();for(c=0;c<b.length;c+=1)switch(b[c].action){case"eliminate":b[c].job.eliminated(),f.removeJob(b[c].job);break;case"update":b[c].job.update(a),f.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[c].job);break;default:}f.job_array.push(a),f.copyJobArrayToLocal()},c.serialized=function(){var a=[],b,c=f.job_array||[];for(b=0;b<c.length;b+=1)a.push(c[b].serialized());return a},c}(),g=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())&&console.log("same ! "+a.getCommand().getPath()+", "+b.getCommand().getPath()+", "+JSON.stringify(a.getStorage().serialized())+", "+JSON.stringify(b.getStorage().serialized())),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}(),h={};a=a||{},b=b||{};var i={},j="jio/id_array";return i.id=null,b.jobManager=f,b.jobIdHandler=e,i.storage=y.storage(a,b),i.init=function(){if(i.id===null){var a,b=LocalOrCookieStorage.getItem(j)||[];i.id=1;for(a=0;a<b.length;a+=1)b[a]>=i.id&&(i.id=b[a]+1);b.push(i.id),LocalOrCookieStorage.setItem(j,b),c.setId(i.id),f.setId(i.id)}},h.start=function(){i.init(),c.start(),f.start()},h.stop=function(){f.stop()},h.close=function(){c.stop(),f.stop(),i.id=null},h.start(),h.getId=function(){return i.id},h.getJobRules=function(){return g},h.validateStorageDescription=function(a){return y.storage(a,b).isValid()},h.getJobArray=function(){return f.serialized()},h.saveDocument=function(a,c,d,e){d=d||{},d.onResponse=d.onResponse||function(){},d.onDone=d.onDone||function(){},d.onFail=d.onFail||function(){},d.max_retry=d.max_retry||0,f.addJob(v({storage:e?y.storage(e,b):i.storage,command:o({path:a,content:c,option:d},b)},b))},h.loadDocument=function(a,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,c.metadata_only=c.metadata_only!==undefined?c.metadata_only:!1,f.addJob(v({storage:d?y.storage(d,b):i.storage,command:m({path:a,option:c},b)},b))},h.removeDocument=function(a,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,f.addJob(v({storage:d?y.storage(d,b):i.storage,command:n({path:a,option:c},b)},b))},h.getDocumentList=function(a,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,c.metadata_only=c.metadata_only!==undefined?c.metadata_only:!0,f.addJob(v({storage:d?y.storage(d,b):i.storage,command:l({path:a,option:c},b)},b))},h},y=function(a,b){var c={};a=a||{},b=b||{};var d={base:i,handler:j};return c.storage=function(a,b,c){a=a||{},b=b||{};var f=c||a.type||"base";if(!d[f])throw e({type:f,message:"Storage does not exists."});return d[f](a,b)},c.newJio=function(a){var b=a;return typeof b=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},x(a)},c.addStorageType=function(a,b){b=b||function(){return null};if(d[a])throw e({type:a,message:"Already known."});d[a]=b},c}();return y}();
\ No newline at end of file
var jio=function(){var a=function(){},b=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},c=function(a,c){var d=b(a,c);a=a||{};var e=a.command;return d.name="invalidCommandState",d.toString=function(){return d.name+": "+e.getLabel()+", "+d.message},d},d=function(a,c){var d=b(a,c);a=a||{};var e=a.storage.getType();return d.name="invalidStorage",d.toString=function(){return d.name+": "+'Type "'+e+'", '+d.message},d},e=function(a,c){var d=b(a,c),e=a.type;return d.name="invalidStorageType",d.toString=function(){return d.name+": "+e+", "+d.message},d},f=function(a,c){var d=b(a,c);return d.name="jobNotReadyException",d},g=function(a,c){var d=b(a,c);return d.name="tooMuchTriesJobException",d},h=function(a,c){var d=b(a,c);return d.name="invalidJobException",d},i=function(b,c){var e={};b=b||{},c=c||{};var f={};return f.type=b.type||"",a("new storage spec: "+JSON.stringify(b)),e.getType=function(){return f.type},e.setType=function(a){f.type=a},e.execute=function(b){a("storage "+e.getType()+" execute(command): "+JSON.stringify(b.serialized())),e.validate(b),e.done=b.done,e.fail=b.fail,e.end=b.end,b.executeOn(e)},e.isValid=function(){return!0},e.validate=function(a){var b=e.validateState();if(b)throw d({storage:e,message:b});a.validate(e)},e.serialized=function(){return{type:e.getType()}},e.saveDocument=function(a){throw d({storage:e,message:"Unknown storage."})},e.loadDocument=function(a){e.saveDocument()},e.removeDocument=function(a){e.saveDocument()},e.getDocumentList=function(a){e.saveDocument()},e.validateState=function(){return""},e.done=function(){},e.fail=function(){},e.end=function(){},e},j=function(b,c){b=b||{},c=c||{};var d=i(b,c);return a("new storageHandler spec: "+JSON.stringify(b)),d.newCommand=function(a,b){var d=b||{};return d.label=a,k(d,c)},d.newStorage=function(a){var b=a||{};return y.storage(b,c)},d.addJob=function(b,e){a("storageHandler "+d.getType()+" addJob (storage, command): "+JSON.stringify(b.serialized())+", "+JSON.stringify(e.serialized())),c.jobManager.addJob(v({storage:b,command:e},c))},d},k=function(b,d){var e={};b=b||{},d=d||{};var f={};return f.commandlist={saveDocument:o,loadDocument:m,removeDocument:n,getDocumentList:l},b.label&&f.commandlist[b.label]?(f.label=b.label,delete b.label,f.commandlist[f.label](b,d)):(f.path=b.path||"",f.tried=0,f.option=b.option||{},f.respond=f.option.onResponse||function(){},f.done=f.option.onDone||function(){},f.fail=f.option.onFail||function(){},f.retry=function(){e.setMaxRetry(-1),e.fail({status:0,statusText:"Fail Retry",message:"Impossible to retry."})},f.end=function(){},e.getLabel=function(){return"command"},e.getPath=function(){return f.path},e.getOption=function(a){return f.option[a]},e.validate=function(a){e.validateState()},e.getTried=function(){return f.tried},e.setMaxRetry=function(a){f.option.max_retry=a},e.execute=function(a){e.validate(a),f.tried++,a.execute(e)},e.executeOn=function(a){},e.validateState=function(){if(f.path==="")throw c({command:e,message:"Path is empty"})},e.done=function(b){a("command done: "+JSON.stringify(b)),f.respond({status:q(),value:b}),f.done(b),f.end(q())},e.fail=function(b){a("command fail: "+JSON.stringify(b)),f.option.max_retry===0||f.tried<f.option.max_retry?f.retry():(f.respond({status:r(),error:b}),f.fail(b),f.end(r()))},e.end=function(){f.end(q())},e.onResponseDo=function(a){if(a)f.respond=a;else return f.respond},e.onDoneDo=function(a){if(a)f.done=a;else return f.done},e.onFailDo=function(a){if(a)f.fail=a;else return f.fail},e.onEndDo=function(a){f.end=a},e.onRetryDo=function(a){f.retry=a},e.serialized=function(){return{label:e.getLabel(),tried:f.tried,max_retry:f.max_retry,path:f.path,option:e.cloneOption()}},e.canBeRestored=function(){return!0},e.clone=function(){return k(e.serialized(),d)},e.cloneOption=function(){var a,b={};for(a in f.option)b[a]=f.option[a];return b},e)},l=function(a,b){var c=k(a,b);a=a||{},b=b||{},c.getLabel=function(){return"getDocumentList"},c.executeOn=function(a){a.getDocumentList(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){var b;if(a)for(b=0;b<a.length;b+=1)typeof a[b].last_modified!="number"&&(a[b].last_modified=(new Date(a[b].last_modified)).getTime()),typeof a[b].creation_date!="number"&&(a[b].creation_date=(new Date(a[b].creation_date)).getTime());d(a)},c},m=function(a,b){var c=k(a,b);a=a||{},b=b||{},c.getLabel=function(){return"loadDocument"},c.executeOn=function(a){a.loadDocument(c)},c.canBeRestored=function(){return!1};var d=c.done;return c.done=function(a){a&&(typeof a.last_modified!="number"&&(a.last_modified=(new Date(a.last_modified)).getTime()),typeof a.creation_date!="number"&&(a.creation_date=(new Date(a.creation_date)).getTime())),d(a)},c},n=function(a,b){var c=k(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"removeDocument"},c.executeOn=function(a){a.removeDocument(c)},c},o=function(a,b){var d=k(a,b);a=a||{},b=b||{};var e={};e.content=a.content,d.getLabel=function(){return"saveDocument"},d.getContent=function(){return e.content};var f=d.validate;d.validate=function(a){if(typeof e.content!="string")throw c({command:d,message:"No data to save"});f(a)},d.executeOn=function(a){a.saveDocument(d)};var g=d.serialized;return d.serialized=function(){var a=g();return a.content=e.content,a},d},p=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.isWaitStatus=function(){return!1},c.isDone=function(){return!1},c},q=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c.isDone=function(){return!0},c},r=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},s=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},t=function(a,b){var c=p(a,b);return a=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},u=function(a,b){var c=p(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=[],c;for(c=0;c<d.job_id_a.length;c+=1)b.jobManager.jobIdExists(d.job_id_a[c])&&a.push(d.job_id_a[c]);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.isWaitStatus=function(){return!0},c},v=function(b,c){var d={};b=b||{},c=c||{};var e={};e.id=c.jobIdHandler.nextId(),e.command=b.command,e.storage=b.storage,e.status=s(),e.date=new Date,a("new job spec: "+JSON.stringify(b)+", priv: "+JSON.stringify(e));if(!e.storage)throw h({job:d,message:"No storage set"});if(!e.command)throw h({job:d,message:"No command set"});return d.getCommand=function(){return e.command},d.getStatus=function(){return e.status},d.getId=function(){return e.id},d.getStorage=function(){return e.storage},d.getDate=function(){return e.date},d.isReady=function(){return e.tried===0?e.status.canStart():e.status.canRestart()},d.serialized=function(){return{id:e.id,date:e.date.getTime(),status:e.status.serialized(),command:e.command.serialized(),storage:e.storage.serialized()}},d.waitForJob=function(b){a("job waitForJob(job): "+JSON.stringify(b.serialized())),e.status.getLabel()!=="wait"&&(e.status=u({},c)),e.status.waitForJob(b)},d.dontWaitFor=function(a){e.status.getLabel()==="wait"&&e.status.dontWaitForJob(a)},d.waitForTime=function(b){a("job waitForTime(ms): "+b),e.status.getLabel()!=="wait"&&(e.status=u({},c)),e.status.waitForTime(b)},d.stopWaitForTime=function(){e.status.getLabel()==="wait"&&e.status.stopWaitForTime()},d.eliminated=function(){e.command.setMaxRetry(-1),a("job eliminated(): "+JSON.stringify(d.serialized())),e.command.fail({status:0,statusText:"Stoped",message:"This job has been stoped by another one."})},d.notAccepted=function(){a("job notAccepted(): "+JSON.stringify(d.serialized())),e.command.setMaxRetry(-1),e.command.onEndDo(function(){e.status=r(),c.jobManager.terminateJob(d)}),e.command.fail({status:0,statusText:"Not Accepted",message:"This job is already running."})},d.update=function(b){a("job update(job): "+JSON.stringify(b.serialized())),e.command.setMaxRetry(-1),e.command.onEndDo(function(b){a("job update on end"+b.getLabel())}),e.command.fail({status:0,statusText:"Replaced",message:"Job has been replaced by another one."}),e.date=b.getDate(),e.command=b.getCommand(),e.status=b.getStatus()},d.execute=function(){a("job execute(): "+JSON.stringify(d.serialized()));if(e.max_retry!==0&&e.tried>=e.max_retry)throw g({job:d,message:"The job was invoked too much time."});if(!d.isReady())throw f({message:"Can not execute this job."});e.status=t(),e.command.onRetryDo(function(){a("command.retry job:"+JSON.stringify(d.serialized()));var b=e.command.getTried();b=b*b*200,b>1e4&&(b=1e4),d.waitForTime(b)}),e.command.onEndDo(function(b){e.status=b,a("command.end job:"+JSON.stringify(d.serialized())),c.jobManager.terminateJob(d)}),e.command.execute(e.storage)},d},w=function(a,b){var c={};a=a||{},b=b||{};var d=[],e=a.name||"",f=a.announcer||{};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(){f.register(c)},c.unregister=function(){f.unregister(c)},c.trigger=function(a){var b;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},x=function(b,c){var d=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}(),e=function(a,b){var c={};a=a||{},b=b||{};var d={};return c.register=function(a){d[a]||(d[a]=w())},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}(),f=function(a,b){var c={};a=a||{},b=b||{};var d=0;return c.nextId=function(){return d=d+1,d},c}(),g=function(a,b){var c={};a=a||{},b=b||{};var d="jio/job_array",e={};return e.id=a.id,e.interval_id=null,e.interval=200,e.job_array=[],b.jobManager=c,b.jobIdHandler=f,e.getJobArrayName=function(){return d+"/"+e.id},e.getJobArray=function(){return LocalOrCookieStorage.getItem(e.getJobArrayName())||[]},e.copyJobArrayToLocal=function(){var a=[],b;for(b=0;b<e.job_array.length;b+=1)a.push(e.job_array[b].serialized());LocalOrCookieStorage.setItem(e.getJobArrayName(),a)},e.removeJob=function(a){var b,c=[];for(b=0;b<e.job_array.length;b+=1)e.job_array[b]!==a&&c.push(e.job_array[b]);e.job_array=c,e.copyJobArrayToLocal()},c.setId=function(a){e.id=a},c.start=function(){var a;e.interval_id===null&&(e.interval_id=setInterval(function(){e.restoreOldJio();for(a=0;a<e.job_array.length;a+=1)c.execute(e.job_array[a])},e.interval))},c.stop=function(){e.interval_id!==null&&(clearInterval(e.interval_id),e.interval_id=null,e.job_array.length===0&&LocalOrCookieStorage.deleteItem(e.getJobArrayName()))},e.restoreOldJio=function(){var a,b;e.lastrestore=e.lastrestore||0;if(e.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)e.restoreOldJioId(b[a]);e.lastrestore=Date.now()},e.restoreOldJioId=function(a){var b;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,b<Date.now()-1e4&&(e.restoreOldJobFromJioId(a),e.removeOldJioId(a),e.removeJobArrayFromJioId(a))},e.restoreOldJobFromJioId=function(a){var d,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(d=0;d<e.length;d+=1){var f=k(e[d].command,b);f.canBeRestored()&&c.addJob(v({storage:y.storage(e[d].storage,b),command:f},b))}},e.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)},e.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}}e.copyJobArrayToLocal()},c.jobIdExists=function(a){var b;for(b=0;b<e.job_array.length;b+=1)if(e.job_array[b].getId()===a)return!0;return!1},c.terminateJob=function(a){e.removeJob(a)},c.addJob=function(a){var b=c.validateJobAccordingToJobList(e.job_array,a);e.appendJob(a,b)},c.validateJobAccordingToJobList=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)d.push(h.validateJobAccordingToJob(a[c],b));return d},e.appendJob=function(a,b){var c;if(e.job_array.length!==b.length)throw new RangeError("Array out of bound");for(c=0;c<b.length;c+=1)if(b[c].action==="dont accept")return a.notAccepted();for(c=0;c<b.length;c+=1)switch(b[c].action){case"eliminate":b[c].job.eliminated(),e.removeJob(b[c].job);break;case"update":b[c].job.update(a),e.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[c].job);break;default:}e.job_array.push(a),e.copyJobArrayToLocal()},c.serialized=function(){var a=[],b,c=e.job_array||[];for(b=0;b<c.length;b+=1)a.push(c[b].serialized());return a},c}(),h=function(b,c){var d={},e={};return e.compare={},e.action={},d.eliminate=function(){return"eliminate"},d.update=function(){return"update"},d.dontAccept=function(){return"dont accept"},d.wait=function(){return"wait"},d.none=function(){return"none"},d.default_action=d.none,d.default_compare=function(b,c){return b.getCommand().getPath()===c.getCommand().getPath()&&JSON.stringify(b.getStorage().serialized())===JSON.stringify(c.getStorage().serialized())&&a("same ! "+b.getCommand().getPath()+", "+c.getCommand().getPath()+", "+JSON.stringify(b.getStorage().serialized())+", "+JSON.stringify(c.getStorage().serialized())),b.getCommand().getPath()===c.getCommand().getPath()&&JSON.stringify(b.getStorage().serialized())===JSON.stringify(c.getStorage().serialized())},e.getAction=function(a,b){var c,f,g;return c=a.getCommand().getLabel(),f=b.getCommand().getLabel(),g=a.getStatus().getLabel()==="on going"?"on going":"not on going",e.action[c]&&e.action[c][g]&&e.action[c][g][f]?e.action[c][g][f](a,b):d.default_action(a,b)},e.canCompare=function(a,b){var c=a.getCommand().getLabel(),f=b.getCommand().getLabel();return e.compare[c]&&e.compare[f]?e.compare[c][f](a,b):d.default_compare(a,b)},d.validateJobAccordingToJob=function(a,b){return e.canCompare(a,b)?{action:e.getAction(a,b),job:a}:{action:d.default_action(a,b),job:a}},d.addActionRule=function(a,b,c,d){var f=b?"on going":"not on going";e.action[a]=e.action[a]||{},e.action[a][f]=e.action[a][f]||{},e.action[a][f][c]=d},d.addCompareRule=function(a,b,c){e.compare[a]=e.compare[a]||{},e.compare[a][b]=c},d.addActionRule("saveDocument",!0,"saveDocument",function(a,b){return a.getCommand().getContent()===b.getCommand().getContent()?d.dontAccept():d.wait()}),d.addActionRule("saveDocument",!0,"loadDocument",d.wait),d.addActionRule("saveDocument",!0,"removeDocument",d.wait),d.addActionRule("saveDocument",!1,"saveDocument",d.update),d.addActionRule("saveDocument",!1,"loadDocument",d.wait),d.addActionRule("saveDocument",!1,"removeDocument",d.eliminate),d.addActionRule("loadDocument",!0,"saveDocument",d.wait),d.addActionRule("loadDocument",!0,"loadDocument",d.dontAccept),d.addActionRule("loadDocument",!0,"removeDocument",d.wait),d.addActionRule("loadDocument",!1,"saveDocument",d.wait),d.addActionRule("loadDocument",!1,"loadDocument",d.update),d.addActionRule("loadDocument",!1,"removeDocument",d.wait),d.addActionRule("removeDocument",!0,"loadDocument",d.dontAccept),d.addActionRule("removeDocument",!0,"removeDocument",d.dontAccept),d.addActionRule("removeDocument",!1,"saveDocument",d.eliminate),d.addActionRule("removeDocument",!1,"loadDocument",d.dontAccept),d.addActionRule("removeDocument",!1,"removeDocument",d.update),d.addActionRule("getDocumentList",!0,"getDocumentList",d.dontAccept),d.addActionRule("getDocumentList",!1,"getDocumentList",d.update),d}(),i={};b=b||{},c=c||{};var j={},p="jio/id_array";return j.id=null,c.jobManager=g,c.jobIdHandler=f,j.storage=y.storage(b,c),j.init=function(){if(j.id===null){var a,b=LocalOrCookieStorage.getItem(p)||[];j.id=1;for(a=0;a<b.length;a+=1)b[a]>=j.id&&(j.id=b[a]+1);b.push(j.id),LocalOrCookieStorage.setItem(p,b),d.setId(j.id),g.setId(j.id)}},i.start=function(){j.init(),d.start(),g.start()},i.stop=function(){g.stop()},i.close=function(){d.stop(),g.stop(),j.id=null},i.start(),i.getId=function(){return j.id},i.getJobRules=function(){return h},i.validateStorageDescription=function(a){return y.storage(a,c).isValid()},i.getJobArray=function(){return g.serialized()},i.saveDocument=function(a,b,d,e){d=d||{},d.onResponse=d.onResponse||function(){},d.onDone=d.onDone||function(){},d.onFail=d.onFail||function(){},d.max_retry=d.max_retry||0,g.addJob(v({storage:e?y.storage(e,c):j.storage,command:o({path:a,content:b,option:d},c)},c))},i.loadDocument=function(a,b,d){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,g.addJob(v({storage:d?y.storage(d,c):j.storage,command:m({path:a,option:b},c)},c))},i.removeDocument=function(a,b,d){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,g.addJob(v({storage:d?y.storage(d,c):j.storage,command:n({path:a,option:b},c)},c))},i.getDocumentList=function(a,b,d){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,g.addJob(v({storage:d?y.storage(d,c):j.storage,command:l({path:a,option:b},c)},c))},i},y=function(a,b){var c={};a=a||{},b=b||{};var d={base:i,handler:j};return c.storage=function(a,b,c){a=a||{},b=b||{};var f=c||a.type||"base";if(!d[f])throw e({type:f,message:"Storage does not exists."});return d[f](a,b)},c.newJio=function(a){var b=a;return typeof b=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},x(a)},c.addStorageType=function(a,b){b=b||function(){return null};if(d[a])throw e({type:a,message:"Already known."});d[a]=b},c}();return y}();
\ No newline at end of file
......@@ -145,7 +145,7 @@ var job = function(spec, my) {
log ('job update(job): ' + JSON.stringify (job.serialized()));
priv.command.setMaxRetry(-1);
priv.command.onEndDo(function (status) {
console.log ('job update on end' + status.getLabel());
log ('job update on end' + status.getLabel());
});
priv.command.fail({status:0,statusText:'Replaced',
message:'Job has been replaced by another one.'});
......
......@@ -15,7 +15,7 @@ var jobRules = (function(spec, my) {
if (job1.getCommand().getPath() === job2.getCommand().getPath() &&
JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized())) {
console.log ('same ! ' + job1.getCommand().getPath() + ', ' +
log ('same ! ' + job1.getCommand().getPath() + ', ' +
job2.getCommand().getPath() + ', ' +
JSON.stringify (job1.getStorage().serialized())+', '+
JSON.stringify (job2.getStorage().serialized()));
......
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