Skip to content

Commit

Permalink
chore(gatsby): log pending jobs when build is stuck (#34102) (#34107)
Browse files Browse the repository at this point in the history
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
GatsbyJS Bot and pieh committed Dec 1, 2021
1 parent 070eeee commit b90f394
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
29 changes: 28 additions & 1 deletion packages/gatsby-cli/src/reporter/redux/diagnostics.ts
Expand Up @@ -38,6 +38,32 @@ const FIVE_SECONDS = 1000 * 5
const TEN_MINUTES = 1000 * 60 * 10
const TEN_SECONDS = 1000 * 10

export type AdditionalDiagnosticsOutputHandler = () => string
const additionalDiagnosticOutputHandlers: Array<AdditionalDiagnosticsOutputHandler> =
[]

export function registerAdditionalDiagnosticOutputHandler(
handler: AdditionalDiagnosticsOutputHandler
): void {
additionalDiagnosticOutputHandlers.push(handler)
}

function generateAdditionalOutput(): string {
const extraMessages: Array<string> = []

for (const handler of additionalDiagnosticOutputHandlers) {
const msg = handler()

if (msg) {
extraMessages.push(msg)
}
}

return extraMessages.length > 0
? `\n\nAdditional debugging logs:\n\n${extraMessages.join(`\n\n`)}`
: ``
}

export function createStructuredLoggingDiagnosticsMiddleware(
getStore: () => GatsbyCLIStore
): DiagnosticsMiddleware {
Expand Down Expand Up @@ -152,7 +178,7 @@ export function createStructuredLoggingDiagnosticsMiddleware(
1000
).toFixed(3)} seconds if nothing will change.`
: ``
}`
}${generateAdditionalOutput()}`
)
displayingStuckStatusDiagnosticWarning = false
displayedStuckStatusDiagnosticWarning = true
Expand All @@ -179,6 +205,7 @@ export function createStructuredLoggingDiagnosticsMiddleware(
stuckStatusDiagnosticMessage:
generateStuckStatusDiagnosticMessage(),
stuckStatusWatchdogTimeoutDelay,
additionalOutput: generateAdditionalOutput(),
},
})
},
Expand Down
10 changes: 10 additions & 0 deletions packages/gatsby-cli/src/reporter/reporter.ts
Expand Up @@ -19,6 +19,10 @@ import {
ILogIntent,
IRenderPageArgs,
} from "./types"
import {
registerAdditionalDiagnosticOutputHandler,
AdditionalDiagnosticsOutputHandler,
} from "./redux/diagnostics"

const errorFormatter = getErrorFormatter()
const tracer = globalTracer()
Expand Down Expand Up @@ -351,6 +355,12 @@ class Reporter {
_renderPageTree(args: IRenderPageArgs): void {
reporterActions.renderPageTree(args)
}

_registerAdditionalDiagnosticOutputHandler(
handler: AdditionalDiagnosticsOutputHandler
): void {
registerAdditionalDiagnosticOutputHandler(handler)
}
}
export type { Reporter }
export const reporter = new Reporter()
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/structured-errors/error-map.ts
Expand Up @@ -620,7 +620,7 @@ const errors = {
3
)} seconds. Activities preventing Gatsby from transitioning to idle state:\n\n${
context.stuckStatusDiagnosticMessage
}`,
}${context.additionalOutput}`,
level: Level.ERROR,
docsUrl: `https://support.gatsbyjs.com/hc/en-us/articles/360056811354`,
},
Expand Down
24 changes: 24 additions & 0 deletions packages/gatsby/src/services/initialize.ts
Expand Up @@ -20,6 +20,7 @@ import { IGatsbyState, IStateProgram } from "../redux/types"
import { IBuildContext } from "./types"
import { detectLmdbStore } from "../datastore"
import { loadConfigAndPlugins } from "../bootstrap/load-config-and-plugins"
import type { InternalJob } from "../utils/jobs/types"

interface IPluginResolution {
resolve: string
Expand Down Expand Up @@ -101,6 +102,29 @@ export async function initialize({
args.setStore(store)
}

if (reporter._registerAdditionalDiagnosticOutputHandler) {
reporter._registerAdditionalDiagnosticOutputHandler(
function logPendingJobs(): string {
const outputs: Array<InternalJob> = []

for (const [, { job }] of store.getState().jobsV2.incomplete) {
outputs.push(job)
if (outputs.length >= 5) {
// 5 not finished jobs should be enough to track down issues
// this is just limiting output "spam"
break
}
}

return outputs.length
? `Unfinished jobs (showing ${outputs.length} of ${
store.getState().jobsV2.incomplete.size
} jobs total):\n\n` + JSON.stringify(outputs, null, 2)
: ``
}
)
}

const directory = slash(args.directory)

const program: IStateProgram = {
Expand Down

0 comments on commit b90f394

Please sign in to comment.