Home > @rushstack/node-core-library > LockFile

LockFile class

The LockFile implements a file-based mutex for synchronizing access to a shared resource between multiple Node.js processes. It is not recommended for synchronization solely within a single Node.js process.

Signature:

export declare class LockFile 

Remarks

The implementation works on Windows, Mac, and Linux without requiring any native helpers. On non-Windows systems, the algorithm requires access to the ps shell command. On Linux, it requires access the /proc/${pidString}/stat filesystem.

Properties

Property Modifiers Type Description
dirtyWhenAcquired   boolean Returns the initial state of the lock. This can be used to detect if the previous process was terminated before releasing the resource.
filePath   string Returns the absolute path to the lockfile
isReleased   boolean Returns true if this lock is currently being held.

Methods

Method Modifiers Description
acquire(resourceFolder, resourceName, maxWaitMs) static Attempts to create the lockfile. Will continue to loop at every 100ms until the lock becomes available or the maxWaitMs is surpassed.
getLockFilePath(resourceFolder, resourceName, pid) static Returns the path of the lockfile that will be created when a lock is successfully acquired.
release()   Unlocks a file and removes it from disk. This can only be called once.
tryAcquire(resourceFolder, resourceName) static Attempts to create a lockfile with the given filePath.