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

[WIP] feat(gatsby): worker-pool for custom GraphQL field resolvers #10938

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f5c4334
first shot at parallel workers
Moocar Dec 18, 2018
f4d816c
stuff
Moocar Dec 18, 2018
7b3fca9
async getNode
Moocar Dec 18, 2018
25a524d
asyncPlugin
Moocar Dec 18, 2018
e6beb71
fix pluginFields
Moocar Dec 18, 2018
aef1e43
use jest-worker for IPC
Moocar Dec 19, 2018
8596eda
remove console.logs
Moocar Dec 19, 2018
e7cbeac
jest workers work!
Moocar Dec 19, 2018
6863ca8
Merge branch 'master' into parallel-sketch
Moocar Jan 8, 2019
242420c
store resolvers against their type, not just fieldName
Moocar Jan 8, 2019
a7559d5
fixed up rpc api
Moocar Jan 8, 2019
fa5ec9f
replace isAsync with workerPlugin
Moocar Jan 8, 2019
3cf872e
document jest-worker IPC code 3 in async/worker code
Moocar Jan 8, 2019
1508298
change async-resolvers -> worker-resolvers
Moocar Jan 8, 2019
a889664
error checking
Moocar Jan 8, 2019
38e0370
resolver-worker docs and error checking
Moocar Jan 8, 2019
70e7aaa
docs
Moocar Jan 9, 2019
c5655ec
use @moocar/jest-worker
Moocar Jan 9, 2019
a77c1d4
yarn.lock for @moocar/jest-worker
Moocar Jan 9, 2019
7a86ee1
end worker pool after bootstrap
Moocar Jan 9, 2019
f629eaa
Merge branch 'master' into parallel-sketch
Moocar Jan 9, 2019
6767039
introduce getCache mocker
Moocar Jan 9, 2019
d50efb2
fix unsupported properties, and general cleanup
Moocar Jan 9, 2019
21d5aba
use plugin.name for cache in resolver-worker
Moocar Jan 11, 2019
98fbec2
Merge branch 'master' into parallel-sketch
Moocar Jan 21, 2019
15099d3
Merge branch 'master' into parallel-sketch
Moocar Jan 30, 2019
61b0168
Merge branch 'master' into parallel-sketch
Moocar Feb 4, 2019
0aca069
step.toString() in markdown benchmark
Moocar Feb 5, 2019
21bfc69
Merge branch 'master' into parallel-sketch
Moocar Feb 12, 2019
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
Prev Previous commit
Next Next commit
fixed up rpc api
  • Loading branch information
Moocar committed Jan 8, 2019
commit a7559d50a55a294afb5fea3b0c5015811c34e405
36 changes: 21 additions & 15 deletions packages/gatsby/src/schema/async-resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ const path = require(`path`)
const Worker = require(`jest-worker`).default
const { store } = require(`../redux`)
const invariant = require(`invariant`)
const { getNode, getNodesByType } = require(`../db/nodes`)
const nodesAPI = require(`../db/nodes`)
const reporter = require(`gatsby-cli/lib/reporter`)

let fields = []
let pool

function reporterHandler({ fnName, args }) {
reporter[fnName].apply(null, args)
}

const rpcMethods = {
getNode,
getNodesByType,
...nodesAPI,
reporter: reporterHandler,
}

function ipcCallback(child, request) {
Expand All @@ -19,14 +24,16 @@ function ipcCallback(child, request) {
invariant(rpc, `rpc`)
const { name, args, id } = rpc
invariant(name, `rpc name`)
invariant(id, `rpc id`)
const response = rpcMethods[name].apply(null, args)
const replyMessage = {
id,
type: `response`,
response,
// Only respond if id is present (this means the message was an RPC)
if (id) {
const replyMessage = {
id,
type: `response`,
response,
}
child.send([3, replyMessage])
}
child.send([3, replyMessage])
}

function getPlugin(pluginName) {
Expand All @@ -42,25 +49,24 @@ function makeFieldSetup({ fieldConfig, typeName, fieldName }) {
invariant(typeName, `typeName`)
invariant(fieldName, `fieldName`)
const { pluginName } = fieldConfig
let pathPrefix = ``
if (store.getState().program.prefixPaths) {
pathPrefix = store.getState().config.pathPrefix
}
const plugin = getPlugin(pluginName)
const resolverFile = path.join(plugin.resolve, `gatsby-node.js`)
invariant(resolverFile, `new resolver asyncFile`)
const type = { name: typeName }
return {
fieldName,
resolverFile,
pathPrefix,
type,
pluginOptions: plugin.pluginOptions,
}
}

function makePool(fields) {
const setupArgs = [{ fields: fields.map(makeFieldSetup) }]
let pathPrefix = ``
if (store.getState().program.prefixPaths) {
pathPrefix = store.getState().config.pathPrefix
}
const setupArgs = [{ pathPrefix, fields: fields.map(makeFieldSetup) }]
const exposedMethods = [`execResolver`]
const workerOptions = {
ipcCallback,
Expand Down
106 changes: 67 additions & 39 deletions packages/gatsby/src/schema/resolver-worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const _ = require(`lodash`)
const invariant = require(`invariant`)
const uuidv4 = require(`uuid/v4`)
const Cache = require(`../utils/cache`)
const _ = require(`lodash`)
const createContentDigest = require(`../utils/create-content-digest`)
const reporter = require(`gatsby-cli/lib/reporter`)

const types = {}
const rpcs = new Map()
Expand Down Expand Up @@ -59,68 +61,94 @@ function makeRpc(fnName) {
}

function makeReporter() {
return {
table: (...args) => console.log(`table`, args),
step: (...args) => console.log(`step`, args),
inspect: (...args) => console.log(`inspect`, args),
list: (...args) => console.log(`list`, args),
header: (...args) => console.log(`header`, args),
footer: (...args) => console.log(`footer`, args),
log: (...args) => console.log(`log`, args),
success: (...args) => console.log(`success`, args),
error: (...args) => console.log(`error`, args),
info: (...args) => console.log(`info`, args),
command: (...args) => console.log(`command`, args),
warn: (...args) => console.log(`warn`, args),
question: (...args) => console.log(`question`, args),
tree: (...args) => console.log(`tree`, args),
activitySet: (...args) => console.log(`activitySet`, args),
activity: (...args) => console.log(`activity`, args),
select: (...args) => console.log(`select`, args),
progress: (...args) => console.log(`progress`, args),
close: (...args) => console.log(`close`, args),
createReporter: (...args) => console.log(`createReporter`, args),
panic: (...args) => console.log(`panic`, args),
panicOnBuild: (...args) => console.log(`panicOnBuild`, args),
uptime: (...args) => console.log(`uptime`, args),
return Object.assign(reporter, {
panicOnBuild(...args) {
// panicOnBuild will send the panic message to the parent
// process, which will terminate, thus killing this and all
// other workers. All other reporter functions operate locally
const msg = {
name: `reporter`,
args: { fnName: `panicOnBuild`, args },
}
process.send([3, msg])
},
})
}

function makeRpcs(o, rpcNames) {
for (const rpcName of rpcNames) {
o[rpcName] = makeRpc(rpcName)
}
return o
}

function makeRpcs() {
return {
getNode: makeRpc(`getNode`),
getNodesByType: makeRpc(`getNodesByType`),
function unsupportedFn(name) {
return () => {
throw new Error(`API [${name}] is unsupported in parallel resolver`)
}
}

function makeUnsupportedProps(o, props) {
for (const prop of props) {
Object.defineProperty(o, prop, unsupportedFn(prop))
}
return o
}

function makeApi(type, pathPrefix) {
const cache = new Cache({ name: `some cache` }).init()
const api = {
cache,
createContentDigest,
// TODO pass in plugin.name into worker so this can mimic api-runner-node
// createNodeId: namespacedCreateNodeId,
// TODO remember to remove from unsupported
reporter: makeReporter(),
type,
pathPrefix,
}
makeUnsupportedProps(api, [
`boundActionCreators`,
`createNodeId`,
`actions`,
`store`,
`emitter`,
`tracing`,
`getNodes`,
])
makeRpcs(api, [
`loadNodeContent`,
`getNode`,
`getNodesByType`,
`hasNodeChanged`,
`getNodeAndSavePathDependency`,
])
return api
}

function storeResolver(typeName, fieldName, fieldConfig) {
_.set(types, [typeName, `fields`, fieldName], fieldConfig)
}

async function initModule({
fieldName,
resolverFile,
pluginOptions,
type,
async function initModule(
pathPrefix,
}) {
{ fieldName, resolverFile, pluginOptions, type }
) {
invariant(resolverFile, `asyncFile`)
invariant(pluginOptions, `pluginOptions`)
const module = require(resolverFile)
const cache = new Cache({ name: `some cache` }).init()
const api = { type, pathPrefix, cache, ...makeRpcs() }
const api = makeApi(type, pathPrefix)
const newFields = await module.setFieldsOnGraphQLNodeType(api, pluginOptions)
_.forEach(newFields, (fieldConfig, fieldName) => {
storeResolver(type.name, fieldName, fieldConfig)
})
}

async function setup(args) {
const { fields } = args
const { pathPrefix, fields } = args
invariant(fields, `setup fields`)
for (const field of fields) {
await initModule(field)
await initModule(pathPrefix, field)
}
}

Expand Down