[SERVER ONLY]
fcf.NLock.tryLockFile(string a_filePath, function a_cb)
[SERVER ONLY]
fcf.NLock.tryLockFile(number a_fileHandle, function a_cb)
[SERVER ONLY]
fcf.NLock.tryLockFile(string a_filePath, boolean a_quiet, function a_cb)
[SERVER ONLY]
fcf.NLock.tryLockFile(number a_fileHandle, boolean a_quiet, function a_cb)
Package: fcf-framework-lock
File: fcf-framework-lock:index.js
Available from version: 1.0.0
Performs a lock on the file, if the lock is already on the file, then the function fails or returns the lock identifier to undefined, depending on the a_quiet argument
Arguments
string a_filePath
- The path to the file
number a_fileHandle
- The open file handle returned by the FS.open() function.
boolean a_quiet = false
- (Since 1.0.3) If true, then if a lock is held on a file that is already locked, the function exits without error, and the value of the a_lock argument in the callback function is undefined. If the value is not set or is false, then if the file is already locked, the function will fail with an error object containing an unavailable field equal to true.
function a_cb
- The function of processing the result of the function execution
Function signature: a_cb(Error|undefined a_error, number a_lock)
-
Error|undefined a_error - Error object. If the function was locking an already locked file, then the error object will contain an unavailable property set to true (if a_quiet is false)
-
number a_lock - If successful, contains a lock handle that must be passed to the unlockFile function to unlock the file. If the lock was on an already locked file and the a_quiet argument is true, then a_lock contains the value undefined
Example: Function application (a_quiet: false)
let libUtil = require("util");
let libLock = require("fcf-framework-lock");
async function main(){
let lock1, lock2;
try {
lock1 = await libUtil.promisify(libLock.tryLockFile)("index.js");
console.log("Lock 1 successful");
} catch(error){
console.log(`Error lock 1 (error.unavailable: ${error.unavailable})`);
}
try {
lock2 = await libUtil.promisify(libLock.tryLockFile)("index.js");
console.log("Lock 1 successful");
} catch(error){
console.log(`Error lock 2 (error.unavailable: ${error.unavailable})`);
}
if (lock1) {
await libUtil.promisify(libLock.unlockFile)(lock1);
}
if (lock2) {
await libUtil.promisify(libLock.unlockFile)(lock2);
}
}
main();
Output:
Lock 1 successful
Error lock 2 (error.unavailable: true)
Example: Function application (a_quiet: true)
let libUtil = require("util");
let libLock = require("fcf-framework-lock");
async function main(){
let lock1 = await libUtil.promisify(libLock.tryLockFile)("index.js", true);
if (lock1) {
console.log(`Lock 1 successful`);
} else {
console.log(`Lock 1. File lock failed because the file is already locked`);
}
let lock2 = await libUtil.promisify(libLock.tryLockFile)("index.js", true);
if (lock2) {
console.log(`Lock 2 successful`);
} else {
console.log(`Lock 2. File lock failed because the file is already locked`);
}
if (lock1) {
await libUtil.promisify(libLock.unlockFile)(lock1);
}
if (lock2) {
await libUtil.promisify(libLock.unlockFile)(lock2);
}
}
main();
Output:
Lock 1 successful
Lock 2. File lock failed because the file is already locked