Commit 979fca4a authored by Tristan Cavelier's avatar Tristan Cavelier

Jio can now restore older jio instance.

parent 3d2ae5e9
...@@ -15,12 +15,12 @@ module.exports = function(grunt) { ...@@ -15,12 +15,12 @@ module.exports = function(grunt) {
dist: { dist: {
src: ['<banner:meta.banner>', src: ['<banner:meta.banner>',
// Wrapper top // Wrapper top
'<file_strip_banner:../../src/<%= pkg.name %>/wrapper.top.js>', '<file_strip_banner:../../src/<%= pkg.name %>/intro.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/exceptions.js>', '<file_strip_banner:../../src/<%= pkg.name %>/exceptions.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/storages/storage.js>', '<file_strip_banner:../../src/<%= pkg.name %>/storages/storage.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/storages/storageHandler.js>', '<file_strip_banner:../../src/<%= pkg.name %>/storages/storageHandler.js>',
// Jio wrapper top // Jio wrapper top
'<file_strip_banner:../../src/<%= pkg.name %>/jio.top.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jio.intro.js>',
// Jio Classes // Jio Classes
'<file_strip_banner:../../src/<%= pkg.name %>/commands/command.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/command.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/getDocumentList.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/getDocumentList.js>',
...@@ -42,10 +42,10 @@ module.exports = function(grunt) { ...@@ -42,10 +42,10 @@ module.exports = function(grunt) {
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/jobManager.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jobs/jobManager.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/jobRules.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jobs/jobRules.js>',
// Jio wrappor bottem // Jio wrappor bottem
'<file_strip_banner:../../src/<%= pkg.name %>/jio.bottom.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jio.outro.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jioNamespace.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jioNamespace.js>',
// Wrapper bottom // Wrapper bottom
'<file_strip_banner:../../src/<%= pkg.name %>/wrapper.bottom.js>'], '<file_strip_banner:../../src/<%= pkg.name %>/outro.js>'],
dest: '../../lib/jio/<%= pkg.name %>.js' dest: '../../lib/jio/<%= pkg.name %>.js'
} }
}, },
...@@ -87,10 +87,7 @@ module.exports = function(grunt) { ...@@ -87,10 +87,7 @@ module.exports = function(grunt) {
browser: true browser: true
}, },
globals: { globals: {
jQuery: true,
sjcl:true,
LocalOrCookieStorage: true, LocalOrCookieStorage: true,
Base64: true,
console: true, console: true,
unescape: true, unescape: true,
// Needed to avoid "not defined error" with requireJs // Needed to avoid "not defined error" with requireJs
...@@ -112,6 +109,6 @@ module.exports = function(grunt) { ...@@ -112,6 +109,6 @@ module.exports = function(grunt) {
}); });
// Default task. // Default task.
grunt.registerTask('default', 'concat min lint qunit'); grunt.registerTask('default', 'concat lint min qunit');
}; };
...@@ -14,7 +14,9 @@ module.exports = function(grunt) { ...@@ -14,7 +14,9 @@ module.exports = function(grunt) {
concat: { concat: {
dist: { dist: {
src: ['<banner:meta.banner>', src: ['<banner:meta.banner>',
'<file_strip_banner:../../src/<%= pkg.name %>.js>'], '<file_strip_banner:../../src/<%= pkg.name %>/intro.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/localStorage.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/outro.js>'],
dest: '../../lib/jio/<%= pkg.name %>.js' dest: '../../lib/jio/<%= pkg.name %>.js'
} }
}, },
...@@ -28,7 +30,7 @@ module.exports = function(grunt) { ...@@ -28,7 +30,7 @@ module.exports = function(grunt) {
files: [] files: []
}, },
lint: { lint: {
files: ['grunt.js','../../src/<%= pkg.name %>.js'] files: ['grunt.js','../../lib/jio/<%= pkg.name %>.js']
}, },
watch: { watch: {
files: '<config:lint.files>', files: '<config:lint.files>',
...@@ -53,7 +55,7 @@ module.exports = function(grunt) { ...@@ -53,7 +55,7 @@ module.exports = function(grunt) {
sjcl: true, sjcl: true,
LocalOrCookieStorage: true, LocalOrCookieStorage: true,
Base64: true, Base64: true,
JIO: true, jio: true,
console: true, console: true,
unescape: true, unescape: true,
// Needed to avoid "not defined error" with requireJs // Needed to avoid "not defined error" with requireJs
...@@ -75,6 +77,6 @@ module.exports = function(grunt) { ...@@ -75,6 +77,6 @@ module.exports = function(grunt) {
}); });
// Default task. // Default task.
grunt.registerTask('default', 'lint concat min'); grunt.registerTask('default', 'concat lint min');
}; };
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
for node in `ls -1` ; do for node in `ls -1` ; do
if [ -d "$node" ] ; then if [ -d "$node" ] ; then
cd "$node"
printf "\n\033[1m\033[36mgrunt --force -> %s\033[0m\n" "$node" printf "\n\033[1m\033[36mmake -C %s\033[0m\n" "$node"
grunt --force make -C "$node"
cd ..
fi fi
done done
This diff is collapsed.
This diff is collapsed.
...@@ -10,9 +10,7 @@ ...@@ -10,9 +10,7 @@
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Dummy Storage 1 : all ok // Dummy Storage 1 : all ok
var newDummyStorageAllOk = function ( spec, my ) { var newDummyStorageAllOk = function ( spec, my ) {
var that = Jio.storage( {type:'base'}, my ); var that = Jio.storage( spec, my, 'base' );
that.setType('dummyallok');
that.saveDocument = function (command) { that.saveDocument = function (command) {
// Tells us that the document is saved. // Tells us that the document is saved.
...@@ -74,7 +72,7 @@ ...@@ -74,7 +72,7 @@
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Dummy Storage 2 : all fail // Dummy Storage 2 : all fail
newDummyStorageAllFail = function ( spec, my ) { newDummyStorageAllFail = function ( spec, my ) {
var that = Jio.storage( {type:'base'}, my ); var that = Jio.storage( spec, my, 'base' );
that.setType('dummyallfail'); that.setType('dummyallfail');
...@@ -121,7 +119,7 @@ ...@@ -121,7 +119,7 @@
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Dummy Storage 3 : all not found // Dummy Storage 3 : all not found
newDummyStorageAllNotFound = function ( spec, my ) { newDummyStorageAllNotFound = function ( spec, my ) {
var that = Jio.storage( {type:'base'}, my ); var that = Jio.storage( spec, my, 'base' );
that.setType('dummyallnotfound'); that.setType('dummyallnotfound');
...@@ -167,7 +165,7 @@ ...@@ -167,7 +165,7 @@
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Dummy Storage 4 : all 3 tries // Dummy Storage 4 : all 3 tries
newDummyStorageAll3Tries = function ( spec, my ) { newDummyStorageAll3Tries = function ( spec, my ) {
var that = Jio.storage( {type:'base'}, my ), priv = {}; var that = Jio.storage( spec, my, 'base' ), priv = {};
that.setType('dummyall3tries'); that.setType('dummyall3tries');
......
auto: grunt
check-syntax: grunt
grunt:
make -C ../../grunt/*_gruntJIO
...@@ -36,3 +36,4 @@ var activityUpdater = (function(spec, my) { ...@@ -36,3 +36,4 @@ var activityUpdater = (function(spec, my) {
}; };
return that; return that;
}()); }());
...@@ -103,7 +103,6 @@ var command = function(spec, my) { ...@@ -103,7 +103,6 @@ var command = function(spec, my) {
}; };
that.done = function(return_value) { that.done = function(return_value) {
console.log ('done');
priv.done(return_value); priv.done(return_value);
priv.respond({status:doneStatus(),value:return_value}); priv.respond({status:doneStatus(),value:return_value});
priv.end(); priv.end();
...@@ -113,7 +112,6 @@ var command = function(spec, my) { ...@@ -113,7 +112,6 @@ var command = function(spec, my) {
if (priv.option.max_retry === 0 || priv.tried < priv.option.max_retry) { if (priv.option.max_retry === 0 || priv.tried < priv.option.max_retry) {
priv.retry(); priv.retry();
} else { } else {
console.log ('fail');
priv.fail(return_error); priv.fail(return_error);
priv.respond({status:failStatus(),error:return_error}); priv.respond({status:failStatus(),error:return_error});
priv.end(); priv.end();
...@@ -142,5 +140,9 @@ var command = function(spec, my) { ...@@ -142,5 +140,9 @@ var command = function(spec, my) {
option:priv.option}; option:priv.option};
}; };
that.canBeRestored = function() {
return true;
};
return that; return that;
}; };
...@@ -12,5 +12,9 @@ var getDocumentList = function(spec, my) { ...@@ -12,5 +12,9 @@ var getDocumentList = function(spec, my) {
storage.getDocumentList(that); storage.getDocumentList(that);
}; };
that.canBeRestored = function() {
return false;
};
return that; return that;
}; };
...@@ -12,5 +12,9 @@ var loadDocument = function(spec, my) { ...@@ -12,5 +12,9 @@ var loadDocument = function(spec, my) {
storage.loadDocument(that); storage.loadDocument(that);
}; };
that.canBeRestored = function() {
return false;
};
return that; return that;
}; };
...@@ -3,14 +3,15 @@ var saveDocument = function(spec, my) { ...@@ -3,14 +3,15 @@ var saveDocument = function(spec, my) {
spec = spec || {}; spec = spec || {};
my = my || {}; my = my || {};
// Attributes // // Attributes //
var content = spec.content; var priv = {};
priv.content = spec.content;
// Methods // // Methods //
that.getLabel = function() { that.getLabel = function() {
return 'saveDocument'; return 'saveDocument';
}; };
that.getContent = function() { that.getContent = function() {
return content; return priv.content;
}; };
/** /**
...@@ -19,7 +20,7 @@ var saveDocument = function(spec, my) { ...@@ -19,7 +20,7 @@ var saveDocument = function(spec, my) {
*/ */
var super_validate = that.validate; var super_validate = that.validate;
that.validate = function(handler) { that.validate = function(handler) {
if (typeof content !== 'string') { if (typeof priv.content !== 'string') {
throw invalidCommandState({command:that,message:'No data to save'}); throw invalidCommandState({command:that,message:'No data to save'});
} }
super_validate(handler); super_validate(handler);
...@@ -29,5 +30,12 @@ var saveDocument = function(spec, my) { ...@@ -29,5 +30,12 @@ var saveDocument = function(spec, my) {
storage.saveDocument(that); storage.saveDocument(that);
}; };
var super_serialized = that.serialized;
that.serialized = function() {
var o = super_serialized();
o.content = priv.content;
return o;
};
return that; return that;
}; };
...@@ -5,14 +5,16 @@ ...@@ -5,14 +5,16 @@
// Attributes // // Attributes //
var priv = {}; var priv = {};
var jio_id_array_name = 'jio/id_array'; var jio_id_array_name = 'jio/id_array';
priv.id = 1; priv.id = null;
priv.storage = jioNamespace.storage(spec, that); priv.storage = jioNamespace.storage(spec);
// initialize // // initialize //
(function () { priv.init = function() {
// Initialize the jio id and add the new id to the list // Initialize the jio id and add the new id to the list
var i, if (priv.id === null) {
jio_id_a = LocalOrCookieStorage.getItem (jio_id_array_name) || []; var i, jio_id_a =
LocalOrCookieStorage.getItem (jio_id_array_name) || [];
priv.id = 1;
for (i = 0; i < jio_id_a.length; i+= 1) { for (i = 0; i < jio_id_a.length; i+= 1) {
if (jio_id_a[i] >= priv.id) { if (jio_id_a[i] >= priv.id) {
priv.id = jio_id_a[i] + 1; priv.id = jio_id_a[i] + 1;
...@@ -20,22 +22,26 @@ ...@@ -20,22 +22,26 @@
} }
jio_id_a.push(priv.id); jio_id_a.push(priv.id);
LocalOrCookieStorage.setItem (jio_id_array_name,jio_id_a); LocalOrCookieStorage.setItem (jio_id_array_name,jio_id_a);
}());
(function (){
// Start Jio updater, and the jobManager
activityUpdater.setId(priv.id); activityUpdater.setId(priv.id);
activityUpdater.start();
jobManager.setId(priv.id); jobManager.setId(priv.id);
jobManager.start(); }
}()); };
// Methods // // Methods //
that.start = function() { that.start = function() {
priv.init();
activityUpdater.start();
jobManager.start(); jobManager.start();
}; };
that.stop = function() { that.stop = function() {
jobManager.stop(); jobManager.stop();
}; };
that.close = function() {
activityUpdater.stop();
jobManager.stop();
priv.id = null;
};
that.start();
/** /**
* Returns the jio id. * Returns the jio id.
......
...@@ -12,11 +12,12 @@ var jioNamespace = (function(spec, my) { ...@@ -12,11 +12,12 @@ var jioNamespace = (function(spec, my) {
* @method storage * @method storage
* @param {object} spec The specifications. * @param {object} spec The specifications.
* @param {object} my The protected object. * @param {object} my The protected object.
* @param {string} forcetype Force storage type
* @return {object} The storage object. * @return {object} The storage object.
*/ */
that.storage = function(spec, my) { that.storage = function(spec, my, forcetype) {
spec = spec || {}; spec = spec || {};
var type = spec.type || 'base'; var type = forcetype || spec.type || 'base';
if (!storage_type_o[type]) { if (!storage_type_o[type]) {
throw invalidStorageType({type:type, throw invalidStorageType({type:type,
message:'Storage does not exists.'}); message:'Storage does not exists.'});
......
...@@ -122,7 +122,6 @@ var job = function(spec, my) { ...@@ -122,7 +122,6 @@ var job = function(spec, my) {
* @param {object} job The other job. * @param {object} job The other job.
*/ */
that.update = function(job) { that.update = function(job) {
console.log ('updating');
priv.command.setMaxRetry(-1); priv.command.setMaxRetry(-1);
priv.command.fail({status:0,statusText:'Replaced', priv.command.fail({status:0,statusText:'Replaced',
message:'Job has been replaced by another one.'}); message:'Job has been replaced by another one.'});
......
...@@ -92,6 +92,7 @@ var jobManager = (function(spec, my) { ...@@ -92,6 +92,7 @@ var jobManager = (function(spec, my) {
if (jio_date < Date.now() - 10000) { if (jio_date < Date.now() - 10000) {
priv.restoreOldJobFromJioId(id); priv.restoreOldJobFromJioId(id);
priv.removeOldJioId(id); priv.removeOldJioId(id);
priv.removeJobArrayFromJioId(id);
} }
}; };
...@@ -99,9 +100,12 @@ var jobManager = (function(spec, my) { ...@@ -99,9 +100,12 @@ var jobManager = (function(spec, my) {
var i, jio_job_array; var i, jio_job_array;
jio_job_array = LocalOrCookieStorage.getItem('jio/job_array/'+id)||[]; jio_job_array = LocalOrCookieStorage.getItem('jio/job_array/'+id)||[];
for (i = 0; i < jio_job_array.length; i+= 1) { for (i = 0; i < jio_job_array.length; i+= 1) {
var command_o = command(jio_job_array[i].command);
if (command_o.canBeRestored()) {
that.addJob ( job( that.addJob ( job(
{storage:jioNamespace.storage(jio_job_array[i]), {storage:jioNamespace.storage(jio_job_array[i].storage),
command:command(jio_job_array[i].command)})); command:command_o}));
}
} }
}; };
...@@ -114,6 +118,11 @@ var jobManager = (function(spec, my) { ...@@ -114,6 +118,11 @@ var jobManager = (function(spec, my) {
} }
} }
LocalOrCookieStorage.setItem('jio/id_array',new_a); LocalOrCookieStorage.setItem('jio/id_array',new_a);
LocalOrCookieStorage.deleteItem('jio/id/'+id);
};
priv.removeJobArrayFromJioId = function(id) {
LocalOrCookieStorage.deleteItem('jio/job_array/'+id);
}; };
/** /**
...@@ -138,11 +147,9 @@ var jobManager = (function(spec, my) { ...@@ -138,11 +147,9 @@ var jobManager = (function(spec, my) {
var i; var i;
for (i = 0; i < priv.job_a.length; i+= 1) { for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i].getId() === id) { if (priv.job_a[i].getId() === id) {
console.log ('found');
return true; return true;
} }
} }
console.log ('not found');
return false; return false;
}; };
...@@ -174,11 +181,9 @@ var jobManager = (function(spec, my) { ...@@ -174,11 +181,9 @@ var jobManager = (function(spec, my) {
return; return;
} }
} }
console.log ('managing '+JSON.stringify (result_a));
for (i = 0; i < result_a.length; i+= 1) { for (i = 0; i < result_a.length; i+= 1) {
switch (result_a[i].action) { switch (result_a[i].action) {
case 'eliminate': case 'eliminate':
console.log ('eliminating');
that.eliminate(result_a[i].job); that.eliminate(result_a[i].job);
break; break;
case 'update': case 'update':
...@@ -186,7 +191,6 @@ var jobManager = (function(spec, my) { ...@@ -186,7 +191,6 @@ var jobManager = (function(spec, my) {
priv.copyJobArrayToLocal(); priv.copyJobArrayToLocal();
return; return;
case 'wait': case 'wait':
console.log ('wait');
job.waitForJob(result_a[i].job); job.waitForJob(result_a[i].job);
break; break;
default: break; default: break;
...@@ -201,7 +205,6 @@ var jobManager = (function(spec, my) { ...@@ -201,7 +205,6 @@ var jobManager = (function(spec, my) {
for (i = 0; i < priv.job_a.length; i+= 1) { for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i].getId() !== job.getId()) { if (priv.job_a[i].getId() !== job.getId()) {
tmp_a.push(priv.job_a[i]); tmp_a.push(priv.job_a[i]);
console.log ('add: '+priv.job_a[i].getId()+' -> it is not '+job.getId());
} }
} }
priv.job_a = tmp_a; priv.job_a = tmp_a;
......
...@@ -138,30 +138,22 @@ var jobRules = (function(spec, my) { ...@@ -138,30 +138,22 @@ var jobRules = (function(spec, my) {
j2label = job2.getCommand().getLabel(); j2label = job2.getCommand().getLabel();
j1status = (job1.getStatus().getLabel()==='on going'? j1status = (job1.getStatus().getLabel()==='on going'?
'on going':'not on going'); 'on going':'not on going');
try { if (priv.action[j1label] &&
console.log (j1label); priv.action[j1label][j1status] &&
console.log (j2label); priv.action[j1label][j1status][j2label]) {
console.log (j1status);
return priv.action[j1label][j1status][j2label](job1,job2); return priv.action[j1label][j1status][j2label](job1,job2);
} catch (e) {
if(e.name==='TypeError') {
return priv.default_action(job1,job2);
} else { } else {
throw e; return priv.default_action(job1,job2);
}
} }
}; };
priv.canCompare = function(job1,job2) { priv.canCompare = function(job1,job2) {
var job1label = job1.getCommand().getLabel(), var job1label = job1.getCommand().getLabel(),
job2label = job2.getCommand().getLabel(); job2label = job2.getCommand().getLabel();
try { if (priv.compare[job1label] &&
priv.compare[job2label]) {
return priv.compare[job1label][job2label](job1,job2); return priv.compare[job1label][job2label](job1,job2);
} catch(e) {
if (e.name==='TypeError') {
return priv.default_compare(job1,job2);
} else { } else {
throw e; return priv.default_compare(job1,job2);
}
} }
}; };
that.validateJobAccordingToJob = function(job1,job2) { that.validateJobAccordingToJob = function(job1,job2) {
......
...@@ -49,7 +49,6 @@ var waitStatus = function(spec, my) { ...@@ -49,7 +49,6 @@ var waitStatus = function(spec, my) {
that.canStart = function() { that.canStart = function() {
priv.refreshJobIdArray(); priv.refreshJobIdArray();
console.log (priv.job_id_a);
return (priv.job_id_a.length === 0 && Date.now() >= priv.threshold); return (priv.job_id_a.length === 0 && Date.now() >= priv.threshold);
}; };
that.canRestart = function() { that.canRestart = function() {
......
...@@ -21,6 +21,7 @@ var storage = function(spec, my) { ...@@ -21,6 +21,7 @@ var storage = function(spec, my) {
* @param {object} command The command * @param {object} command The command
*/ */
that.execute = function(command) { that.execute = function(command) {
that.validate(command);
command.executeOn(that); command.executeOn(that);
}; };
...@@ -34,6 +35,10 @@ var storage = function(spec, my) { ...@@ -34,6 +35,10 @@ var storage = function(spec, my) {
}; };
that.validate = function(command) { that.validate = function(command) {
var mess = that.validateState();
if (mess) {
throw invalidStorage({storage:that,message:mess});
}
command.validate(that); command.validate(that);
}; };
...@@ -59,5 +64,9 @@ var storage = function(spec, my) { ...@@ -59,5 +64,9 @@ var storage = function(spec, my) {
that.saveDocument(); that.saveDocument();
}; };
that.validateState = function() {
return '';
};
return that; return that;
}; };
...@@ -12,24 +12,22 @@ var storageHandler = function(spec, my) { ...@@ -12,24 +12,22 @@ var storageHandler = function(spec, my) {
* Override this function. * Override this function.
* @method beforeExecute * @method beforeExecute
* @param {object} command The command. * @param {object} command The command.
* @param {object} option Some options.
*/ */
that.beforeExecute = function(command,option) {}; that.beforeExecute = function(command) {};
/** /**
* Execute the command according to this storage. * Execute the command according to this storage.
* @method execute * @method execute
* @param {object} command The command. * @param {object} command The command.
* @param {object} option Some options.
*/ */
that.execute = function(command,option) { that.execute = function(command) {
var i; var i;
that.validate(command); that.validate(command);
that.beforeExecute(command,option); that.beforeExecute(command);
for(i = 0; i < priv.storage_a.length; i++) { for(i = 0; i < priv.storage_a.length; i++) {
priv.storage_a[i].execute(command); priv.storage_a[i].execute(command);
} }
that.afterExecute(command,option); that.afterExecute(command);
}; };
/** /**
...@@ -37,10 +35,9 @@ var storageHandler = function(spec, my) { ...@@ -37,10 +35,9 @@ var storageHandler = function(spec, my) {
* Override this function. * Override this function.
* @method afterExecute * @method afterExecute
* @param {object} command The command. * @param {object} command The command.
* @param {object} option Some options.
*/ */
that.afterExecute = function(command,option) { that.afterExecute = function(command) {
that.done(); command.done();
}; };
/** /**
......
This diff is collapsed.
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