Commit 1f289bc7 authored by Tristan Cavelier's avatar Tristan Cavelier

Redesigning JIO in order to use 'success' and 'error' callbacks instead of...

Redesigning JIO in order to use 'success' and 'error' callbacks instead of 'onResponse', 'onDone' or 'onFail'.
parent 168cd8d5
This diff is collapsed.
This diff is collapsed.
...@@ -18,17 +18,30 @@ var command = function(spec, my) { ...@@ -18,17 +18,30 @@ var command = function(spec, my) {
priv.path = spec.path || ''; priv.path = spec.path || '';
priv.tried = 0; priv.tried = 0;
priv.option = spec.option || {}; priv.option = spec.option || {};
priv.respond = priv.option.onResponse || function(){}; priv.success = priv.option.success || function (){};
priv.done = priv.option.onDone || function(){}; priv.error = priv.option.error || function (){};
priv.fail = priv.option.onFail || function(){};
priv.retry = function() { priv.retry = function() {
that.setMaxRetry(-1); that.error({status:13,statusText:'Fail Retry',
that.fail({status:0,statusText:'Fail Retry', message:'Impossible to retry.'});
message:'Impossible to retry.'});
}; };
priv.end = function() {}; priv.end = function() {};
priv.on_going = false;
// Methods // // Methods //
/**
* Returns a serialized version of this command.
* Override this function.
* @method serialized
* @return {object} The serialized command.
*/
that.serialized = function() {
return {label:that.getLabel(),
tried:priv.tried,
max_retry:priv.max_retry,
path:priv.path,
option:that.cloneOption()};
};
/** /**
* Returns the label of the command. * Returns the label of the command.
* @method getLabel * @method getLabel
...@@ -66,12 +79,13 @@ var command = function(spec, my) { ...@@ -66,12 +79,13 @@ var command = function(spec, my) {
that.validateState(); that.validateState();
}; };
that.getTried = function() { that.canBeRetried = function () {
return priv.tried; return (priv.option.max_retry === 0 ||
priv.tried < priv.option.max_retry);
}; };
that.setMaxRetry = function(max_retry) { that.getTried = function() {
priv.option.max_retry = max_retry; return priv.tried;
}; };
/** /**
...@@ -79,9 +93,12 @@ var command = function(spec, my) { ...@@ -79,9 +93,12 @@ var command = function(spec, my) {
* @param {object} handler The storage handler. * @param {object} handler The storage handler.
*/ */
that.execute = function(handler) { that.execute = function(handler) {
that.validate(handler); if (!priv.on_going) {
priv.tried ++; that.validate(handler);
handler.execute(that); priv.tried ++;
priv.on_going = true;
handler.execute(that);
}
}; };
/** /**
...@@ -102,49 +119,44 @@ var command = function(spec, my) { ...@@ -102,49 +119,44 @@ var command = function(spec, my) {
} }
}; };
that.done = function(return_value) { that.success = function(return_value) {
log ('command done: ' + JSON.stringify (return_value)); priv.on_going = false;
priv.respond({status:doneStatus(),value:return_value}); priv.success (return_value);
priv.done(return_value);
priv.end(doneStatus()); priv.end(doneStatus());
}; };
that.fail = function(return_error) { that.retry = function (return_error) {
log ('command fail: ' + JSON.stringify (return_error)); priv.on_going = false;
if (priv.option.max_retry === 0 || priv.tried < priv.option.max_retry) { if (that.canBeRetried()) {
priv.retry(); priv.retry();
} else { } else {
priv.respond({status:failStatus(),error:return_error}); that.error (return_error);
priv.fail(return_error);
priv.end(failStatus());
} }
}; };
that.end = function () { that.error = function(return_error) {
priv.end(doneStatus()); priv.on_going = false;
priv.error(return_error);
priv.end(failStatus());
}; };
that.onResponseDo = function (fun) { that.end = function () {
if (fun) { priv.end(doneStatus());
priv.respond = fun;
} else {
return priv.respond;
}
}; };
that.onDoneDo = function (fun) { that.onSuccessDo = function (fun) {
if (fun) { if (fun) {
priv.done = fun; priv.success = fun;
} else { } else {
return priv.done; return priv.success;
} }
}; };
that.onFailDo = function (fun) { that.onErrorDo = function (fun) {
if (fun) { if (fun) {
priv.fail = fun; priv.error = fun;
} else { } else {
return priv.fail; return priv.error;
} }
}; };
...@@ -156,20 +168,6 @@ var command = function(spec, my) { ...@@ -156,20 +168,6 @@ var command = function(spec, my) {
priv.retry = fun; priv.retry = fun;
}; };
/**
* Returns a serialized version of this command.
* Override this function.
* @method serialized
* @return {object} The serialized command.
*/
that.serialized = function() {
return {label:that.getLabel(),
tried:priv.tried,
max_retry:priv.max_retry,
path:priv.path,
option:that.cloneOption()};
};
/** /**
* Is the command can be restored by another JIO : yes. * Is the command can be restored by another JIO : yes.
* @method canBeRestored * @method canBeRestored
...@@ -194,12 +192,10 @@ var command = function(spec, my) { ...@@ -194,12 +192,10 @@ var command = function(spec, my) {
* @return {object} The clone of the command options. * @return {object} The clone of the command options.
*/ */
that.cloneOption = function () { that.cloneOption = function () {
// log ('command cloneOption(): ' + JSON.stringify (priv.option));
var k, o = {}; var k, o = {};
for (k in priv.option) { for (k in priv.option) {
o[k] = priv.option[k]; o[k] = priv.option[k];
} }
// log ('cloneOption result: ' + JSON.stringify (o));
return o; return o;
}; };
......
...@@ -16,8 +16,8 @@ var getDocumentList = function(spec, my) { ...@@ -16,8 +16,8 @@ var getDocumentList = function(spec, my) {
return false; return false;
}; };
var super_done = that.done; var super_success = that.success;
that.done = function (res) { that.success = function (res) {
var i; var i;
if (res) { if (res) {
for (i = 0; i < res.length; i+= 1) { for (i = 0; i < res.length; i+= 1) {
...@@ -31,7 +31,7 @@ var getDocumentList = function(spec, my) { ...@@ -31,7 +31,7 @@ var getDocumentList = function(spec, my) {
} }
} }
} }
super_done(res); super_success(res);
}; };
return that; return that;
......
...@@ -16,8 +16,8 @@ var loadDocument = function(spec, my) { ...@@ -16,8 +16,8 @@ var loadDocument = function(spec, my) {
return false; return false;
}; };
var super_done = that.done; var super_success = that.success;
that.done = function (res) { that.success = function (res) {
if (res) { if (res) {
if (typeof res.last_modified !== 'number') { if (typeof res.last_modified !== 'number') {
res.last_modified=new Date(res.last_modified).getTime(); res.last_modified=new Date(res.last_modified).getTime();
...@@ -26,7 +26,7 @@ var loadDocument = function(spec, my) { ...@@ -26,7 +26,7 @@ var loadDocument = function(spec, my) {
res.creation_date=new Date(res.creation_date).getTime(); res.creation_date=new Date(res.creation_date).getTime();
} }
} }
super_done(res); super_success(res);
}; };
return that; return that;
}; };
var jio = (function () { var jio = (function () {
var log = function(){};
// var log = console.log;
...@@ -84,18 +84,16 @@ ...@@ -84,18 +84,16 @@
* @param {string} path The document path name. * @param {string} path The document path name.
* @param {string} content The document's content. * @param {string} content The document's content.
* @param {object} option (optional) Contains some options: * @param {object} option (optional) Contains some options:
* - {function} onResponse The callback called when the job is terminated. * - {function} success The callback called when the job has passed.
* - {function} onDone The callback called when the job has passed. * - {function} error The callback called when the job has fail.
* - {function} onFail The callback called when the job has fail.
* - {number} max_retry The number max of retries, 0 = infinity. * - {number} max_retry The number max of retries, 0 = infinity.
* @param {object} specificstorage (optional) A specific storage, only if * @param {object} specificstorage (optional) A specific storage, only if
* you want to save this document elsewhere. * you want to save this document elsewhere.
*/ */
that.saveDocument = function(path, content, option, specificstorage) { that.saveDocument = function(path, content, option, specificstorage) {
option = option || {}; option = option || {};
option.onResponse = option.onResponse || function(){}; option.success = option.success || function(){};
option.onDone = option.onDone || function(){}; option.error = option.error || function(){};
option.onFail = option.onFail || function(){};
option.max_retry = option.max_retry || 0; option.max_retry = option.max_retry || 0;
jobManager.addJob( jobManager.addJob(
job({storage:(specificstorage? job({storage:(specificstorage?
...@@ -110,9 +108,8 @@ ...@@ -110,9 +108,8 @@
* @method loadDocument * @method loadDocument
* @param {string} path The document path name. * @param {string} path The document path name.
* @param {object} option (optional) Contains some options: * @param {object} option (optional) Contains some options:
* - {function} onResponse The callback called when the job is terminated. * - {function} success The callback called when the job has passed.
* - {function} onDone The callback called when the job has passed. * - {function} error The callback called when the job has fail.
* - {function} onFail The callback called when the job has fail.
* - {number} max_retry The number max of retries, 0 = infinity. * - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} metadata_only Load only document metadata. * - {boolean} metadata_only Load only document metadata.
* @param {object} specificstorage (optional) A specific storage, only if * @param {object} specificstorage (optional) A specific storage, only if
...@@ -120,9 +117,8 @@ ...@@ -120,9 +117,8 @@
*/ */
that.loadDocument = function(path, option, specificstorage) { that.loadDocument = function(path, option, specificstorage) {
option = option || {}; option = option || {};
option.onResponse = option.onResponse || function(){}; option.success = option.success || function(){};
option.onDone = option.onDone || function(){}; option.error = option.error || function(){};
option.onFail = option.onFail || function(){};
option.max_retry = option.max_retry || 0; option.max_retry = option.max_retry || 0;
option.metadata_only = (option.metadata_only !== undefined? option.metadata_only = (option.metadata_only !== undefined?
option.metadata_only:false); option.metadata_only:false);
...@@ -139,18 +135,16 @@ ...@@ -139,18 +135,16 @@
* @method removeDocument * @method removeDocument
* @param {string} path The document path name. * @param {string} path The document path name.
* @param {object} option (optional) Contains some options: * @param {object} option (optional) Contains some options:
* - {function} onResponse The callback called when the job is terminated. * - {function} success The callback called when the job has passed.
* - {function} onDone The callback called when the job has passed. * - {function} error The callback called when the job has fail.
* - {function} onFail The callback called when the job has fail.
* - {number} max_retry The number max of retries, 0 = infinity. * - {number} max_retry The number max of retries, 0 = infinity.
* @param {object} specificstorage (optional) A specific storage, only if * @param {object} specificstorage (optional) A specific storage, only if
* you want to save this document elsewhere. * you want to save this document elsewhere.
*/ */
that.removeDocument = function(path, option, specificstorage) { that.removeDocument = function(path, option, specificstorage) {
option = option || {}; option = option || {};
option.onResponse = option.onResponse || function(){}; option.success = option.success || function(){};
option.onDone = option.onDone || function(){}; option.error = option.error || function(){};
option.onFail = option.onFail || function(){};
option.max_retry = option.max_retry || 0; option.max_retry = option.max_retry || 0;
jobManager.addJob( jobManager.addJob(
job({storage:(specificstorage? job({storage:(specificstorage?
...@@ -165,9 +159,8 @@ ...@@ -165,9 +159,8 @@
* @method getDocumentList * @method getDocumentList
* @param {string} path The folder path. * @param {string} path The folder path.
* @param {object} option (optional) Contains some options: * @param {object} option (optional) Contains some options:
* - {function} onResponse The callback called when the job is terminated. * - {function} success The callback called when the job has passed.
* - {function} onDone The callback called when the job has passed. * - {function} error The callback called when the job has fail.
* - {function} onFail The callback called when the job has fail.
* - {number} max_retry The number max of retries, 0 = infinity. * - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} metadata_only Load only document metadata * - {boolean} metadata_only Load only document metadata
* @param {object} specificstorage (optional) A specific storage, only if * @param {object} specificstorage (optional) A specific storage, only if
...@@ -175,9 +168,8 @@ ...@@ -175,9 +168,8 @@
*/ */
that.getDocumentList = function(path, option, specificstorage) { that.getDocumentList = function(path, option, specificstorage) {
option = option || {}; option = option || {};
option.onResponse = option.onResponse || function(){}; option.success = option.success || function(){};
option.onDone = option.onDone || function(){}; option.error = option.error || function(){};
option.onFail = option.onFail || function(){};
option.max_retry = option.max_retry || 0; option.max_retry = option.max_retry || 0;
option.metadata_only = (option.metadata_only !== undefined? option.metadata_only = (option.metadata_only !== undefined?
option.metadata_only:true); option.metadata_only:true);
......
...@@ -9,8 +9,6 @@ var job = function(spec, my) { ...@@ -9,8 +9,6 @@ var job = function(spec, my) {
priv.storage = spec.storage; priv.storage = spec.storage;
priv.status = initialStatus(); priv.status = initialStatus();
priv.date = new Date(); priv.date = new Date();
log ('new job spec: ' + JSON.stringify (spec) + ', priv: ' +
JSON.stringify (priv));
// Initialize // // Initialize //
if (!priv.storage){ if (!priv.storage){
...@@ -51,7 +49,7 @@ var job = function(spec, my) { ...@@ -51,7 +49,7 @@ var job = function(spec, my) {
* @return {boolean} true if ready, else false. * @return {boolean} true if ready, else false.
*/ */
that.isReady = function() { that.isReady = function() {
if (priv.tried === 0) { if (that.getCommand().getTried() === 0) {
return priv.status.canStart(); return priv.status.canStart();
} else { } else {
return priv.status.canRestart(); return priv.status.canRestart();
...@@ -77,7 +75,6 @@ var job = function(spec, my) { ...@@ -77,7 +75,6 @@ var job = function(spec, my) {
* @param {object} job The job to wait for. * @param {object} job The job to wait for.
*/ */
that.waitForJob = function(job) { that.waitForJob = function(job) {
log ('job waitForJob(job): ' + JSON.stringify (job.serialized()));
if (priv.status.getLabel() !== 'wait') { if (priv.status.getLabel() !== 'wait') {
priv.status = waitStatus({},my); priv.status = waitStatus({},my);
} }
...@@ -101,7 +98,6 @@ var job = function(spec, my) { ...@@ -101,7 +98,6 @@ var job = function(spec, my) {
* @param {number} ms Time to wait in millisecond. * @param {number} ms Time to wait in millisecond.
*/ */
that.waitForTime = function(ms) { that.waitForTime = function(ms) {
log ('job waitForTime(ms): ' + ms);
if (priv.status.getLabel() !== 'wait') { if (priv.status.getLabel() !== 'wait') {
priv.status = waitStatus({},my); priv.status = waitStatus({},my);
} }
...@@ -119,21 +115,18 @@ var job = function(spec, my) { ...@@ -119,21 +115,18 @@ var job = function(spec, my) {
}; };
that.eliminated = function () { that.eliminated = function () {
priv.command.setMaxRetry(-1); priv.command.error ({
log ('job eliminated(): '+JSON.stringify (that.serialized())); status:10,statusText:'Stoped',
priv.command.fail({status:0,statusText:'Stoped', message:'This job has been stoped by another one.'});
message:'This job has been stoped by another one.'});
}; };
that.notAccepted = function () { that.notAccepted = function () {
log ('job notAccepted(): '+JSON.stringify (that.serialized()));
priv.command.setMaxRetry(-1);
priv.command.onEndDo (function () { priv.command.onEndDo (function () {
priv.status = failStatus(); priv.status = failStatus();
my.jobManager.terminateJob (that); my.jobManager.terminateJob (that);
}); });
priv.command.fail ({status:0,statusText:'Not Accepted', priv.command.error ({status:11,statusText:'Not Accepted',
message:'This job is already running.'}); message:'This job is already running.'});
}; };
/** /**
...@@ -142,30 +135,24 @@ var job = function(spec, my) { ...@@ -142,30 +135,24 @@ 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) {
log ('job update(job): ' + JSON.stringify (job.serialized())); priv.command.error ({status:12,statusText:'Replaced',
priv.command.setMaxRetry(-1); message:'Job has been replaced by another one.'});
priv.command.onEndDo(function (status) {
log ('job update on end' + status.getLabel());
});
priv.command.fail({status:0,statusText:'Replaced',
message:'Job has been replaced by another one.'});
priv.date = job.getDate(); priv.date = job.getDate();
priv.command = job.getCommand(); priv.command = job.getCommand();
priv.status = job.getStatus(); priv.status = job.getStatus();
}; };
that.execute = function() { that.execute = function() {
log ('job execute(): ' + JSON.stringify (that.serialized())); if (!that.getCommand().canBeRetried()) {
if (priv.max_retry !== 0 && priv.tried >= priv.max_retry) {
throw tooMuchTriesJobException( throw tooMuchTriesJobException(
{job:that,message:'The job was invoked too much time.'}); {job:that,message:'The job was invoked too much time.'});
} }
if (!that.isReady()) { if (!that.isReady()) {
throw jobNotReadyException({message:'Can not execute this job.'}); throw jobNotReadyException(
{job:that,message:'Can not execute this job.'});
} }
priv.status = onGoingStatus(); priv.status = onGoingStatus();
priv.command.onRetryDo (function() { priv.command.onRetryDo (function() {
log ('command.retry job:' + JSON.stringify (that.serialized()));
var ms = priv.command.getTried(); var ms = priv.command.getTried();
ms = ms*ms*200; ms = ms*ms*200;
if (ms>10000){ if (ms>10000){
...@@ -175,7 +162,6 @@ var job = function(spec, my) { ...@@ -175,7 +162,6 @@ var job = function(spec, my) {
}); });
priv.command.onEndDo (function(status) { priv.command.onEndDo (function(status) {
priv.status = status; priv.status = status;
log ('command.end job:' + JSON.stringify (that.serialized()));
my.jobManager.terminateJob (that); my.jobManager.terminateJob (that);
}); });
priv.command.execute (priv.storage); priv.command.execute (priv.storage);
......
...@@ -124,7 +124,7 @@ var jobManager = (function(spec, my) { ...@@ -124,7 +124,7 @@ var jobManager = (function(spec, my) {
priv.restoreOldJioId = function(id) { priv.restoreOldJioId = function(id) {
var jio_date; var jio_date;
jio_date = LocalOrCookieStorage.getItem('jio/id/'+id)||0; jio_date = LocalOrCookieStorage.getItem('jio/id/'+id)||0;
if (jio_date < Date.now() - 10000) { if (jio_date < (Date.now() - 10000)) { // 10 sec
priv.restoreOldJobFromJioId(id); priv.restoreOldJobFromJioId(id);
priv.removeOldJioId(id); priv.removeOldJioId(id);
priv.removeJobArrayFromJioId(id); priv.removeJobArrayFromJioId(id);
......
...@@ -12,14 +12,6 @@ var jobRules = (function(spec, my) { ...@@ -12,14 +12,6 @@ var jobRules = (function(spec, my) {
that.none = function() { return 'none'; }; that.none = function() { return 'none'; };
that.default_action = that.none; that.default_action = that.none;
that.default_compare = function(job1,job2) { that.default_compare = function(job1,job2) {
if (job1.getCommand().getPath() === job2.getCommand().getPath() &&
JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized())) {
log ('same ! ' + job1.getCommand().getPath() + ', ' +
job2.getCommand().getPath() + ', ' +
JSON.stringify (job1.getStorage().serialized())+', '+
JSON.stringify (job2.getStorage().serialized()));
}
return (job1.getCommand().getPath() === job2.getCommand().getPath() && return (job1.getCommand().getPath() === job2.getCommand().getPath() &&
JSON.stringify(job1.getStorage().serialized()) === JSON.stringify(job1.getStorage().serialized()) ===
JSON.stringify(job2.getStorage().serialized())); JSON.stringify(job2.getStorage().serialized()));
......
...@@ -5,7 +5,6 @@ var storage = function(spec, my) { ...@@ -5,7 +5,6 @@ var storage = function(spec, my) {
// Attributes // // Attributes //
var priv = {}; var priv = {};
priv.type = spec.type || ''; priv.type = spec.type || '';
log ('new storage spec: ' + JSON.stringify (spec));
// Methods // // Methods //
that.getType = function() { that.getType = function() {
...@@ -21,12 +20,11 @@ var storage = function(spec, my) { ...@@ -21,12 +20,11 @@ var storage = function(spec, my) {
* @param {object} command The command * @param {object} command The command
*/ */
that.execute = function(command) { that.execute = function(command) {
log ('storage '+that.getType()+' execute(command): ' +
JSON.stringify (command.serialized()));
that.validate(command); that.validate(command);
that.done = command.done; that.success = command.success;
that.fail = command.fail; that.error = command.error;
that.end = command.end; that.retry = command.retry;
that.end = command.end;
command.executeOn(that); command.executeOn(that);
}; };
...@@ -78,9 +76,10 @@ var storage = function(spec, my) { ...@@ -78,9 +76,10 @@ var storage = function(spec, my) {
return ''; return '';
}; };
that.done = function() {}; that.success = function() {};
that.fail = function() {}; that.retry = function() {};
that.end = function() {}; // terminate the current job. that.error = function() {};
that.end = function() {}; // terminate the current job.
return that; return that;
}; };
...@@ -2,7 +2,6 @@ var storageHandler = function(spec, my) { ...@@ -2,7 +2,6 @@ var storageHandler = function(spec, my) {
spec = spec || {}; spec = spec || {};
my = my || {}; my = my || {};
var that = storage( spec, my ); var that = storage( spec, my );
log ('new storageHandler spec: '+JSON.stringify (spec));
that.newCommand = function (method, spec) { that.newCommand = function (method, spec) {
var o = spec || {}; var o = spec || {};
...@@ -16,10 +15,6 @@ var storageHandler = function(spec, my) { ...@@ -16,10 +15,6 @@ var storageHandler = function(spec, my) {
}; };
that.addJob = function (storage,command) { that.addJob = function (storage,command) {
log ('storageHandler ' + that.getType() +
' addJob (storage, command): ' +
JSON.stringify (storage.serialized()) + ', ' +
JSON.stringify (command.serialized()));
my.jobManager.addJob ( job({storage:storage, command:command}, my) ); my.jobManager.addJob ( job({storage:storage, command:command}, my) );
}; };
......
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