[Persistence] Requeue on overwrite

Requeue (instead of trying to access persistence again) on overwrite
WTD-1033.
This commit is contained in:
Victor Woeltjen 2015-03-24 14:09:51 -07:00
parent 2709fde9a3
commit 6b43256afd
3 changed files with 32 additions and 23 deletions

View File

@ -16,14 +16,11 @@ define(
// Issue a new persist call for the domain object associated with // Issue a new persist call for the domain object associated with
// this failure. // this failure.
function persist(failure) { function persist(failure) {
var decoratedPersistence = // Note that we reissue the persist request here, but don't
failure.domainObject.getCapability('persistence'); // return it, to avoid a circular wait. We trust that the
// Note that we issue the persist request here, but don't // PersistenceQueue will behave correctly on the next round
// return it. We trust that the PersistenceQueue will // of flushing.
// behave correctly on the next round of flushing. failure.requeue();
if (decoratedPersistence) {
decoratedPersistence.persist();
}
} }
// Retry persistence (overwrite) for this set of failed attempts // Retry persistence (overwrite) for this set of failed attempts

View File

@ -17,14 +17,21 @@ define(
function PersistenceQueueHandler($q, failureHandler) { function PersistenceQueueHandler($q, failureHandler) {
// Handle a group of persistence invocations // Handle a group of persistence invocations
function persistGroup(ids, queue, domainObjects) { function persistGroup(ids, persistences, domainObjects, queue) {
var failures = []; var failures = [];
// Try to persist a specific domain object // Try to persist a specific domain object
function tryPersist(id) { function tryPersist(id) {
// Look up its persistence capability from the provided // Look up its persistence capability from the provided
// id->persistence object. // id->persistence object.
var persistence = queue[id]; var persistence = persistences[id],
domainObject = domainObjects[id];
// Put a domain object back in the queue
// (e.g. after Overwrite)
function requeue() {
return queue.put(domainObject, persistence);
}
// Handle success // Handle success
function succeed(value) { function succeed(value) {
@ -36,7 +43,8 @@ define(
failures.push({ failures.push({
id: id, id: id,
persistence: persistence, persistence: persistence,
domainObject: domainObjects[id], domainObject: domainObject,
requeue: requeue,
error: error error: error
}); });
return false; return false;
@ -63,14 +71,16 @@ define(
/** /**
* Invoke the persist method on the provided persistence * Invoke the persist method on the provided persistence
* capabilities. * capabilities.
* @param {Object.<string,PersistenceCapability>} queue * @param {Object.<string,PersistenceCapability>} persistences
* capabilities to invoke, in id->capability pairs. * capabilities to invoke, in id->capability pairs.
* @param {Object.<string,DomainObject>} domainObjects * @param {Object.<string,DomainObject>} domainObjects
* associated domain objects, in id->object pairs. * associated domain objects, in id->object pairs.
* @param {PersistenceQueue} queue the persistence queue,
* to requeue as necessary
*/ */
persist: function (queue, domainObjects) { persist: function (persistences, domainObjects, queue) {
var ids = Object.keys(queue); var ids = Object.keys(persistences);
return persistGroup(ids, queue, domainObjects); return persistGroup(ids, persistences, domainObjects, queue);
} }
}; };
} }

View File

@ -22,7 +22,8 @@ define(
* attempts to flush the queue * attempts to flush the queue
*/ */
function PersistenceQueueImpl($q, $timeout, handler, DELAY) { function PersistenceQueueImpl($q, $timeout, handler, DELAY) {
var queue = {}, var self,
persistences = {},
objects = {}, objects = {},
lastObservedSize = 0, lastObservedSize = 0,
pendingTimeout, pendingTimeout,
@ -31,10 +32,9 @@ define(
// Check if the queue's size has stopped increasing) // Check if the queue's size has stopped increasing)
function quiescent() { function quiescent() {
return Object.keys(queue).length === lastObservedSize; return Object.keys(persistences).length === lastObservedSize;
} }
// Persist all queued objects // Persist all queued objects
function flush() { function flush() {
// Get a local reference to the active promise; // Get a local reference to the active promise;
@ -51,11 +51,11 @@ define(
} }
// Persist all queued objects // Persist all queued objects
flushPromise = handler.persist(queue, objects) flushPromise = handler.persist(persistences, objects, self)
.then(clearFlushPromise, clearFlushPromise); .then(clearFlushPromise, clearFlushPromise);
// Reset queue, etc. // Reset queue, etc.
queue = {}; persistences = {};
objects = {}; objects = {};
lastObservedSize = 0; lastObservedSize = 0;
pendingTimeout = undefined; pendingTimeout = undefined;
@ -71,7 +71,7 @@ define(
// Only flush when we've stopped receiving updates // Only flush when we've stopped receiving updates
(quiescent() ? flush : scheduleFlush)(); (quiescent() ? flush : scheduleFlush)();
// Update lastObservedSize to detect quiescence // Update lastObservedSize to detect quiescence
lastObservedSize = Object.keys(queue).length; lastObservedSize = Object.keys(persistences).length;
} }
// If we are already flushing the queue... // If we are already flushing the queue...
@ -91,7 +91,7 @@ define(
// If no delay is provided, use a default // If no delay is provided, use a default
DELAY = DELAY || 0; DELAY = DELAY || 0;
return { self = {
/** /**
* Queue persistence of a domain object. * Queue persistence of a domain object.
* @param {DomainObject} domainObject the domain object * @param {DomainObject} domainObject the domain object
@ -100,11 +100,13 @@ define(
*/ */
put: function (domainObject, persistence) { put: function (domainObject, persistence) {
var id = domainObject.getId(); var id = domainObject.getId();
queue[id] = persistence; persistences[id] = persistence;
objects[id] = domainObject; objects[id] = domainObject;
return scheduleFlush(); return scheduleFlush();
} }
}; };
return self;
} }
return PersistenceQueueImpl; return PersistenceQueueImpl;