Process locking made easy in VCF Automation Orchestrator, featuring automatic unlock
VCF Automation Orchestrator has a built-in locking semaphore provided by the LockingSystem class. When a lock is created, the workflow is placed into a waiting state, and any additional executions of the workflow will be placed in a queue until the lock is released.
Locking is a useful technique that can help protect the consistency of data or prevent multiple processes from updating a resource at the same time. This is especially important when workflows run concurrently and update the same resources.
LockingSystem provides the method ‘lock‘ that will attempt to acquire a lock for a given lockId and owner. If a lock is successfully acquired, the boolean true is returned, otherwise, the return value is false. Another function that could have been used is ‘LockAndWait‘; however, this will wait indefinitely if the lock cannot be acquired. Using the ‘lock‘ function provides more control and allows implementing a timeout feature or taking other corrective measures. Subsequently, a lock can be released using the ‘unlock‘ method.
I have created a LockingService that extends on LockingSystem to provide additional features:
- Re-attempt a failed lock (default 5 attempts) with a delay specified in seconds (default 60 seconds). This will allow the amount of time to wait for a lock to be configured independently for every use case;
- Option to automatically remove an existing lock once the max attempts have been reached. This can be useful if a previous workflow run has failed, leaving behind a stale lock.
You can download my LockingService module as a package here or as native JS here.
Using the LockingService
Using the LockingService in an Action or Workflow simply requires the following to import the module:
var locking = new (System.getModule("com.simplygeek.vcf.orchestrator.locking").LockingService());
A new instance of the LockingService class will be created and exposed by the variable ‘locking, which can be used to create and remove locks. Note that ‘locking‘ can be changed to any value you require.
Available Methods
To use LockingService to create and remove locks, use one of the following methods:
createLock
locking.createLock(lockOwner, lockId, retryMaxAttempts, retryDelay, autoRemoveLock) → {Boolean}
Parameters:
Name | Type | Description |
---|---|---|
lockOwner | String | The lock owner |
lockId | String | The unique (per lock) id |
retryMaxAttempts | Number | OPTIONAL – The maximum number of attempts to retry lock (default 5) |
retryDelay | Number | The delay between retry attempts (default 60 seconds) |
autoRemoveLock | Boolean | Automatically remove the lock if one is already present and the max retry attempts have been reached. A new lock is created (default true) |
Returns a boolean
removeLock
locking.removeLock(lockOwner, lockId)
Parameters:
Name | Type | Description |
---|---|---|
lockOwner | String | The lock owner |
lockId | String | The unique (per lock) id |
Example using default values
var locking = new (System.getModule("com.simplygeek.vcf.orchestrator.locking").LockingService()); locking.createLock("IPAM", subnetId); // Update operation locking.removeLock("IPAM", subnetId);
Example using custom values
var locking = new (System.getModule("com.simplygeek.vcf.orchestrator.locking").LockingService()); var retryMaxAttempts = 20; var retryDelay = 60; var autoRemoveLock = false; locking.createLock("IPAM", subnetId, retryMaxAttempts, retryDelay, autoRemoveLock); // Update operation locking.removeLock("IPAM", subnetId);
Thanks for reading, and please let me know if you have any suggestions for improving this service.