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

LockFile class

A helper utility for working with file-based locks. This class should only be used for locking resources across processes, but should not be used for attempting to lock a resource in the same process.


export declare class LockFile 


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.


Method Modifiers Description
acquire(resourceDir, 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(resourceDir, resourceName, pid) static Returns the path to the lockfile, should it be created successfully.
release()   Unlocks a file and removes it from disk. This can only be called once.
tryAcquire(resourceDir, resourceName) static Attempts to create a lockfile with the given filePath. If successful, returns a LockFile instance. If unable to get a lock, returns undefined.