Rush StackShopBlogEvents
Skip to main content

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

PropertyModifiersTypeDescription
dirtyWhenAcquiredreadonlybooleanReturns the initial state of the lock. This can be used to detect if the previous process was terminated before releasing the resource.
filePathreadonlystringReturns the absolute path to the lockfile
isReleasedreadonlybooleanReturns true if this lock is currently being held.

Methods

MethodModifiersDescription
acquire(resourceFolder, resourceName, maxWaitMs)staticAttempts 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)staticReturns the path of the lockfile that will be created when a lock is successfully acquired.
release(deleteFile)Unlocks a file and optionally removes it from disk. This can only be called once.
tryAcquire(resourceFolder, resourceName)staticAttempts to create a lockfile with the given filePath.