Skip to content

Commit cb0e1fb

Browse files
committed
fix mutex review
1 parent 034a236 commit cb0e1fb

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

packages/gatsby-core-utils/src/mutex.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ interface IMutex {
66
}
77

88
// Random number to re-check if mutex got released
9-
const MUTEX_INTERVAL = 3000
9+
const DEFAULT_MUTEX_INTERVAL = 3000
1010

1111
async function waitUntilUnlocked(
1212
storage: ReturnType<typeof getStorage>,
13-
key: string
13+
key: string,
14+
timeout: number
1415
): Promise<void> {
1516
const isUnlocked = await storage.mutex.ifNoExists(key, () => {
1617
storage.mutex.put(key, LockStatus.Locked)
@@ -22,8 +23,8 @@ async function waitUntilUnlocked(
2223

2324
await new Promise<void>(resolve => {
2425
setTimeout(() => {
25-
resolve(waitUntilUnlocked(storage, key))
26-
}, MUTEX_INTERVAL)
26+
resolve(waitUntilUnlocked(storage, key, timeout))
27+
}, timeout)
2728
})
2829
}
2930

@@ -32,15 +33,18 @@ async function waitUntilUnlocked(
3233
*
3334
* @param {string} key A unique key
3435
*/
35-
export function createMutex(key: string): IMutex {
36+
export function createMutex(
37+
key: string,
38+
timeout = DEFAULT_MUTEX_INTERVAL
39+
): IMutex {
3640
const storage = getStorage(getDatabaseDir())
3741
const BUILD_ID = global.__GATSBY?.buildId ?? ``
3842
const prefixedKey = `${BUILD_ID}-${key}`
3943

4044
return {
41-
acquire: (): Promise<void> => waitUntilUnlocked(storage, prefixedKey),
45+
acquire: (): Promise<void> =>
46+
waitUntilUnlocked(storage, prefixedKey, timeout),
4247
release: async (): Promise<void> => {
43-
// console.log({ unlock: prefixedKey })
4448
await storage.mutex.remove(prefixedKey)
4549
},
4650
}

packages/gatsby/src/services/initialize.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ export async function initialize({
314314

315315
const state = store.getState()
316316

317-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
318-
const hashes: Array<any> = await Promise.all([
317+
const hashes: any = await Promise.all([
319318
md5File(`package.json`),
320319
md5File(`${program.directory}/gatsby-config.js`).catch(() => {}), // ignore as this file isn't required),
321320
md5File(`${program.directory}/gatsby-node.js`).catch(() => {}), // ignore as this file isn't required),
@@ -324,7 +323,6 @@ export async function initialize({
324323

325324
const pluginsHash = crypto
326325
.createHash(`md5`)
327-
// @ts-ignore - concat expecting same type for some reason
328326
.update(JSON.stringify(pluginVersions.concat(hashes)))
329327
.digest(`hex`)
330328

@@ -417,6 +415,15 @@ export async function initialize({
417415
`!${cacheDirectory}/data/gatsby-core-utils/`,
418416
`!${cacheDirectory}/data/gatsby-core-utils/**`,
419417
]
418+
419+
if (process.env.GATSBY_EXPERIMENTAL_PRESERVE_FILE_DOWNLOAD_CACHE) {
420+
// Stop the caches directory from being deleted, add all sub directories,
421+
// but remove gatsby-source-filesystem
422+
deleteGlobs.push(`!${cacheDirectory}/caches`)
423+
deleteGlobs.push(`${cacheDirectory}/caches/*`)
424+
deleteGlobs.push(`!${cacheDirectory}/caches/gatsby-source-filesystem`)
425+
}
426+
420427
if (process.env.GATSBY_EXPERIMENTAL_PRESERVE_WEBPACK_CACHE) {
421428
// Add webpack
422429
deleteGlobs.push(`!${cacheDirectory}/webpack`)

0 commit comments

Comments
 (0)