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

feature(gatsby): Add experiment to run source plugins in parallel #28214

Merged
merged 6 commits into from
Dec 1, 2020
Merged
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
159 changes: 89 additions & 70 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,90 +558,109 @@ module.exports = async (api, args = {}, { pluginSource, activity } = {}) =>
}
}

Promise.mapSeries(implementingPlugins, plugin => {
if (stopQueuedApiRuns) {
return null
}
let apiRunPromiseOptions = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR has a lot less change than it appears with all the whitespace changes — see it with those removed https://github.com/gatsbyjs/gatsby/pull/28214/files?diff=unified&w=1

let runPromise
if (
api === `sourceNodes` &&
process.env.GATSBY_EXPERIMENTAL_PARALLEL_SOURCING
) {
runPromise = Promise.map
apiRunPromiseOptions.concurrency = 20
} else {
runPromise = Promise.mapSeries
apiRunPromiseOptions = undefined
}

let gatsbyNode = pluginNodeCache.get(plugin.name)
if (!gatsbyNode) {
gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
pluginNodeCache.set(plugin.name, gatsbyNode)
}
runPromise(
implementingPlugins,
plugin => {
if (stopQueuedApiRuns) {
return null
}

const pluginName =
plugin.name === `default-site-plugin` ? `gatsby-node.js` : plugin.name

// TODO: rethink createNode API to handle this better
if (
api === `onCreateNode` &&
gatsbyNode?.unstable_shouldOnCreateNode && // Don't bail if this api is not exported
!gatsbyNode.unstable_shouldOnCreateNode(
{ node: args.node },
plugin.pluginOptions
)
) {
// Do not try to schedule an async event for this node for this plugin
return null
}
let gatsbyNode = pluginNodeCache.get(plugin.name)
if (!gatsbyNode) {
gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
pluginNodeCache.set(plugin.name, gatsbyNode)
}

return new Promise(resolve => {
resolve(runAPI(plugin, api, { ...args, parentSpan: apiSpan }, activity))
}).catch(err => {
decorateEvent(`BUILD_PANIC`, {
pluginName: `${plugin.name}@${plugin.version}`,
})
const pluginName =
plugin.name === `default-site-plugin` ? `gatsby-node.js` : plugin.name

// TODO: rethink createNode API to handle this better
if (
api === `onCreateNode` &&
gatsbyNode?.unstable_shouldOnCreateNode && // Don't bail if this api is not exported
!gatsbyNode.unstable_shouldOnCreateNode(
{ node: args.node },
plugin.pluginOptions
)
) {
// Do not try to schedule an async event for this node for this plugin
return null
}

return new Promise(resolve => {
resolve(
runAPI(plugin, api, { ...args, parentSpan: apiSpan }, activity)
)
}).catch(err => {
decorateEvent(`BUILD_PANIC`, {
pluginName: `${plugin.name}@${plugin.version}`,
})

const localReporter = getLocalReporter({ activity, reporter })
const localReporter = getLocalReporter({ activity, reporter })

const file = stackTrace
.parse(err)
.find(file => /gatsby-node/.test(file.fileName))
const file = stackTrace
.parse(err)
.find(file => /gatsby-node/.test(file.fileName))

let codeFrame = ``
const structuredError = errorParser({ err })
let codeFrame = ``
const structuredError = errorParser({ err })

if (file) {
const { fileName, lineNumber: line, columnNumber: column } = file
if (file) {
const { fileName, lineNumber: line, columnNumber: column } = file

try {
const code = fs.readFileSync(fileName, { encoding: `utf-8` })
codeFrame = codeFrameColumns(
code,
{
start: {
line,
column,
try {
const code = fs.readFileSync(fileName, { encoding: `utf-8` })
codeFrame = codeFrameColumns(
code,
{
start: {
line,
column,
},
},
},
{
highlightCode: true,
}
)
} catch (_e) {
// sometimes stack trace point to not existing file
// particularly when file is transpiled and path actually changes
// (like pointing to not existing `src` dir or original typescript file)
}
{
highlightCode: true,
}
)
} catch (_e) {
// sometimes stack trace point to not existing file
// particularly when file is transpiled and path actually changes
// (like pointing to not existing `src` dir or original typescript file)
}

structuredError.location = {
start: { line: line, column: column },
structuredError.location = {
start: { line: line, column: column },
}
structuredError.filePath = fileName
}
structuredError.filePath = fileName
}

structuredError.context = {
...structuredError.context,
pluginName,
api,
codeFrame,
}
structuredError.context = {
...structuredError.context,
pluginName,
api,
codeFrame,
}

localReporter.panicOnBuild(structuredError)
localReporter.panicOnBuild(structuredError)

return null
})
}).then(results => {
return null
})
},
apiRunPromiseOptions
).then(results => {
if (onAPIRunComplete) {
onAPIRunComplete()
}
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/src/utils/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ const activeFlags: Array<IFlag> = [
description: `Don't process images during development until they're requested from the browser. Speeds starting the develop server.`,
umbrellaIssue: `https://github.com/gatsbyjs/gatsby/discussions/27603`,
},
{
name: `PARALLEL_SOURCING`,
env: `GATSBY_EXPERIMENTAL_PARALLEL_SOURCING`,
command: `all`,
telemetryId: `ParallelSourcing`,
experimental: true,
description: `Run all source plugins at the same time instead of serially. For sites with multiple source plugins, this can speedup sourcing and transforming considerably.`,
umbrellaIssue: `https://github.com/gatsbyjs/gatsby/discussions/28336`,
},
]

export default activeFlags