Skip to content

Commit

Permalink
debugging more
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdz committed Nov 17, 2020
1 parent f3af8a6 commit a5dc27e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
24 changes: 22 additions & 2 deletions packages/gatsby/src/cache/cache-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import reporter from "gatsby-cli/lib/reporter"
// we currently don't support two concurrent builds at the same time anyways.
const globalGatsbyCacheLock = new Map()

global.debugging = []

/**
* construction of the disk storage
* @param {object} [args] options of disk store
Expand Down Expand Up @@ -221,24 +223,39 @@ DiskStore.prototype.reset = wrapCallback(async function (): Promise<void> {
}
})

let lockRid = 0

/**
* locks a file so other forks that want to use the same file have to wait
* @param {string} filePath
* @returns {Promise}
* @private
*/
DiskStore.prototype._lock = function _lock(filePath): Promise<void> {
const rid = ++lockRid
global.debugging.push(rid + ` lock(` + filePath + `) requested`)
return new Promise((resolve, reject) =>
innerLock(resolve, reject, filePath)
).then(() => {
global.debugging.push(rid + ` lock(` + filePath + `) received`)
globalGatsbyCacheLock.set(filePath, Date.now())
})
}

function innerLock(resolve, reject, filePath): void {
function innerLock(resolve, reject, filePath, rid): void {
try {
let lockTime = globalGatsbyCacheLock.get(filePath) ?? 0
global.debugging.push(
rid +
` lock time for ` +
filePath +
` is ` +
lockTime +
`, cond: ` +
(lockTime > 0 && Date.now() - lockTime > 10 * 1000)
)
if (lockTime > 0 && Date.now() - lockTime > 10 * 1000) {
global.debugging.push(rid + ` lock timeout for ` + filePath)
reporter.verbose(
`Warning: lock file older than 10s, ignoring it... There is a possibility this leads to caching problems later.`
)
Expand All @@ -247,10 +264,12 @@ function innerLock(resolve, reject, filePath): void {
}

if (lockTime > 0) {
global.debugging.push(rid + ` waiting 50ms for ` + filePath)
setTimeout(() => {
innerLock(resolve, reject, filePath)
innerLock(resolve, reject, filePath, rid)
}, 50)
} else {
global.debugging.push(rid + ` have lock for ` + filePath)
resolve()
}
} catch (e) {
Expand All @@ -266,6 +285,7 @@ function innerLock(resolve, reject, filePath): void {
* @private
*/
DiskStore.prototype._unlock = function _unlock(filePath): Promise<void> {
global.debugging.push(`lock(` + filePath + `) released`)
globalGatsbyCacheLock.delete(filePath)
}

Expand Down
7 changes: 6 additions & 1 deletion packages/gatsby/src/cache/json-file-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const fs = require(`fs`)
const zlib = require(`zlib`)

exports.write = async function (path, data, options): Promise<void> {
global.debugging.push(`writing to ` + path)
const externalBuffers = []
let dataString = JSON.stringify(data, function replacerFunction(k, value) {
//Buffers searilize to {data: [...], type: "Buffer"}
Expand Down Expand Up @@ -84,6 +85,8 @@ exports.write = async function (path, data, options): Promise<void> {
}

exports.read = async function (path, options): Promise<string> {
global.debugging.push(`reading from ` + path)

let zipExtension = ``
if (options.zip) {
zipExtension = `.gz`
Expand Down Expand Up @@ -137,7 +140,9 @@ exports.read = async function (path, options): Promise<string> {
"json-file-store failed to JSON.parse this string: `" +
dataString.replace(/\n/g, `⏎`) +
"`\n" +
e.message
e.message +
`\ndebugging:` +
global.debugging.map(s => `- ` + s).join(`\n`)
)
}

Expand Down

0 comments on commit a5dc27e

Please sign in to comment.