File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed
Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change 11const { lock } = require ( 'proper-lockfile' )
22
3+ const staleSeconds = 30
4+
35// Obtains a lock on the path, and maintains it until the callback finishes
46async function withLock ( path , options = { } , callback = options ) {
5- const { mustExist } = options
6- const releaseLock = await lock ( path , { retries : 10 , realpath : ! ! mustExist } )
7+ // Obtain the lock
8+ const releaseLock = await lock ( path , {
9+ retries : 10 ,
10+ update : 1000 ,
11+ stale : staleSeconds * 1000 ,
12+ realpath : ! ! options . mustExist ,
13+ onCompromised : ( ) => {
14+ throw new Error ( `The file at ${ path } was not updated within ${ staleSeconds } s.` )
15+ }
16+ } )
17+
18+ // Try executing the callback, waiting for its returned promise to resolve
719 try {
820 return await callback ( )
921 } finally {
22+ // Ensure the lock is always released
1023 releaseLock ( )
1124 }
1225}
You can’t perform that action at this time.
0 commit comments