-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Queue page requests to wp endpoints (#4735)
* Wrap better-queue with Promise syntax that resolves when the queue drains * Queue requests for wp objects. Added config to set concurreny of the requests * Use concurrency instead of batchSize * Fix passing the config to the getPages function Fix how options are passed in to better-queue
- Loading branch information
1 parent
24bd7a8
commit fe55f6d
Showing
4 changed files
with
123 additions
and
13 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/gatsby-source-wordpress/src/__tests__/request-in-queue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
jest.mock(`axios`) | ||
|
||
const requestInQueue = require(`../request-in-queue`) | ||
const axios = require(`axios`) | ||
|
||
axios.mockImplementation(opts => { | ||
if (opts.throw) { throw new Error(opts.throw) } | ||
|
||
return opts.url.slice(opts.url.lastIndexOf(`/`) + 1) | ||
}) | ||
|
||
describe(`requestInQueue`, () => { | ||
let requests | ||
|
||
beforeEach(() => { | ||
requests = [ | ||
{ method: `get`, url: `https://gatsbyjs.org/1` }, | ||
{ method: `get`, url: `https://gatsbyjs.org/2` }, | ||
{ method: `get`, url: `https://gatsbyjs.org/3` }, | ||
{ method: `get`, url: `https://gatsbyjs.org/4` }, | ||
] | ||
}) | ||
|
||
afterEach(() => { | ||
axios.mockClear() | ||
}) | ||
|
||
it(`runs all requests in queue`, async () => { | ||
await requestInQueue(requests) | ||
|
||
requests.forEach((req) => { | ||
expect(axios).toHaveBeenCalledWith(req) | ||
}) | ||
}) | ||
|
||
it(`returns the values in the same order they were requested`, async () => { | ||
const responses = await requestInQueue(requests) | ||
expect(responses).toEqual([`1`, `2`, `3`, `4`]) | ||
}) | ||
|
||
it(`stops any requests when one throws an error`, async () => { | ||
try { | ||
await requestInQueue([{ throw: `error` }, ...requests]) | ||
} catch (err) { | ||
expect(err).toBeDefined() | ||
} | ||
expect(axios).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const Queue = require(`better-queue`) | ||
const Promise = require(`bluebird`) | ||
const request = require(`axios`) | ||
|
||
const _defaults = { | ||
id: `url`, | ||
} | ||
|
||
/** | ||
* [handleQueue description] | ||
* @param {[type]} task [description] | ||
* @param {Function} cb [description] | ||
* @return {[type]} [description] | ||
*/ | ||
async function handleQueue(task, cb) { | ||
try { | ||
const response = await request(task) | ||
cb(null, response) | ||
} catch (err) { | ||
cb(err) | ||
} | ||
} | ||
|
||
/** | ||
* @typedef {Options} | ||
* @type {Object} | ||
* @see For a detailed descriptions of the options, | ||
* see {@link https://www.npmjs.com/package/better-queue#full-documentation|better-queue on Github} | ||
*/ | ||
|
||
/** | ||
* Run a series of requests tasks in a queue for better flow control | ||
* | ||
* @param {Object[]} tasks An array of Axios formatted request objects | ||
* @param {Options} opts Options that will be given to better-queue | ||
* @return {Promise} Resolves with the accumulated values from the tasks | ||
*/ | ||
module.exports = function requestInQueue (tasks, opts = {}) { | ||
return new Promise((res, rej) => { | ||
const q = new Queue(handleQueue, { ..._defaults, ...opts }) | ||
|
||
const taskMap = new Map(tasks.map((t) => { | ||
q.push(t) | ||
return [t.url, null] | ||
})) | ||
|
||
q.on(`task_failed`, (id, err) => { | ||
rej(`${id} failed with err: ${err}`) | ||
q.destroy() | ||
}) | ||
|
||
q.on(`task_finish`, (id, response) => { | ||
taskMap.set(id, response) | ||
}) | ||
|
||
q.on(`drain`, () => res(Array.from(taskMap.values()))) | ||
}) | ||
} |