'Alfresco Share Javascript API - check for race condition on document node
This should be a simple answer, and I would have thought quite a common issue although I've not found anything myself, hence the question!
Using Alfresco Share-side Javascript (in a script executed by a folder rule) I want to update a shared document by checking it out, updating and then checking in again. The whole process only takes milliseconds but it is in theory possible that this script might be executed by more than one user at the same time. However vanishingly small that possibility I still want to eliminate it. So I want to check to see if the document is locked, and if it is wait until it is free again. Basic transactional behaviour.
Using the Alfresco Javascript Console I've created this code that demonstrates what I am trying to do:
var n1 = search.findNode(document.nodeRef);
var l1 = n1.isLocked;
while(l1 == true){
print("locked");
java.lang.Thread.sleep(1000);
var n2 = search.findNode(n1.nodeRef);
var l1 = n2.isLocked;
}
print("unlocked");
(Don't worry I don't loop indefinitely this is just an example!).
My problem is that if the isLocked property is true once the script starts executing then it never seems to become false. I've tried various ways of writing the JS code (including just checking document.isLocked every iteration) but it seems that the script's process never seems to register that the lock has disappeared. If I unlock the document half way through execution then the script just carries on waiting. If I run the script again it sees that the file is now unlocked.
My question is, how do I resolve this, and is it even possible? I know that Alfresco JS is Rhino Javascript, so might the problem be something to do with the underlying Java process?
I can of course simply fail the script and stop the action by throwing an exception if the document is locked (or at least I hope I can - I've not tried that yet!), but that doesn't exactly give a great user experience.
Any help or gleeful pointing out a schoolboy error appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
