Skip to content

Commit 2168882

Browse files
committed
Keep lock for up to 30 seconds.
1 parent ff80d46 commit 2168882

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/lock.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
const { lock } = require('proper-lockfile')
22

3+
const staleSeconds = 30
4+
35
// Obtains a lock on the path, and maintains it until the callback finishes
46
async 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
}

0 commit comments

Comments
 (0)