33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
const lockfile = require('proper-lockfile');
|
|
|
|
async function lockFile(filePath) {
|
|
try {
|
|
await lockfile.lock(filePath, {
|
|
realpath: false,
|
|
retries: {
|
|
retries: 10, // Number of retries
|
|
factor: 2, // The exponential factor
|
|
minTimeout: 1000, // The number of milliseconds before starting the first retry
|
|
maxTimeout: 5000, // The maximum number of milliseconds between two retries
|
|
randomize: true, // Randomizes the timeouts by multiplying with a factor between 1 to 2
|
|
}
|
|
});
|
|
console.log(`File locked: ${filePath}`);
|
|
} catch (error) {
|
|
console.error(`Error locking file ${filePath}: ${error.message}`);
|
|
throw error; // Propagate the error if unable to lock after retries
|
|
}
|
|
}
|
|
|
|
async function unlockFile(filePath) {
|
|
try {
|
|
await lockfile.unlock(filePath);
|
|
console.log(`File unlocked: ${filePath}`);
|
|
} catch (error) {
|
|
console.error(`Error unlocking file ${filePath}: ${error.message}`);
|
|
// Decide whether to throw the error or not, based on your error handling strategy
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
module.exports = {lockFile, unlockFile} |