Skip to content

Commit

Permalink
Create queue configurations for build/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Moocar committed Mar 27, 2019
1 parent e85d723 commit 60d1241
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby/src/internal-plugins/query-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const processQueries = async (queryJobs, { activity }) => {
}
const startQueries = process.hrtime()

const queue = queryQueue.create()
const queue = queryQueue.createBuild()
queue.on(`task_finish`, () => {
const stats = queue.getStats()
activity.setStatus(
Expand Down Expand Up @@ -222,7 +222,7 @@ const processQueries = async (queryJobs, { activity }) => {
* For what constitutes a dirty query, see `calcDirtyQueryIds`
*/
const startDaemon = () => {
const queue = queryQueue.create()
const queue = queryQueue.createDaemon()

const runQueuedActions = () => {
const state = store.getState()
Expand Down
60 changes: 49 additions & 11 deletions packages/gatsby/src/internal-plugins/query-runner/query-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ const { boundActionCreators } = require(`../../redux/actions`)
const websocketManager = require(`../../utils/websocket-manager`)
const FastMemoryStore = require(`./better-queue-custom-store`)

const makeOptions = ({ processing, waiting }) => {
const makeBaseOptions = () => {
return {
concurrent: 4,
// Merge duplicate jobs.
store: FastMemoryStore(),
}
}

const pageQueryRun = (queryJob, result) => {
// Send event that the page query finished.
boundActionCreators.pageQueryRun({
path: queryJob.id,
componentPath: queryJob.componentPath,
isPage: queryJob.isPage,
})
}

const makeDaemonOptions = ({ processing, waiting }) => {
return {
merge: (oldTask, newTask, cb) => {
cb(null, newTask)
},
Expand All @@ -32,13 +46,22 @@ const makeOptions = ({ processing, waiting }) => {
cb(null, job)
}
},
store: FastMemoryStore(),
}
}

const create = () => {
/**
* Creates a queue that is optimized for running as a daemon during
* gatsby develop. It pushes query result changes to the
* websock-manager so they can be used by the running develop app. It
* also ensures that concurrent user changes are not lost.
*/
const createDaemon = () => {
const processing = new Set()
const waiting = new Map()
const queueOptions = {
...makeBaseOptions(),
...makeDaemonOptions({ processing, waiting }),
}
const queue = new Queue((queryJob, callback) => {
const component = store.getState().components[queryJob.component]
return queryRunner({ queryJob, component })
Expand All @@ -52,20 +75,35 @@ const create = () => {
}

// Send event that the page query finished.
boundActionCreators.pageQueryRun({
path: queryJob.id,
componentPath: queryJob.componentPath,
isPage: queryJob.isPage,
})
pageQueryRun(queryJob, result)

return callback(null, result)
},
error => callback(error)
)
}, makeOptions({ processing, waiting }))
}, queueOptions)
return queue
}

const createBuild = () => {
const queueOptions = makeBaseOptions()
const queue = new Queue((queryJob, callback) => {
const component = store.getState().components[queryJob.component]
return queryRunner({ queryJob, component })
.catch(e => console.log(`Error running queryRunner`, e))
.then(
result => {
// Send event that the page query finished.
pageQueryRun(queryJob, result)
return callback(null, result)
},
error => callback(error)
)
}, queueOptions)
return queue
}

module.exports = {
create,
createDaemon,
createBuild,
}

0 comments on commit 60d1241

Please sign in to comment.