npm install enqueue-server1. Shared lock
2. Exclusive Lock
3. Exclusive but not cumulative Lock
4. Optimistic Lock
``javascript
var lockClient = require('enqueue-client');
var lock = {"name":"product","argument":["Computer"],"mode":"E","owner":"B"};
lockClient.lock(lock, function(lockUUID,RC,OWNER){
if(RC === '0'){ //Lock is acquired
//Allow Editing
lockClient.unlock(lockUUID); //Unlock the object after finish editing
}else{
//Report message: "The product Computer is now locked by
}
});
`
!Enqueue Server is behind application server process
`bash`
$ npm install -g enqueue-server
2. Start the enqueue server:
CD to the install path of EnqueueServer
`bash`
$ node index.js
3. Test to connect:
Download the enqueue-client,
and execute following javascript in NodeJS.
`javascript `
var lockClient = require('enqueue-client');
lockClient.setEnqueueServerConnection('127.0.0.1', 3721);
var lock = {"name":"product","argument":["Computer"],"mode":"E","owner":"B"};
lockClient.lock(lock, function(lockUUID,RC,OWNER){
console.log('Lock is acquired with lock UUID: '+lockUUID);
});
Lock Types
4 lock types are supported.
An exclusive lock (E) set by another user on an object that already has a shared lock will be rejected.
Every extended exclusive lock (X) will also be rejected.
name: lock object name, usually you can use the database table name.
argument: the key of the object, it is an array,
as the object key may be combined with several fields.
For example, an object has the key combined with 3 fields.
The argument ['A', 'B', '@'] stands for the object with first 2 key fields equals 'A' and 'B',
the wildcard letter represented here by '@'.
mode: 4 lock types: S, E, X, O.
owner: the owner who holds the lock, it can be a login user ID.
waitTime: time to wait until the lock is acquired, default is 0.
timeout: the time to hold the lock, after which the elementary lock will be released automatically.
Default is 15 minutes.
`javascript `
var elementaryLock = {"name":"product","argument":["Computer"],"mode":"E","owner":"B"};
lockClient.lock(elementaryLock, function(lockUUID,RC,MSG){
//lockUUID is a unique id for the acquired lock, you must record it so that you can unlock it afterward;
//RC stands for the return code. 0:Success, 1:Fail, 3:Error in client, 4:Error in server;
//MSG returns the detail error message if RC is 3 or 4, and the lock owner if RC is 1;
});
`javascript`
lockClient.unlock(lockUUID, function(RC,MSG){
//RC: 0:Success, 4:Error in server;
//MSG returns the detail error message if RC is 4;
});
$3
Promote the optimistic lock with the specified lock UUID. The _callback_ receives the response from server.
`javascript`
lockClient.promote(lockUUID, function(RC,MSG){
//RC: 0:Success, 2:Fail, 3:Error in client, 4:Error in server;
//MSG returns the detail error message if RC is 3 or 4, and the existing lock owner if RC is 2;
});
$3
Get a list of existing locks in the enqueue server by lockName and lockOwner.
The callback receives an array of locks.
If you give both lockName and lockOwner, it filters with both.
If you only give either lockName or lockOwner, it filters with one of the given.
If you assign null for both, it return the complete lock list.
`javascript`
lockClient.getLocksBy(null, null, function(RC,locks){
//RC: 0:Success, 3:Error in client;
//locks is an array contains the locks if RC is 0 , and an error message if RC is 2;
});
$3
Set the remote enqueue server's host and port.
The method is optional if the enqueue server lies on the same server as the lock client.
As the default host and port are "127.0.0.1" and "3721".
Tests
You can find all the unit tests in the _test_ folder, and run them by:
`bash``
$ npm run test