Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): explicitly exit workers when they're done #8226

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/sanity/src/_internal/cli/threads/extractManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export interface ExtractManifestWorkerData {
workDir: string
}

if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}
async function main() {
if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}

const opts = _workerData as ExtractManifestWorkerData
const opts = _workerData as ExtractManifestWorkerData

const cleanup = mockBrowserEnvironment(opts.workDir)
const cleanup = mockBrowserEnvironment(opts.workDir)

async function main() {
try {
const workspaces = await getStudioWorkspaces({basePath: opts.workDir})

Expand All @@ -30,4 +30,4 @@ async function main() {
}
}

main()
main().then(() => process.exit())
14 changes: 7 additions & 7 deletions packages/sanity/src/_internal/cli/threads/extractSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export interface ExtractSchemaWorkerResult {
schema: ReturnType<typeof extractSchema>
}

if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}
async function main() {
if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}

const opts = _workerData as ExtractSchemaWorkerData
const cleanup = mockBrowserEnvironment(opts.workDir)
const opts = _workerData as ExtractSchemaWorkerData
const cleanup = mockBrowserEnvironment(opts.workDir)

async function main() {
try {
if (opts.format !== 'groq-type-nodes') {
throw new Error(`Unsupported format: "${opts.format}"`)
Expand All @@ -48,7 +48,7 @@ async function main() {
}
}

main()
main().then(() => process.exit())

function getWorkspace({
workspaces,
Expand Down
10 changes: 7 additions & 3 deletions packages/sanity/src/_internal/cli/threads/getGraphQLAPIs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import {type Workspace} from 'sanity'
import {type SchemaDefinitionish, type TypeResolvedGraphQLAPI} from '../actions/graphql/types'
import {getStudioWorkspaces} from '../util/getStudioWorkspaces'

if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
async function main() {
if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}

await getGraphQLAPIsForked(parentPort)
}

getGraphQLAPIsForked(parentPort)
main().then(() => process.exit())

async function getGraphQLAPIsForked(parent: MessagePort): Promise<void> {
const {cliConfig, cliConfigPath, workDir} = workerData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async function* readerToGenerator(reader: ReadableStreamDefaultReader<Uint8Array
}
}

validateDocuments()
main().then(() => process.exit())

async function loadWorkspace() {
const workspaces = await getStudioWorkspaces({basePath: workDir, configPath})
Expand Down Expand Up @@ -302,7 +302,7 @@ async function checkReferenceExistence({
return {existingIds}
}

async function validateDocuments() {
async function main() {
// note: this is dynamically imported because this module is ESM only and this
// file gets compiled to CJS at this time
const {default: pMap} = await import('p-map')
Expand Down
84 changes: 44 additions & 40 deletions packages/sanity/src/_internal/cli/threads/validateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,57 @@ const {
level = 'warning',
} = _workerData as ValidateSchemaWorkerData

if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}
async function main() {
if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}

const cleanup = mockBrowserEnvironment(workDir)
const cleanup = mockBrowserEnvironment(workDir)

try {
const workspaces = getStudioConfig({basePath: workDir})
try {
const workspaces = getStudioConfig({basePath: workDir})

if (!workspaces.length) {
throw new Error(`Configuration did not return any workspaces.`)
}

let workspace
if (workspaceName) {
workspace = workspaces.find((w) => w.name === workspaceName)
if (!workspace) {
throw new Error(`Could not find any workspaces with name \`${workspaceName}\``)
if (!workspaces.length) {
throw new Error(`Configuration did not return any workspaces.`)
}
} else {
if (workspaces.length !== 1) {
throw new Error(
"Multiple workspaces found. Please specify which workspace to use with '--workspace'.",
)

let workspace
if (workspaceName) {
workspace = workspaces.find((w) => w.name === workspaceName)
if (!workspace) {
throw new Error(`Could not find any workspaces with name \`${workspaceName}\``)
}
} else {
if (workspaces.length !== 1) {
throw new Error(
"Multiple workspaces found. Please specify which workspace to use with '--workspace'.",
)
}
workspace = workspaces[0]
}
workspace = workspaces[0]
}

const schemaTypes = resolveSchemaTypes({
config: workspace,
context: {dataset: workspace.dataset, projectId: workspace.projectId},
})
const schemaTypes = resolveSchemaTypes({
config: workspace,
context: {dataset: workspace.dataset, projectId: workspace.projectId},
})

const validation = groupProblems(validateSchema(schemaTypes).getTypes())
const validation = groupProblems(validateSchema(schemaTypes).getTypes())

const result: ValidateSchemaWorkerResult = {
validation: validation
.map((group) => ({
...group,
problems: group.problems.filter((problem) =>
level === 'error' ? problem.severity === 'error' : true,
),
}))
.filter((group) => group.problems.length),
}
const result: ValidateSchemaWorkerResult = {
validation: validation
.map((group) => ({
...group,
problems: group.problems.filter((problem) =>
level === 'error' ? problem.severity === 'error' : true,
),
}))
.filter((group) => group.problems.length),
}

parentPort?.postMessage(result)
} finally {
cleanup()
parentPort?.postMessage(result)
} finally {
cleanup()
}
}

main().then(() => process.exit())
Loading