Skip to content

Commit b90f394

Browse files
GatsbyJS Botpieh
GatsbyJS Bot
andauthored
chore(gatsby): log pending jobs when build is stuck (#34102) (#34107)
Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 070eeee commit b90f394

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

packages/gatsby-cli/src/reporter/redux/diagnostics.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ const FIVE_SECONDS = 1000 * 5
3838
const TEN_MINUTES = 1000 * 60 * 10
3939
const TEN_SECONDS = 1000 * 10
4040

41+
export type AdditionalDiagnosticsOutputHandler = () => string
42+
const additionalDiagnosticOutputHandlers: Array<AdditionalDiagnosticsOutputHandler> =
43+
[]
44+
45+
export function registerAdditionalDiagnosticOutputHandler(
46+
handler: AdditionalDiagnosticsOutputHandler
47+
): void {
48+
additionalDiagnosticOutputHandlers.push(handler)
49+
}
50+
51+
function generateAdditionalOutput(): string {
52+
const extraMessages: Array<string> = []
53+
54+
for (const handler of additionalDiagnosticOutputHandlers) {
55+
const msg = handler()
56+
57+
if (msg) {
58+
extraMessages.push(msg)
59+
}
60+
}
61+
62+
return extraMessages.length > 0
63+
? `\n\nAdditional debugging logs:\n\n${extraMessages.join(`\n\n`)}`
64+
: ``
65+
}
66+
4167
export function createStructuredLoggingDiagnosticsMiddleware(
4268
getStore: () => GatsbyCLIStore
4369
): DiagnosticsMiddleware {
@@ -152,7 +178,7 @@ export function createStructuredLoggingDiagnosticsMiddleware(
152178
1000
153179
).toFixed(3)} seconds if nothing will change.`
154180
: ``
155-
}`
181+
}${generateAdditionalOutput()}`
156182
)
157183
displayingStuckStatusDiagnosticWarning = false
158184
displayedStuckStatusDiagnosticWarning = true
@@ -179,6 +205,7 @@ export function createStructuredLoggingDiagnosticsMiddleware(
179205
stuckStatusDiagnosticMessage:
180206
generateStuckStatusDiagnosticMessage(),
181207
stuckStatusWatchdogTimeoutDelay,
208+
additionalOutput: generateAdditionalOutput(),
182209
},
183210
})
184211
},

packages/gatsby-cli/src/reporter/reporter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import {
1919
ILogIntent,
2020
IRenderPageArgs,
2121
} from "./types"
22+
import {
23+
registerAdditionalDiagnosticOutputHandler,
24+
AdditionalDiagnosticsOutputHandler,
25+
} from "./redux/diagnostics"
2226

2327
const errorFormatter = getErrorFormatter()
2428
const tracer = globalTracer()
@@ -351,6 +355,12 @@ class Reporter {
351355
_renderPageTree(args: IRenderPageArgs): void {
352356
reporterActions.renderPageTree(args)
353357
}
358+
359+
_registerAdditionalDiagnosticOutputHandler(
360+
handler: AdditionalDiagnosticsOutputHandler
361+
): void {
362+
registerAdditionalDiagnosticOutputHandler(handler)
363+
}
354364
}
355365
export type { Reporter }
356366
export const reporter = new Reporter()

packages/gatsby-cli/src/structured-errors/error-map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ const errors = {
620620
3
621621
)} seconds. Activities preventing Gatsby from transitioning to idle state:\n\n${
622622
context.stuckStatusDiagnosticMessage
623-
}`,
623+
}${context.additionalOutput}`,
624624
level: Level.ERROR,
625625
docsUrl: `https://support.gatsbyjs.com/hc/en-us/articles/360056811354`,
626626
},

packages/gatsby/src/services/initialize.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { IGatsbyState, IStateProgram } from "../redux/types"
2020
import { IBuildContext } from "./types"
2121
import { detectLmdbStore } from "../datastore"
2222
import { loadConfigAndPlugins } from "../bootstrap/load-config-and-plugins"
23+
import type { InternalJob } from "../utils/jobs/types"
2324

2425
interface IPluginResolution {
2526
resolve: string
@@ -101,6 +102,29 @@ export async function initialize({
101102
args.setStore(store)
102103
}
103104

105+
if (reporter._registerAdditionalDiagnosticOutputHandler) {
106+
reporter._registerAdditionalDiagnosticOutputHandler(
107+
function logPendingJobs(): string {
108+
const outputs: Array<InternalJob> = []
109+
110+
for (const [, { job }] of store.getState().jobsV2.incomplete) {
111+
outputs.push(job)
112+
if (outputs.length >= 5) {
113+
// 5 not finished jobs should be enough to track down issues
114+
// this is just limiting output "spam"
115+
break
116+
}
117+
}
118+
119+
return outputs.length
120+
? `Unfinished jobs (showing ${outputs.length} of ${
121+
store.getState().jobsV2.incomplete.size
122+
} jobs total):\n\n` + JSON.stringify(outputs, null, 2)
123+
: ``
124+
}
125+
)
126+
}
127+
104128
const directory = slash(args.directory)
105129

106130
const program: IStateProgram = {

0 commit comments

Comments
 (0)