Skip to content

Commit

Permalink
shutdown gracefully after build
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 4, 2024
1 parent f9fcdc1 commit 1da22c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ pub async fn project_update(
Ok(())
}

#[napi(ts_return_type = "{ __napiType: \"Project\" }")]
pub async fn project_shutdown(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) {
project.turbo_tasks.stop_and_wait().await;
}

#[napi(object)]
#[derive(Default)]
struct AppPageNapiRoute {
Expand Down
14 changes: 13 additions & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ export default async function build(
async function turbopackBuild(): Promise<{
duration: number
buildTraceContext: undefined
shutdownPromise: Promise<void>
}> {
if (!IS_TURBOPACK_BUILD) {
throw new Error("next build doesn't support turbopack yet")
Expand Down Expand Up @@ -1464,6 +1465,8 @@ export default async function build(
}
}

const shutdownPromise = project.shutdown()

if (warnings.length > 0) {
Log.warn(
`Turbopack build collected ${warnings.length} warnings:\n${warnings
Expand All @@ -1487,6 +1490,7 @@ export default async function build(
return {
duration: process.hrtime(startTime)[0],
buildTraceContext: undefined,
shutdownPromise,
}
}

Expand Down Expand Up @@ -1531,9 +1535,15 @@ export default async function build(
},
})

let shutdownPromise = Promise.resolve()
if (!isGenerateMode) {
if (turboNextBuild) {
const { duration: compilerDuration, ...rest } = await turbopackBuild()
const {
duration: compilerDuration,
shutdownPromise: p,
...rest
} = await turbopackBuild()
shutdownPromise = p
traceMemoryUsage('Finished build', nextBuildSpan)

buildTraceContext = rest.buildTraceContext
Expand Down Expand Up @@ -3566,6 +3576,8 @@ export default async function build(
await nextBuildSpan
.traceChild('telemetry-flush')
.traceAsyncFn(() => telemetry.flush())

await shutdownPromise
})
} finally {
// Ensure we wait for lockfile patching if present
Expand Down
6 changes: 6 additions & 0 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,8 @@ export interface Project {
aggregationMs: number
): AsyncIterableIterator<TurbopackResult<UpdateMessage>>

shutdown(): Promise<void>

onExit(): Promise<void>
}

Expand Down Expand Up @@ -1100,6 +1102,10 @@ function bindingToApi(
return subscription
}

shutdown(): Promise<void> {
return binding.projectShutdown(this._nativeProject)
}

onExit(): Promise<void> {
return binding.projectOnExit(this._nativeProject)
}
Expand Down

0 comments on commit 1da22c8

Please sign in to comment.