|
var mongoose = require("mongoose"); |
|
|
|
var models = require("./documentModel"); |
|
|
|
|
|
|
|
/* |
|
This method uses the nonce field in the document to protect against concurrent edits on the post document |
|
http://docs.mongodb.org/ecosystem/use-cases/metadata-and-asset-management/#create-and-edit-content-nodes |
|
transformFunction will be given the post doc and should callback the data to update: |
|
transformFunction(doc, callback) |
|
*/ |
|
var savePostByIdWithConcurrencyProtection = function(postId, transformFunction, retries, callback){ |
|
FindPostById(postId, function(err, doc){ |
|
if(err || !doc){ |
|
callback(err || "Unable to find post in savePostByIdWithConcurrencyProtection"); |
|
return; |
|
} |
|
transformFunction(doc, function(err, data){ |
|
if(err){ |
|
callback(err); |
|
return; |
|
} |
|
data.nonce = new mongoose.Types.ObjectId; |
|
//setTimeout(function(){ //setTimeout for testing concurrent edits |
|
models.PostModel.update({"_id":postId, "nonce":doc.nonce}, data, function(err, numberAffected, raw){ |
|
if(err){ |
|
callback(err); |
|
return; |
|
} |
|
//console.log("savePostByIdWithConcurrencyProtection: ", numberAffected, raw); |
|
if(!numberAffected && retries < 10){ |
|
//we weren't able to update the doc because someone else modified it first, retry |
|
console.log("Unable to savePostByIdWithConcurrencyProtection, retrying ", retries); |
|
//retry with a little delay |
|
setTimeout(function(){ |
|
savePostByIdWithConcurrencyProtection(postId, transformFunction, (retries + 1), callback); |
|
}, 20); |
|
} else if(retries >= 10){ |
|
//there is probably something wrong, just return an error |
|
callback("Couldn't update document after 10 retries in savePostByIdWithConcurrencyProtection"); |
|
} else { |
|
FindPostById(postId, callback); |
|
} |
|
}); |
|
//}, 5000); |
|
}); |
|
}); |
|
|
|
}; |
|
|
|
|
|
var FindPostById = function(id, callback){ |
|
//search for an existing post |
|
models.PostModel.findOne({"_id":id}, function(err, doc){ |
|
if(err){ |
|
console.error("FindPostById:", err); |
|
} else if(!doc){ |
|
var msg = "Could Not FindPostById:" + id; |
|
console.error(msg); |
|
return callback(msg); |
|
} |
|
callback(err, doc); |
|
}); |
|
}; |
|
|