-
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.
feat(gatsby): enable Jobs API v2 (#19858)
Added a new action called createJobV2 to support the new api. createJob is still available to keep backward-compatibility. I'll add a deprecation message when creatJobV2 is fully operational. The createJobV2 needs a job object that contains 4 arguments name, inputPaths, outputDir & args. These args are used to create a unique content digest to make sure a job is deterministic. InputPaths are converted into relative paths before sending it to the worker as they need to be filesystem agnostic. More info on why can be found in #19831
- Loading branch information
Showing
28 changed files
with
1,210 additions
and
27 deletions.
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
e2e-tests/production-runtime/plugins/gatsby-plugin-local-worker/gatsby-node.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,16 @@ | ||
const path = require(`path`) | ||
|
||
exports.onPostBootstrap = async ({ actions, store }) => { | ||
const result = await actions.createJobV2({ | ||
name: `TEST_JOB`, | ||
inputPaths: [], | ||
outputDir: path.join(store.getState().program.directory, `./public`), | ||
args: { | ||
result: `hello`, | ||
}, | ||
}) | ||
|
||
if (result.result !== `hello`) { | ||
throw new Error(`the result of our worker is wrong`) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
e2e-tests/production-runtime/plugins/gatsby-plugin-local-worker/gatsby-worker.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,9 @@ | ||
const sleep = timeout => new Promise(resolve => setTimeout(resolve, timeout)) | ||
|
||
exports.TEST_JOB = async ({ args }) => { | ||
await sleep(5000) | ||
|
||
return { | ||
result: args.result, | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
e2e-tests/production-runtime/plugins/gatsby-plugin-local-worker/index.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 @@ | ||
// noop |
4 changes: 4 additions & 0 deletions
4
e2e-tests/production-runtime/plugins/gatsby-plugin-local-worker/package.json
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,4 @@ | ||
{ | ||
"name": "gatsby-plugin-local-worker", | ||
"version": "1.0.0" | ||
} |
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
33 changes: 33 additions & 0 deletions
33
packages/gatsby/src/bootstrap/__tests__/__snapshots__/remove-stale-jobs.js.snap
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,33 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`remove-stale-jobs should enqueue pending jobs 1`] = ` | ||
Array [ | ||
undefined, | ||
] | ||
`; | ||
|
||
exports[`remove-stale-jobs should remove stale jobs from complete cache 1`] = ` | ||
Array [ | ||
Object { | ||
"payload": Object { | ||
"contentDigest": "1234", | ||
}, | ||
"plugin": undefined, | ||
"traceId": undefined, | ||
"type": "REMOVE_STALE_JOB_V2", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`remove-stale-jobs should remove stale jobs from pending cache 1`] = ` | ||
Array [ | ||
Object { | ||
"payload": Object { | ||
"contentDigest": "1234", | ||
}, | ||
"plugin": undefined, | ||
"traceId": undefined, | ||
"type": "REMOVE_STALE_JOB_V2", | ||
}, | ||
] | ||
`; |
86 changes: 86 additions & 0 deletions
86
packages/gatsby/src/bootstrap/__tests__/remove-stale-jobs.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,86 @@ | ||
jest.mock(`../../utils/jobs-manager`) | ||
|
||
const { isJobStale } = require(`../../utils/jobs-manager`) | ||
const { internalActions, publicActions } = require(`../../redux/actions`) | ||
|
||
jest.spyOn(internalActions, `removeStaleJob`) | ||
|
||
const removeStaleJobs = require(`../remove-stale-jobs`) | ||
|
||
describe(`remove-stale-jobs`, () => { | ||
let state | ||
|
||
beforeEach(() => { | ||
state = { | ||
jobsV2: { | ||
complete: new Map(), | ||
incomplete: new Map(), | ||
}, | ||
} | ||
|
||
publicActions.createJobV2 = jest.fn() | ||
internalActions.removeStaleJob.mockClear() | ||
}) | ||
|
||
it(`should remove stale jobs from complete cache`, () => { | ||
const job = { | ||
inputPaths: [`/src/myfile.js`], | ||
} | ||
|
||
state.jobsV2.complete.set(`1234`, job) | ||
|
||
isJobStale.mockReturnValue(true) | ||
|
||
expect(removeStaleJobs(state)).toMatchSnapshot() | ||
expect(internalActions.removeStaleJob).toHaveBeenCalledTimes(1) | ||
expect(internalActions.removeStaleJob).toHaveBeenCalledWith(`1234`) | ||
expect(publicActions.createJobV2).not.toHaveBeenCalled() | ||
}) | ||
|
||
it(`should remove stale jobs from pending cache`, () => { | ||
const data = { | ||
job: { | ||
inputPaths: [`/src/myfile.js`], | ||
contentDigest: `1234`, | ||
}, | ||
plugin: { | ||
name: `test`, | ||
version: `1.0.0`, | ||
}, | ||
} | ||
|
||
state.jobsV2.incomplete.set(`1234`, data) | ||
|
||
isJobStale.mockReturnValue(true) | ||
|
||
expect(removeStaleJobs(state)).toMatchSnapshot() | ||
expect(internalActions.removeStaleJob).toHaveBeenCalledTimes(1) | ||
expect(internalActions.removeStaleJob).toHaveBeenCalledWith(`1234`) | ||
expect(publicActions.createJobV2).not.toHaveBeenCalled() | ||
}) | ||
|
||
it(`should enqueue pending jobs`, () => { | ||
const data = { | ||
job: { | ||
inputPaths: [`/src/myfile.js`], | ||
contentDigest: `1234`, | ||
}, | ||
plugin: { | ||
name: `test`, | ||
version: `1.0.0`, | ||
}, | ||
} | ||
|
||
state.jobsV2.incomplete.set(`1234`, data) | ||
|
||
isJobStale.mockReturnValue(false) | ||
|
||
expect(removeStaleJobs(state)).toMatchSnapshot() | ||
expect(internalActions.removeStaleJob).toHaveBeenCalledTimes(0) | ||
expect(publicActions.createJobV2).toHaveBeenCalledTimes(1) | ||
expect(publicActions.createJobV2).toHaveBeenCalledWith( | ||
data.job, | ||
data.plugin | ||
) | ||
}) | ||
}) |
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,25 @@ | ||
const { isJobStale } = require(`../utils/jobs-manager`) | ||
const { publicActions, internalActions } = require(`../redux/actions`) | ||
|
||
module.exports = state => { | ||
const actions = [] | ||
|
||
// If any of our finished jobs are stale we remove them to keep our cache small | ||
state.jobsV2.complete.forEach((job, contentDigest) => { | ||
if (isJobStale(job)) { | ||
actions.push(internalActions.removeStaleJob(contentDigest)) | ||
} | ||
}) | ||
|
||
// If any of our pending jobs do not have an existing inputPath or the inputPath changed | ||
// we remove it from the queue as they would fail anyway | ||
state.jobsV2.incomplete.forEach(({ job, plugin }) => { | ||
if (isJobStale(job)) { | ||
actions.push(internalActions.removeStaleJob(job.contentDigest)) | ||
} else { | ||
actions.push(publicActions.createJobV2(job, plugin)) | ||
} | ||
}) | ||
|
||
return actions | ||
} |
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
Oops, something went wrong.