File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,7 @@ import {
62
62
} from "../utils/page-mode"
63
63
import { validateEngines } from "../utils/validate-engines"
64
64
import { constructConfigObject } from "../utils/gatsby-cloud-config"
65
+ import { waitUntilWorkerJobsAreComplete } from "../utils/jobs/worker-messaging"
65
66
66
67
module . exports = async function build (
67
68
program : IBuildArgs ,
@@ -295,7 +296,10 @@ module.exports = async function build(
295
296
parentSpan : buildSpan ,
296
297
} )
297
298
// Jobs still might be running even though query running finished
298
- await waitUntilAllJobsComplete ( )
299
+ await Promise . all ( [
300
+ waitUntilAllJobsComplete ( ) ,
301
+ waitUntilWorkerJobsAreComplete ( ) ,
302
+ ] )
299
303
// Restart worker pool before merging state to lower memory pressure while merging state
300
304
waitForWorkerPoolRestart = workerPool . restart ( )
301
305
await mergeWorkerState ( workerPool , buildSpan )
Original file line number Diff line number Diff line change @@ -11,11 +11,18 @@ import { internalActions } from "../../redux/actions"
11
11
import { GatsbyWorkerPool } from "../worker/types"
12
12
import { isWorker , getMessenger } from "../worker/messaging"
13
13
14
+ let hasActiveWorkerJobs : pDefer . DeferredPromise < void > | null = null
15
+ let activeWorkerJobs = 0
16
+
14
17
export function initJobsMessagingInMainProcess (
15
18
workerPool : GatsbyWorkerPool
16
19
) : void {
17
20
workerPool . onMessage ( ( msg , workerId ) => {
18
21
if ( msg . type === MESSAGE_TYPES . JOB_CREATED ) {
22
+ if ( activeWorkerJobs === 0 ) {
23
+ hasActiveWorkerJobs = pDefer < void > ( )
24
+ }
25
+ activeWorkerJobs ++
19
26
store
20
27
. dispatch ( internalActions . createJobV2FromInternalJob ( msg . payload ) )
21
28
. then ( result => {
@@ -42,10 +49,19 @@ export function initJobsMessagingInMainProcess(
42
49
workerId
43
50
)
44
51
} )
52
+ . finally ( ( ) => {
53
+ if ( -- activeWorkerJobs === 0 ) {
54
+ hasActiveWorkerJobs ?. resolve ( )
55
+ hasActiveWorkerJobs = null
56
+ }
57
+ } )
45
58
}
46
59
} )
47
60
}
48
61
62
+ export const waitUntilWorkerJobsAreComplete = ( ) : Promise < void > =>
63
+ hasActiveWorkerJobs ? hasActiveWorkerJobs . promise : Promise . resolve ( )
64
+
49
65
/**
50
66
* This map is ONLY used in worker. It's purpose is to keep track of promises returned to plugins
51
67
* when creating jobs (in worker context), so that we can resolve or reject those once main process
You can’t perform that action at this time.
0 commit comments