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

Change JobWorkspace to JobQueue

parent c97b0cc1
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global deepClone */ /*global deepClone, dictFilter, uniqueJSONStringify */
/** /**
* Tool to manipulate a list of object containing at least one property: 'id'. * Tool to manipulate a list of object containing at least one property: 'id'.
...@@ -7,12 +7,72 @@ ...@@ -7,12 +7,72 @@
* *
* @class JobQueue * @class JobQueue
* @constructor * @constructor
* @param {Array} An array of object * @param {Workspace} workspace The workspace where to store
* @param {String} namespace The namespace to use in the workspace
* @param {Array} job_keys An array of job keys to store
* @param {Array} [array] An array of object
*/ */
function JobQueue(array) { function JobQueue(workspace, namespace, job_keys, array) {
this._array = array; this._workspace = workspace;
this._namespace = namespace;
this._job_keys = job_keys;
if (Array.isArray(array)) {
this._array = array;
} else {
this._array = [];
}
} }
/**
* Store the job queue into the workspace.
*
* @method save
*/
JobQueue.prototype.save = function () {
var i, job_queue = deepClone(this._array);
for (i = 0; i < job_queue.length; i += 1) {
dictFilter(job_queue[i], this._job_keys);
}
if (this._array.length === 0) {
this._workspace.removeItem(this._namespace);
} else {
this._workspace.setItem(
this._namespace,
uniqueJSONStringify(job_queue)
);
}
return this;
};
/**
* Loads the job queue from the workspace.
*
* @method load
*/
JobQueue.prototype.load = function () {
var job_list;
try {
job_list = JSON.parse(this._workspace.getItem(this._namespace));
} catch (ignore) {}
if (!Array.isArray(job_list)) {
job_list = [];
}
this.clear();
new JobQueue(job_list).repair();
this.update(job_list);
return this;
};
/**
* Returns the array version of the job queue
*
* @method asArray
* @return {Array} The job queue as array
*/
JobQueue.prototype.asArray = function () {
return this._array;
};
/** /**
* Removes elements which are not objects containing at least 'id' property. * Removes elements which are not objects containing at least 'id' property.
* *
......
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global JobQueue, uniqueJSONStringify, deepClone, dictFilter */
/**
* Manipulates a job queue and is also able to store it in a specific place.
*
* @class JobWorkspace
* @constructor
* @param {Workspace} workspace The workspace where to store
* @param {JobQueue} job_queue The job queue to use
* @param {String} namespace The namespace to use in the workspace
* @param {Array} job_keys An array of job keys to store
*/
function JobWorkspace(workspace, job_queue, namespace, job_keys) {
this._workspace = workspace;
this._job_queue = job_queue;
this._namespace = namespace;
this._job_keys = job_keys;
}
/**
* Store the job queue into the workspace.
*
* @method save
*/
JobWorkspace.prototype.save = function () {
var i, job_queue = deepClone(this._job_queue._array);
for (i = 0; i < job_queue.length; i += 1) {
dictFilter(job_queue[i], this._job_keys);
}
if (this._job_queue._array.length === 0) {
this._workspace.removeItem(this._namespace);
} else {
this._workspace.setItem(
this._namespace,
uniqueJSONStringify(job_queue)
);
}
return this;
};
/**
* Loads the job queue from the workspace.
*
* @method load
*/
JobWorkspace.prototype.load = function () {
var job_list;
try {
job_list = JSON.parse(this._workspace.getItem(this._namespace));
} catch (ignore) {}
if (!Array.isArray(job_list)) {
job_list = [];
}
this._job_queue.clear();
new JobQueue(job_list).repair();
this._job_queue.update(job_list);
return this;
};
/**
* Returns the array version of the job queue
*
* @method asArray
* @return {Array} The job queue as array
*/
JobWorkspace.prototype.asArray = function () {
return this._job_queue._array;
};
/**
* Post a job in the job queue
*
* @method post
* @param {Object} job The job object
* @return {Number} The generated id
*/
JobWorkspace.prototype.post = function (job) {
return this._job_queue.post(job);
};
/**
* Put a job to the job queue
*
* @method put
* @param {Object} job The job object with an id
*/
JobWorkspace.prototype.put = function (job) {
return this._job_queue.put(job);
};
/**
* Get a job from an id. Returns undefined if not found
*
* @method get
* @param {Number} id The job id
* @return {Object} The job or undefined
*/
JobWorkspace.prototype.get = function (id) {
return this._job_queue.get(id);
};
/**
* Removes a job from an id
*
* @method remove
* @param {Number} id The job id
*/
JobWorkspace.prototype.remove = function (id) {
return this._job_queue.remove(id);
};
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, unparam: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, unparam: true */
/*global arrayExtend, localStorage, Workspace, uniqueJSONStringify, JobQueue, /*global arrayExtend, localStorage, Workspace, uniqueJSONStringify, JobQueue,
JobWorkspace, constants */ constants */
function enableJobQueue(jio, shared, options) { function enableJobQueue(jio, shared, options) {
...@@ -14,7 +14,7 @@ function enableJobQueue(jio, shared, options) { ...@@ -14,7 +14,7 @@ function enableJobQueue(jio, shared, options) {
// creates // creates
// - shared.storage_spec_str String // - shared.storage_spec_str String
// - shared.workspace Workspace // - shared.workspace Workspace
// - shared.job_queue JobWorkspace // - shared.job_queue JobQueue
// uses 'job', 'jobRun', 'jobStop', 'jobEnd' events // uses 'job', 'jobRun', 'jobStop', 'jobEnd' events
// emits 'jobEnd' events // emits 'jobEnd' events
...@@ -33,9 +33,8 @@ function enableJobQueue(jio, shared, options) { ...@@ -33,9 +33,8 @@ function enableJobQueue(jio, shared, options) {
shared.storage_spec_str = uniqueJSONStringify(shared.storage_spec); shared.storage_spec_str = uniqueJSONStringify(shared.storage_spec);
} }
shared.job_queue = new JobWorkspace( shared.job_queue = new JobQueue(
shared.workspace, shared.workspace,
new JobQueue([]),
'jio/jobs/' + shared.storage_spec_str, 'jio/jobs/' + shared.storage_spec_str,
shared.job_keys shared.job_keys
); );
......
...@@ -7,7 +7,7 @@ function enableJobRecovery(jio, shared, options) { ...@@ -7,7 +7,7 @@ function enableJobRecovery(jio, shared, options) {
// - JobQueue enabled and before this // - JobQueue enabled and before this
// uses // uses
// - shared.job_queue JobWorkspace // - shared.job_queue JobQueue
function numberOrDefault(number, default_value) { function numberOrDefault(number, default_value) {
return (typeof number === 'number' && return (typeof number === 'number' &&
......
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