Commit cb89209b authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

Jio can recover job object from older inactive Jio.

parent 37b13101
......@@ -55,6 +55,7 @@ module.exports = function(grunt) {
JIO: true,
console: true,
unescape: true,
alert: true,
// Needed to avoid "not defined error" with requireJs
define: true,
require: true,
......
/*! JIO - v0.1.0 - 2012-05-16
/*! JIO - v0.1.0 - 2012-05-18
* Copyright (c) 2012 Nexedi; Licensed */
......@@ -259,6 +259,7 @@ var JIO =
// options.publisher : is the publisher to use to send events
// options.jioID : the jio ID
var k, emptyfun = function (){};
if (options.publisher) {
priv.publisher = publisher;
}
......@@ -266,6 +267,10 @@ var JIO =
priv.jobObjectName = 'jio/jobObject/'+options.jioID;
priv.jobObject = {};
that.copyJobQueueToLocalStorage();
for (k in priv.recoveredJobObject) {
priv.recoveredJobObject[k].callback = emptyfun;
that.addJob (priv.recoveredJobObject[k]);
}
};
that.close = function () {
// close the job queue.
......@@ -284,10 +289,6 @@ var JIO =
splitk = k.split('/');
if (splitk[0] === 'jio' &&
splitk[1] === 'id') {
if (JSON.parse(localStorageObject[k]) < Date.now() - 10000){
// 10 sec ?
jioGlobalObj.localStorage.deleteItem(k);
}
if (JSON.parse(splitk[2]) >= jioGlobalObj.queueID) {
jioGlobalObj.queueID = JSON.parse(splitk[2]) + 1;
}
......@@ -297,6 +298,28 @@ var JIO =
jioGlobalObj.queueID ++;
return id;
};
that.recoverOlderJobObject = function () {
// recover job object from older inactive jio
var localStorageObject = jioGlobalObj.localStorage.getAll(),
k = 'key', splitk = ['splitedkey'];
for (k in localStorageObject) {
splitk = k.split('/');
if (splitk[0] === 'jio' &&
splitk[1] === 'id' ) {
if (localStorageObject[k] < Date.now() - 10000){
// 10 sec ? delete item
jioGlobalObj.localStorage.deleteItem(k);
// job recovery
priv.recoveredJobObject = jioGlobalObj.
localStorage.getItem('jio/jioObject/'+splitk[2]);
// remove ex job object
jioGlobalObj.localStorage.deleteItem(
'jio/jobObject/'+splitk[2]);
}
}
}
};
that.isThereJobsWhere = function( func ) {
// Check if there is jobs, in the queue,
// where [func](job) == true.
......@@ -578,6 +601,7 @@ var JIO =
priv.jioID = 0;
priv.jobObjectName = '';
priv.jobObject = {};
priv.recoveredJobObject = {};
//// end Initialize
return that;
......@@ -606,10 +630,9 @@ var JIO =
if (!priv.id) {
priv.id = setInterval (function () {
// if there is jobs
if (JSON.stringify(queue.jobObject) !== '{}') {
priv.queue.invokeAll();
}
// recover older jobs
priv.queue.recoverOlderJobObject();
priv.queue.invokeAll();
},priv.interval);
return true;
} else {
......
This diff is collapsed.
/*! JIO Storage - v0.1.0 - 2012-05-16
/*! JIO Storage - v0.1.0 - 2012-05-18
* Copyright (c) 2012 Nexedi; Licensed */
......
/*! JIO Storage - v0.1.0 - 2012-05-16
/*! JIO Storage - v0.1.0 - 2012-05-18
* Copyright (c) 2012 Nexedi; Licensed */
(function(){var a=function(a,b,c,d){var e=function(){var a=!0,c=function(b){console.error("Fail to load "+b),a=!1};try{b||c("Base64")}catch(d){c("Base64")}return a},f,g,h;f=function(b){var e=c.newBaseStorage(b),f={};return e.checkNameAvailability=function(){setTimeout(function(){var b=null;b=a.getAll();for(var c in b){var d=c.split("/");if(d[0]==="jio"&&d[1]==="local"&&d[2]===e.getUserName())return e.done(!1)}return e.done(!0)},100)},e.saveDocument=function(){setTimeout(function(){var b=null;return b=a.getItem("jio/local/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName()),b?(b.lastModified=Date.now(),b.fileContent=e.getFileContent()):b={fileName:e.getFileName(),fileContent:e.getFileContent(),creationDate:Date.now(),lastModified:Date.now()},a.setItem("jio/local/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName(),b),e.done()},100)},e.loadDocument=function(){setTimeout(function(){var b=null,c=d.extend({getContent:!0},e.cloneOptionObject());b=a.getItem("jio/local/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName()),b?(c.getContent||delete b.fileContent,e.done(b)):e.fail("Document not found.",404)},100)},e.getDocumentList=function(){setTimeout(function(){var b=[],c=null,d="key",f=["splitedkey"],g={};c=a.getAll();for(d in c)f=d.split("/"),f[0]==="jio"&&f[1]==="local"&&f[2]===e.getStorageUserName()&&f[3]===e.getApplicantID()&&(g=JSON.parse(c[d]),b.push({fileName:g.fileName,creationDate:g.creationDate,lastModified:g.lastModified}));e.done(b)},100)},e.removeDocument=function(){setTimeout(function(){return a.deleteItem("jio/local/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName()),e.done()},100)},e},g=function(a){var e=c.newBaseStorage(a);return e.mkcol=function(a){var c=d.extend({success:function(){},error:function(){}},a),f=["splitedpath"],g="temp/path";if(!c.pathsteps)c.pathsteps=1,e.mkcol(c);else{f=c.path.split("/");if(c.pathsteps>=f.length-1)return c.success();f.length=c.pathsteps+1,c.pathsteps++,g=f.join("/"),d.ajax({url:c.location+g,type:"MKCOL",async:!0,headers:{Authorization:"Basic "+b.encode(c.userName+":"+c.password),Depth:"1"},success:function(){e.mkcol(c)},error:function(a){c.error()}})}},e.checkNameAvailability=function(){d.ajax({url:e.getStorageLocation()+"/dav/"+e.getStorageUserName()+"/",async:!0,type:"PROPFIND",dataType:"xml",headers:{Authorization:"Basic "+b.encode(e.getStorageUserName()+":"+e.getStoragePassword()),Depth:"1"},success:function(a){e.done(!1)},error:function(a){a.status===404?e.done(!0):e.fail("Cannot check if "+e.getUserName()+" is available.",a.status)}})},e.saveDocument=function(){d.ajax({url:e.getStorageLocation()+"/dav/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName(),type:"PUT",data:e.getFileContent(),async:!0,dataType:"text",headers:{Authorization:"Basic "+b.encode(e.getStorageUserName()+":"+e.getStoragePassword())},success:function(){e.done()},error:function(a){e.fail("Cannot save document.",a.status)}})},e.loadDocument=function(){var a={},c=d.extend({getContent:!0},e.cloneOptionObject()),f=function(){d.ajax({url:e.getStorageLocation()+"/dav/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName(),type:"GET",async:!0,dataType:"text",headers:{Authorization:"Basic "+b.encode(e.getStorageUserName()+":"+e.getStoragePassword())},success:function(b){a.fileContent=b,e.done(a)},error:function(a){var b;a.status===404?b="Document not found.":b='Cannot load "'+e.getFileName()+'".',e.fail(b,a.status)}})};d.ajax({url:e.getStorageLocation()+"/dav/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName(),type:"PROPFIND",async:!0,dataType:"xml",headers:{Authorization:"Basic "+b.encode(e.getStorageUserName()+":"+e.getStoragePassword())},success:function(b){d(b).find("lp1\\:getlastmodified, getlastmodified").each(function(){a.lastModified=d(this).text()}),d(b).find("lp1\\:creationdate, creationdate").each(function(){a.creationDate=d(this).text()}),a.fileName=e.getFileName(),c.getContent?f():e.done(a)},error:function(a){e.fail("Cannot get document informations.",a.status)}})},e.getDocumentList=function(){var a=[],c={},f=[];d.ajax({url:e.getStorageLocation()+"/dav/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/",async:!0,type:"PROPFIND",dataType:"xml",headers:{Authorization:"Basic "+b.encode(e.getStorageUserName()+":"+e.getStoragePassword()),Depth:"1"},success:function(b){d(b).find("D\\:response, response").each(function(b,e){if(b>0){c={},d(e).find("D\\:href, href").each(function(){f=d(this).text().split("/"),c.fileName=f[f.length-1]?f[f.length-1]:f[f.length-2]+"/"});if(c.fileName===".htaccess"||c.fileName===".htpasswd")return;d(e).find("lp1\\:getlastmodified, getlastmodified").each(function(){c.lastModified=d(this).text()}),d(e).find("lp1\\:creationdate, creationdate").each(function(){c.creationDate=d(this).text()}),a.push(c)}}),e.done(a)},error:function(a){e.fail("Cannot get list.",a.status)}})},e.removeDocument=function(){d.ajax({url:e.getStorageLocation()+"/dav/"+e.getStorageUserName()+"/"+e.getApplicantID()+"/"+e.getFileName(),type:"DELETE",async:!0,headers:{Authorization:"Basic "+b.encode(e.getStorageUserName()+":"+e.getStoragePassword())},success:function(){e.done()},error:function(a){a.status===404?e.done():e.fail('Cannot remove "'+e.getFileName()+'".',a.status)}})},e},h=function(a){var b=c.newBaseStorage(a),e={};return e.storageArray=b.getStorageArray(),e.length=e.storageArray.length,e.returnsValuesArray=[],e.maxtries=b.getMaxTries(),b.setMaxTries(1),b.checkNameAvailability=function(){var a={},c=!0,d="id",f={status:"done"},g=function(a){e.returnsValuesArray.push(a),a.status==="fail"&&(f.status="fail"),a.isAvailable||(c=!1),e.returnsValuesArray.length===e.length&&b.done(c)};for(d in e.storageArray)a=b.cloneJob(),a.maxtries=e.maxtries,a.storage=e.storageArray[d],a.callback=g,b.addJob(a)},b.saveDocument=function(){var a={},c={status:"done"},d="id",f=function(a){e.returnsValuesArray.push(a),a.status==="fail"&&(c.status="fail"),e.returnsValuesArray.length===e.length&&(c.status==="fail"?b.fail("Unable to save all files.",0):b.done())};for(d in e.storageArray)a=b.cloneJob(),a.maxtries=e.maxtries,a.storage=e.storageArray[d],a.callback=f,b.addJob(a)},b.loadDocument=function(){var a={},c=!1,f={},g="id",h={status:"done"},i=function(a){e.returnsValuesArray.push(a),a.status==="fail"?h.status="fail":!f.fileContent&&!f.creationDate&&!f.lastModified?f=d.extend({},a.document):(f.fileContent!==a.document.fileContent&&(c=!0),f.creationDate>a.document.creationDate&&(f.creationDate=a.document.creationDate),f.lastModified<a.document.lastModified&&(f.fileContent=a.document.fileContent,f.lastModified=a.document.lastModified)),e.returnsValuesArray.length===e.length&&(h.status==="fail"?b.fail("Unable to load all files.",0):c?b.done(f):b.done(f))};for(g in e.storageArray)a=b.cloneJob(),a.maxtries=e.maxtries,a.storage=e.storageArray[g],a.callback=i,b.addJob(a)},b.getDocumentList=function(){var a={},c={status:"done"},d="id",f=function(a){e.returnsValuesArray.push(a),a.status==="fail"&&(c.status="fail"),e.returnsValuesArray.length===e.length&&(c.status==="fail"?b.fail("Unable retrieve all lists.",0):b.done(a.list))};for(d in e.storageArray)a=b.cloneJob(),a.maxtries=e.maxtries,a.storage=e.storageArray[d],a.callback=f,b.addJob(a)},b.removeDocument=function(){var a={},c={status:"done"},d="key",f=function(a){e.returnsValuesArray.push(a),a.status==="fail"&&(c.status="fail"),e.returnsValuesArray.length===e.length&&(c.status==="fail"?b.fail("Unable remove all files.",0):b.done())};for(d in e.storageArray)a=b.cloneJob(),a.maxtries=e.maxtries,a.storage=e.storageArray[d],a.callback=f,b.addJob(a)},b},c.addStorageType("local",function(a){return new f(a)}),c.addStorageType("dav",function(a){return new g(a)}),c.addStorageType("replicate",function(a){return new h(a)})};window.requirejs?define("JIOStorages",["LocalOrCookieStorage","Base64","JIO","jQuery"],a):a(LocalOrCookieStorage,Base64,JIO,jQuery)})();
\ No newline at end of file
/*! Local Or Cookie Storage - v0.1.0 - 2012-05-16
/*! Local Or Cookie Storage - v0.1.0 - 2012-05-18
* Copyright (c) 2012 Nexedi; Licensed */
var LocalOrCookieStorage =
......
/*! Local Or Cookie Storage - v0.1.0 - 2012-05-16
/*! Local Or Cookie Storage - v0.1.0 - 2012-05-18
* Copyright (c) 2012 Nexedi; Licensed */
var LocalOrCookieStorage=function(){var a=function(){var a=function(){};a.prototype={getItem:function(a){return JSON.parse(localStorage.getItem(a))},setItem:function(a,b){if(a)return localStorage.setItem(a,JSON.stringify(b))},getAll:function(){return localStorage},deleteItem:function(a){a&&delete localStorage[a]}};var b=function(){};b.prototype={getItem:function(a){var b=document.cookie.split(";");for(var c in b){var d=b[c].substr(0,b[c].indexOf("=")),e=b[c].substr(b[c].indexOf("=")+1);d=d.replace(/^\s+|\s+$/g,"");if(d===a)return unescape(e)}return null},setItem:function(a,b){return b!==undefined?(document.cookie=a+"="+JSON.stringify(b)+";domain="+window.location.hostname+";path="+window.location.pathname,!0):!1},getAll:function(){var a={},b=document.cookie.split(":");for(var c in b){var d=b[c].substr(0,b[c].indexOf("=")),e=b[c].substr(b[c].indexOf("=")+1);d=d.replace(/^\s+|\s+$/g,""),a[d]=unescape(e)}return a},deleteItem:function(a){document.cookie=a+"=null;domain="+window.location.hostname+";path="+window.location.pathname+";expires=Thu, 01-Jan-1970 00:00:01 GMT"}};try{return localStorage.getItem?new a:new b}catch(c){return new b}};return window.requirejs?(define("LocalOrCookieStorage",[],a),undefined):a()}();
\ No newline at end of file
......@@ -256,6 +256,7 @@ var JIO =
// options.publisher : is the publisher to use to send events
// options.jioID : the jio ID
var k, emptyfun = function (){};
if (options.publisher) {
priv.publisher = publisher;
}
......@@ -263,6 +264,10 @@ var JIO =
priv.jobObjectName = 'jio/jobObject/'+options.jioID;
priv.jobObject = {};
that.copyJobQueueToLocalStorage();
for (k in priv.recoveredJobObject) {
priv.recoveredJobObject[k].callback = emptyfun;
that.addJob (priv.recoveredJobObject[k]);
}
};
that.close = function () {
// close the job queue.
......@@ -281,10 +286,6 @@ var JIO =
splitk = k.split('/');
if (splitk[0] === 'jio' &&
splitk[1] === 'id') {
if (JSON.parse(localStorageObject[k]) < Date.now() - 10000){
// 10 sec ?
jioGlobalObj.localStorage.deleteItem(k);
}
if (JSON.parse(splitk[2]) >= jioGlobalObj.queueID) {
jioGlobalObj.queueID = JSON.parse(splitk[2]) + 1;
}
......@@ -294,6 +295,28 @@ var JIO =
jioGlobalObj.queueID ++;
return id;
};
that.recoverOlderJobObject = function () {
// recover job object from older inactive jio
var localStorageObject = jioGlobalObj.localStorage.getAll(),
k = 'key', splitk = ['splitedkey'];
for (k in localStorageObject) {
splitk = k.split('/');
if (splitk[0] === 'jio' &&
splitk[1] === 'id' ) {
if (localStorageObject[k] < Date.now() - 10000){
// 10 sec ? delete item
jioGlobalObj.localStorage.deleteItem(k);
// job recovery
priv.recoveredJobObject = jioGlobalObj.
localStorage.getItem('jio/jioObject/'+splitk[2]);
// remove ex job object
jioGlobalObj.localStorage.deleteItem(
'jio/jobObject/'+splitk[2]);
}
}
}
};
that.isThereJobsWhere = function( func ) {
// Check if there is jobs, in the queue,
// where [func](job) == true.
......@@ -575,6 +598,7 @@ var JIO =
priv.jioID = 0;
priv.jobObjectName = '';
priv.jobObject = {};
priv.recoveredJobObject = {};
//// end Initialize
return that;
......@@ -603,10 +627,9 @@ var JIO =
if (!priv.id) {
priv.id = setInterval (function () {
// if there is jobs
if (JSON.stringify(queue.jobObject) !== '{}') {
priv.queue.invokeAll();
}
// recover older jobs
priv.queue.recoverOlderJobObject();
priv.queue.invokeAll();
},priv.interval);
return true;
} else {
......
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