Commit 3d2ae5e9 authored by Tristan Cavelier's avatar Tristan Cavelier

Improving the new jio, it just doesn't restore older jio instance for now.

parent a9caa60c
......@@ -16,28 +16,27 @@ module.exports = function(grunt) {
src: ['<banner:meta.banner>',
// Wrapper top
'<file_strip_banner:../../src/<%= pkg.name %>/wrapper.top.js>',
// Classes
'<file_strip_banner:../../src/<%= pkg.name %>/exceptions.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/storages/storage.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/storages/storageHandler.js>',
// Jio wrapper top
'<file_strip_banner:../../src/<%= pkg.name %>/jio.top.js>',
// Jio Classes
'<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/loadDocument.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/removeDocument.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/saveDocument.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/exceptions.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/jobStatus.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/doneStatus.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/failStatus.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/initialStatus.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/onGoingStatus.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/waitStatus.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 %>/activityUpdater.js>',
// Jio wrapper top
'<file_strip_banner:../../src/<%= pkg.name %>/jio.top.js>',
// Jio Classes
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/job.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcement.js>',
// Singletons
'<file_strip_banner:../../src/<%= pkg.name %>/activityUpdater.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/announcements/announcer.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/jobIdHandler.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/jobManager.js>',
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -4,11 +4,28 @@ var command = function(spec, my) {
my = my || {};
// Attributes //
var priv = {};
priv.commandlist = {'saveDocument':saveDocument,
'loadDocument':loadDocument,
'removeDocument':removeDocument,
'getDocumentList':getDocumentList};
// creates the good command thanks to his label
if (spec.label && priv.commandlist[spec.label]) {
priv.label = spec.label;
delete spec.label;
return priv.commandlist[priv.label](spec, my);
}
priv.path = spec.path || '';
priv.tried = 0;
priv.option = spec.option || {};
priv.respond = priv.option.onResponse || function(){};
priv.done = priv.option.onDone || function(){};
priv.fail = priv.option.onFail || function(){};
priv.retry = function() {
that.setMaxRetry(-1);
that.fail({status:0,statusText:'Fail Retry',
message:'Impossible to retry.'});
};
priv.end = function() {};
// Methods //
......@@ -49,12 +66,21 @@ var command = function(spec, my) {
that.validateState();
};
that.getTried = function() {
return priv.tried;
};
that.setMaxRetry = function(max_retry) {
priv.option.max_retry = max_retry;
};
/**
* Delegate actual excecution the storage handler.
* @param {object} handler The storage handler.
*/
that.execute = function(handler) {
that.validate(handler);
priv.tried ++;
handler.execute(that);
};
......@@ -77,22 +103,31 @@ var command = function(spec, my) {
};
that.done = function(return_value) {
console.log ('test');
console.log ('done');
priv.done(return_value);
priv.respond({status:doneStatus(),value:return_value});
priv.end();
};
that.fail = function(return_error) {
priv.fail(return_error);
priv.respond({status:failStatus(),error:return_error});
priv.end();
if (priv.option.max_retry === 0 || priv.tried < priv.option.max_retry) {
priv.retry();
} else {
console.log ('fail');
priv.fail(return_error);
priv.respond({status:failStatus(),error:return_error});
priv.end();
}
};
that.onEndDo = function(fun) {
priv.end = fun;
};
that.onRetryDo = function(fun) {
priv.retry = fun;
};
/**
* Returns a serialized version of this command.
* Override this function.
......@@ -101,6 +136,8 @@ var command = function(spec, my) {
*/
that.serialized = function() {
return {label:that.getLabel(),
tried:priv.tried,
max_retry:priv.max_retry,
path:priv.path,
option:priv.option};
};
......
......@@ -4,7 +4,7 @@ var getDocumentList = function(spec, my) {
my = my || {};
// Attributes //
// Methods //
that.label = function() {
that.getLabel = function() {
return 'getDocumentList';
};
......
......@@ -4,7 +4,7 @@ var loadDocument = function(spec, my) {
my = my || {};
// Attributes //
// Methods //
that.label = function() {
that.getLabel = function() {
return 'loadDocument';
};
......
......@@ -4,7 +4,7 @@ var removeDocument = function(spec, my) {
my = my || {};
// Attributes //
// Methods //
that.label = function() {
that.getLabel = function() {
return 'removeDocument';
};
......
......@@ -5,7 +5,7 @@ var saveDocument = function(spec, my) {
// Attributes //
var content = spec.content;
// Methods //
that.label = function() {
that.getLabel = function() {
return 'saveDocument';
};
......
......@@ -75,7 +75,6 @@
option.onDone = option.onDone || function(){};
option.onFail = option.onFail || function(){};
option.max_retry = option.max_retry || 0;
console.log ('add job save: ' + JSON.stringify (priv.storage.serialized()));
jobManager.addJob(
job({storage:(specificstorage?
jioNamespace.storage(specificstorage):
......
......@@ -18,9 +18,9 @@ var jioNamespace = (function(spec, my) {
spec = spec || {};
var type = spec.type || 'base';
if (!storage_type_o[type]) {
throw invalidStorageType({type:type});
throw invalidStorageType({type:type,
message:'Storage does not exists.'});
}
console.log ('create storage: ' + JSON.stringify (spec) + JSON.stringify (my));
return storage_type_o[type](spec, my);
};
......@@ -40,7 +40,6 @@ var jioNamespace = (function(spec, my) {
storage = JSON.parse (storage);
}
storage = storage || {type:'base'};
console.log ('new jio: storage: ' + JSON.stringify (spec));
return jio(spec);
};
......@@ -56,7 +55,6 @@ var jioNamespace = (function(spec, my) {
throw invalidStorageType({type:type,message:'Already known.'});
}
storage_type_o[type] = constructor;
console.log ('adding: '+type);
};
return that;
......
......@@ -8,8 +8,6 @@ var job = function(spec, my) {
priv.command = spec.command;
priv.storage = spec.storage;
priv.status = initialStatus();
priv.tried = 0;
priv.max_retry = 0;
priv.date = new Date();
// Initialize //
......@@ -43,6 +41,10 @@ var job = function(spec, my) {
return priv.storage;
};
that.getDate = function() {
return priv.date;
};
/**
* Checks if the job is ready.
* @method isReady
......@@ -64,8 +66,6 @@ var job = function(spec, my) {
that.serialized = function() {
return {id:priv.id,
date:priv.date.getTime(),
tried:priv.tried,
max_retry:priv.max_retry,
status:priv.status.serialized(),
command:priv.command.serialized(),
storage:priv.storage.serialized()};
......@@ -122,7 +122,13 @@ var job = function(spec, my) {
* @param {object} job The other job.
*/
that.update = function(job) {
console.log ('updating');
priv.command.setMaxRetry(-1);
priv.command.fail({status:0,statusText:'Replaced',
message:'Job has been replaced by another one.'});
priv.date = job.getDate();
priv.command = job.getCommand();
priv.status = job.getStatus();
};
that.execute = function() {
......@@ -134,7 +140,14 @@ var job = function(spec, my) {
throw jobNotReadyException({message:'Can not execute this job.'});
}
priv.status = onGoingStatus();
priv.tried ++;
priv.command.onRetryDo (function() {
var ms = priv.command.getTried();
ms = ms*ms*200;
if (ms>10000){
ms = 10000;
}
that.waitForTime(ms);
});
priv.command.onEndDo (function() {
jobManager.terminateJob (that);
});
......
......@@ -53,6 +53,7 @@ var jobManager = (function(spec, my) {
var i;
if (priv.interval_id === null) {
priv.interval_id = setInterval (function() {
priv.restoreOldJio();
for (i = 0; i < priv.job_a.length; i+= 1) {
that.execute(priv.job_a[i]);
}
......@@ -74,6 +75,47 @@ var jobManager = (function(spec, my) {
}
};
priv.restoreOldJio = function() {
var i, jio_id_a;
priv.lastrestore = priv.lastrestore || 0;
if (priv.lastrestore > (Date.now()) - 2000) { return; }
jio_id_a = LocalOrCookieStorage.getItem('jio/id_array')||[];
for (i = 0; i < jio_id_a.length; i+= 1) {
priv.restoreOldJioId(jio_id_a[i]);
}
priv.lastrestore = Date.now();
};
priv.restoreOldJioId = function(id) {
var jio_date;
jio_date = LocalOrCookieStorage.getItem('jio/id/'+id)||0;
if (jio_date < Date.now() - 10000) {
priv.restoreOldJobFromJioId(id);
priv.removeOldJioId(id);
}
};
priv.restoreOldJobFromJioId = function(id) {
var i, jio_job_array;
jio_job_array = LocalOrCookieStorage.getItem('jio/job_array/'+id)||[];
for (i = 0; i < jio_job_array.length; i+= 1) {
that.addJob ( job(
{storage:jioNamespace.storage(jio_job_array[i]),
command:command(jio_job_array[i].command)}));
}
};
priv.removeOldJioId = function(id) {
var i, jio_id_a, new_a = [];
jio_id_a = LocalOrCookieStorage.getItem('jio/id_array')||[];
for (i = 0; i < jio_id_a.length; i+= 1) {
if (jio_id_a[i] !== id) {
new_a.push(jio_id_a[i]);
}
}
LocalOrCookieStorage.setItem('jio/id_array',new_a);
};
/**
* Executes a job.
* @method execute
......@@ -92,6 +134,18 @@ var jobManager = (function(spec, my) {
priv.copyJobArrayToLocal();
};
that.jobIdExists = function(id) {
var i;
for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i].getId() === id) {
console.log ('found');
return true;
}
}
console.log ('not found');
return false;
};
that.terminateJob = function(job) {
priv.removeJob(job);
priv.copyJobArrayToLocal();
......@@ -100,7 +154,6 @@ var jobManager = (function(spec, my) {
that.addJob = function(job) {
var result_a = that.validateJobAccordingToJobList (priv.job_a,job);
priv.manage (job,result_a);
priv.copyJobArrayToLocal();
};
that.validateJobAccordingToJobList = function(job_a,job) {
......@@ -121,16 +174,19 @@ var jobManager = (function(spec, my) {
return;
}
}
console.log ('managing '+JSON.stringify (result_a));
for (i = 0; i < result_a.length; i+= 1) {
switch (result_a[i].action) {
case 'eliminate':
console.log ('eliminating');
that.eliminate(result_a[i].job);
break;
case 'replace':
job.update(result_a[i].job);
case 'update':
result_a[i].job.update(job);
priv.copyJobArrayToLocal();
return;
case 'wait':
console.log ('wait');
job.waitForJob(result_a[i].job);
break;
default: break;
......@@ -145,10 +201,12 @@ var jobManager = (function(spec, my) {
for (i = 0; i < priv.job_a.length; i+= 1) {
if (priv.job_a[i].getId() !== job.getId()) {
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.copyJobArrayToLocal();
};
return that;
}());
\ No newline at end of file
}());
......@@ -12,8 +12,8 @@ var jobRules = (function(spec, my) {
};
priv.default_compare = function(job1,job2) {
return (job1.getCommand().getPath() === job2.getCommand().getPath() &&
JSON.stringify(job1.getStorage()) ===
JSON.stringify(job2.getStorage()));
JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized()));
};
priv.action = {
/*
......@@ -130,7 +130,7 @@ var jobRules = (function(spec, my) {
}
}
};
priv.default_action = 'none';
priv.default_action = that.none;
// Methods //
priv.getAction = function(job1,job2) {
var j1label, j2label, j1status;
......@@ -139,24 +139,36 @@ var jobRules = (function(spec, my) {
j1status = (job1.getStatus().getLabel()==='on going'?
'on going':'not on going');
try {
console.log (j1label);
console.log (j2label);
console.log (j1status);
return priv.action[j1label][j1status][j2label](job1,job2);
} catch (e) {
return priv.default_action;
if(e.name==='TypeError') {
return priv.default_action(job1,job2);
} else {
throw e;
}
}
};
priv.canCompare = function(job1,job2) {
var key = priv.stringifyJobForCompare(job1,job2);
if (priv.compare[key]) {
return priv.compare[key](job1,job2);
var job1label = job1.getCommand().getLabel(),
job2label = job2.getCommand().getLabel();
try {
return priv.compare[job1label][job2label](job1,job2);
} catch(e) {
if (e.name==='TypeError') {
return priv.default_compare(job1,job2);
} else {
throw e;
}
}
return priv.default_compare(job1,job2);
};
that.validateJobAccordingToJob = function(job1,job2) {
var key = priv.stringifyJobForAction(job1,job2);
if (priv.canCompare(job1,job2)) {
return {action:priv.getAction(job1,job2),job:job1};
}
return {action:priv.default_action,job:job1};
return {action:priv.default_action(job1,job2),job:job1};
};
return that;
......
......@@ -15,4 +15,4 @@ var failStatus = function(spec, my) {
return true;
};
return that;
};
\ No newline at end of file
};
......@@ -3,50 +3,63 @@ var waitStatus = function(spec, my) {
spec = spec || {};
my = my || {};
// Attributes //
var job_id_a = spec.job_id_array || [];
var threshold = 0;
var priv = {};
priv.job_id_a = spec.job_id_array || [];
priv.threshold = 0;
// Methods //
that.getLabel = function() {
return 'wait';
};
priv.refreshJobIdArray = function() {
var tmp_job_id_a = [], i;
for (i = 0; i < priv.job_id_a.length; i+= 1) {
if (jobManager.jobIdExists(priv.job_id_a[i])) {
tmp_job_id_a.push(priv.job_id_a[i]);
}
}
priv.job_id_a = tmp_job_id_a;
};
that.waitForJob = function(job) {
var i;
for (i = 0; i < job_id_a.length; i+= 1) {
if (job_id_a[i] === job.getId()) {
for (i = 0; i < priv.job_id_a.length; i+= 1) {
if (priv.job_id_a[i] === job.getId()) {
return;
}
}
job_id_a.push(job.getId());
priv.job_id_a.push(job.getId());
};
that.dontWaitForJob = function(job) {
var i, tmp_job_id_a = [];
for (i = 0; i < job_id_a.length; i+= 1) {
if (job_id_a[i] !== job.getId()){
tmp_job_id_a.push(job_id_a[i]);
for (i = 0; i < priv.job_id_a.length; i+= 1) {
if (priv.job_id_a[i] !== job.getId()){
tmp_job_id_a.push(priv.job_id_a[i]);
}
}
job_id_a = tmp_job_id_a;
priv.job_id_a = tmp_job_id_a;
};
that.waitForTime = function(ms) {
threshold = Date.now() + ms;
priv.threshold = Date.now() + ms;
};
that.stopWaitForTime = function() {
threshold = 0;
priv.threshold = 0;
};
that.canStart = function() {
return (job_id_a.length === 0 && Date.now() >= threshold);
priv.refreshJobIdArray();
console.log (priv.job_id_a);
return (priv.job_id_a.length === 0 && Date.now() >= priv.threshold);
};
that.canRestart = function() {
return false;
return that.canStart();
};
that.serialized = function() {
return {label:that.getLabel(),
waitfortime:threshold,
waitforjob:job_id_a};
waitfortime:priv.threshold,
waitforjob:priv.job_id_a};
};
return that;
......
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