Skip to content

Commit 104b74c

Browse files
authoredJan 13, 2025··
fix(cli): explicitly exit workers when they're done (#8226)
1 parent f3151c3 commit 104b74c

File tree

5 files changed

+67
-59
lines changed

5 files changed

+67
-59
lines changed
 

‎packages/sanity/src/_internal/cli/threads/extractManifest.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ export interface ExtractManifestWorkerData {
99
workDir: string
1010
}
1111

12-
if (isMainThread || !parentPort) {
13-
throw new Error('This module must be run as a worker thread')
14-
}
12+
async function main() {
13+
if (isMainThread || !parentPort) {
14+
throw new Error('This module must be run as a worker thread')
15+
}
1516

16-
const opts = _workerData as ExtractManifestWorkerData
17+
const opts = _workerData as ExtractManifestWorkerData
1718

18-
const cleanup = mockBrowserEnvironment(opts.workDir)
19+
const cleanup = mockBrowserEnvironment(opts.workDir)
1920

20-
async function main() {
2121
try {
2222
const workspaces = await getStudioWorkspaces({basePath: opts.workDir})
2323

@@ -30,4 +30,4 @@ async function main() {
3030
}
3131
}
3232

33-
main()
33+
main().then(() => process.exit())

‎packages/sanity/src/_internal/cli/threads/extractSchema.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export interface ExtractSchemaWorkerResult {
1919
schema: ReturnType<typeof extractSchema>
2020
}
2121

22-
if (isMainThread || !parentPort) {
23-
throw new Error('This module must be run as a worker thread')
24-
}
22+
async function main() {
23+
if (isMainThread || !parentPort) {
24+
throw new Error('This module must be run as a worker thread')
25+
}
2526

26-
const opts = _workerData as ExtractSchemaWorkerData
27-
const cleanup = mockBrowserEnvironment(opts.workDir)
27+
const opts = _workerData as ExtractSchemaWorkerData
28+
const cleanup = mockBrowserEnvironment(opts.workDir)
2829

29-
async function main() {
3030
try {
3131
if (opts.format !== 'groq-type-nodes') {
3232
throw new Error(`Unsupported format: "${opts.format}"`)
@@ -48,7 +48,7 @@ async function main() {
4848
}
4949
}
5050

51-
main()
51+
main().then(() => process.exit())
5252

5353
function getWorkspace({
5454
workspaces,

‎packages/sanity/src/_internal/cli/threads/getGraphQLAPIs.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ import {type Workspace} from 'sanity'
99
import {type SchemaDefinitionish, type TypeResolvedGraphQLAPI} from '../actions/graphql/types'
1010
import {getStudioWorkspaces} from '../util/getStudioWorkspaces'
1111

12-
if (isMainThread || !parentPort) {
13-
throw new Error('This module must be run as a worker thread')
12+
async function main() {
13+
if (isMainThread || !parentPort) {
14+
throw new Error('This module must be run as a worker thread')
15+
}
16+
17+
await getGraphQLAPIsForked(parentPort)
1418
}
1519

16-
getGraphQLAPIsForked(parentPort)
20+
main().then(() => process.exit())
1721

1822
async function getGraphQLAPIsForked(parent: MessagePort): Promise<void> {
1923
const {cliConfig, cliConfigPath, workDir} = workerData

‎packages/sanity/src/_internal/cli/threads/validateDocuments.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async function* readerToGenerator(reader: ReadableStreamDefaultReader<Uint8Array
129129
}
130130
}
131131

132-
validateDocuments()
132+
main().then(() => process.exit())
133133

134134
async function loadWorkspace() {
135135
const workspaces = await getStudioWorkspaces({basePath: workDir, configPath})
@@ -302,7 +302,7 @@ async function checkReferenceExistence({
302302
return {existingIds}
303303
}
304304

305-
async function validateDocuments() {
305+
async function main() {
306306
// note: this is dynamically imported because this module is ESM only and this
307307
// file gets compiled to CJS at this time
308308
const {default: pMap} = await import('p-map')

‎packages/sanity/src/_internal/cli/threads/validateSchema.ts

+44-40
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,57 @@ const {
2525
level = 'warning',
2626
} = _workerData as ValidateSchemaWorkerData
2727

28-
if (isMainThread || !parentPort) {
29-
throw new Error('This module must be run as a worker thread')
30-
}
28+
async function main() {
29+
if (isMainThread || !parentPort) {
30+
throw new Error('This module must be run as a worker thread')
31+
}
3132

32-
const cleanup = mockBrowserEnvironment(workDir)
33+
const cleanup = mockBrowserEnvironment(workDir)
3334

34-
try {
35-
const workspaces = getStudioConfig({basePath: workDir})
35+
try {
36+
const workspaces = getStudioConfig({basePath: workDir})
3637

37-
if (!workspaces.length) {
38-
throw new Error(`Configuration did not return any workspaces.`)
39-
}
40-
41-
let workspace
42-
if (workspaceName) {
43-
workspace = workspaces.find((w) => w.name === workspaceName)
44-
if (!workspace) {
45-
throw new Error(`Could not find any workspaces with name \`${workspaceName}\``)
38+
if (!workspaces.length) {
39+
throw new Error(`Configuration did not return any workspaces.`)
4640
}
47-
} else {
48-
if (workspaces.length !== 1) {
49-
throw new Error(
50-
"Multiple workspaces found. Please specify which workspace to use with '--workspace'.",
51-
)
41+
42+
let workspace
43+
if (workspaceName) {
44+
workspace = workspaces.find((w) => w.name === workspaceName)
45+
if (!workspace) {
46+
throw new Error(`Could not find any workspaces with name \`${workspaceName}\``)
47+
}
48+
} else {
49+
if (workspaces.length !== 1) {
50+
throw new Error(
51+
"Multiple workspaces found. Please specify which workspace to use with '--workspace'.",
52+
)
53+
}
54+
workspace = workspaces[0]
5255
}
53-
workspace = workspaces[0]
54-
}
5556

56-
const schemaTypes = resolveSchemaTypes({
57-
config: workspace,
58-
context: {dataset: workspace.dataset, projectId: workspace.projectId},
59-
})
57+
const schemaTypes = resolveSchemaTypes({
58+
config: workspace,
59+
context: {dataset: workspace.dataset, projectId: workspace.projectId},
60+
})
6061

61-
const validation = groupProblems(validateSchema(schemaTypes).getTypes())
62+
const validation = groupProblems(validateSchema(schemaTypes).getTypes())
6263

63-
const result: ValidateSchemaWorkerResult = {
64-
validation: validation
65-
.map((group) => ({
66-
...group,
67-
problems: group.problems.filter((problem) =>
68-
level === 'error' ? problem.severity === 'error' : true,
69-
),
70-
}))
71-
.filter((group) => group.problems.length),
72-
}
64+
const result: ValidateSchemaWorkerResult = {
65+
validation: validation
66+
.map((group) => ({
67+
...group,
68+
problems: group.problems.filter((problem) =>
69+
level === 'error' ? problem.severity === 'error' : true,
70+
),
71+
}))
72+
.filter((group) => group.problems.length),
73+
}
7374

74-
parentPort?.postMessage(result)
75-
} finally {
76-
cleanup()
75+
parentPort?.postMessage(result)
76+
} finally {
77+
cleanup()
78+
}
7779
}
80+
81+
main().then(() => process.exit())

0 commit comments

Comments
 (0)
Please sign in to comment.