Skip to content

Commit

Permalink
fix mutex review
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Feb 10, 2022
1 parent 8091100 commit 2f42d93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
18 changes: 11 additions & 7 deletions packages/gatsby-core-utils/src/mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ interface IMutex {
}

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

async function waitUntilUnlocked(
storage: ReturnType<typeof getStorage>,
key: string
key: string,
timeout: number
): Promise<void> {
const isUnlocked = await storage.mutex.ifNoExists(key, () => {
storage.mutex.put(key, LockStatus.Locked)
Expand All @@ -22,8 +23,8 @@ async function waitUntilUnlocked(

await new Promise<void>(resolve => {
setTimeout(() => {
resolve(waitUntilUnlocked(storage, key))
}, MUTEX_INTERVAL)
resolve(waitUntilUnlocked(storage, key, timeout))
}, timeout)
})
}

Expand All @@ -32,15 +33,18 @@ async function waitUntilUnlocked(
*
* @param {string} key A unique key
*/
export function createMutex(key: string): IMutex {
export function createMutex(
key: string,
timeout = DEFAULT_MUTEX_INTERVAL
): IMutex {
const storage = getStorage(getDatabaseDir())
const BUILD_ID = global.__GATSBY?.buildId ?? ``
const prefixedKey = `${BUILD_ID}-${key}`

return {
acquire: (): Promise<void> => waitUntilUnlocked(storage, prefixedKey),
acquire: (): Promise<void> =>
waitUntilUnlocked(storage, prefixedKey, timeout),
release: async (): Promise<void> => {
// console.log({ unlock: prefixedKey })
await storage.mutex.remove(prefixedKey)
},
}
Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby/src/services/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ export async function initialize({

const state = store.getState()

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

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

Expand Down Expand Up @@ -417,6 +415,15 @@ export async function initialize({
`!${cacheDirectory}/data/gatsby-core-utils/`,
`!${cacheDirectory}/data/gatsby-core-utils/**`,
]

if (process.env.GATSBY_EXPERIMENTAL_PRESERVE_FILE_DOWNLOAD_CACHE) {
// Stop the caches directory from being deleted, add all sub directories,
// but remove gatsby-source-filesystem
deleteGlobs.push(`!${cacheDirectory}/caches`)
deleteGlobs.push(`${cacheDirectory}/caches/*`)
deleteGlobs.push(`!${cacheDirectory}/caches/gatsby-source-filesystem`)
}

if (process.env.GATSBY_EXPERIMENTAL_PRESERVE_WEBPACK_CACHE) {
// Add webpack
deleteGlobs.push(`!${cacheDirectory}/webpack`)
Expand Down

0 comments on commit 2f42d93

Please sign in to comment.