Skip to content

Commit

Permalink
fix(gatsby): Add back an activity for jobs (#34061)
Browse files Browse the repository at this point in the history
* Add back an activity for jobs

* chore: format

* after format fixes + add total getter

* fix tests

* feat: add job type to progress label, so each job type has separate progress bar (#34095)

Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
3 people committed Nov 30, 2021
1 parent 8922762 commit af39171
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/gatsby-cli/src/reporter/reporter-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,14 @@ export const createProgressReporter = ({
},

set total(value: number) {
unflushedTotal = value
total = unflushedTotal = value
updateProgress()
},

get total(): number {
return total
},

span,
}
}
8 changes: 8 additions & 0 deletions packages/gatsby/src/utils/jobs/__tests__/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jest.mock(`p-defer`, () =>
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
phantomActivity: jest.fn(),
createProgress: jest.fn(),
warn: jest.fn(),
}
})
Expand Down Expand Up @@ -89,6 +90,13 @@ describe(`Jobs manager`, () => {
end: endActivity,
}
})
reporter.createProgress.mockImplementation(() => {
return {
start: jest.fn(),
tick: jest.fn(),
end: jest.fn(),
}
})

jest.isolateModules(() => {
jobManager = require(`../manager`)
Expand Down
34 changes: 32 additions & 2 deletions packages/gatsby/src/utils/jobs/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ export function createInternalJob(
return internalJob
}

const activitiesForJobTypes = new Map<
string,
ReturnType<typeof reporter.createProgress>
>()

/**
* Creates a job
*/
Expand All @@ -271,6 +276,22 @@ export async function enqueueJob(
activityForJobs!.start()
}

const jobType = `${job.plugin.name}.${job.name}`

let activityForJobsProgress = activitiesForJobTypes.get(jobType)

if (!activityForJobsProgress) {
activityForJobsProgress = reporter.createProgress(
`Running ${jobType} jobs`,
1,
0
)
activityForJobsProgress.start()
activitiesForJobTypes.set(jobType, activityForJobsProgress)
} else {
activityForJobsProgress.total++
}

const deferred = pDefer<Record<string, unknown>>()
jobsInProcess.set(job.contentDigest, {
id: job.id,
Expand All @@ -296,6 +317,8 @@ export async function enqueueJob(
// eslint-disable-next-line require-atomic-updates
activityForJobs = null
}

activityForJobsProgress.tick()
}

return deferred.promise
Expand All @@ -320,10 +343,17 @@ export function removeInProgressJob(contentDigest: string): void {
/**
* Wait for all processing jobs to have finished
*/
export function waitUntilAllJobsComplete(): Promise<void> {
return hasActiveJobs ? hasActiveJobs.promise : Promise.resolve()
export async function waitUntilAllJobsComplete(): Promise<void> {
await (hasActiveJobs ? hasActiveJobs.promise : Promise.resolve())
for (const progressActivity of activitiesForJobTypes.values()) {
progressActivity.end()
}
activitiesForJobTypes.clear()
}

/**
* Wait for specific jobs for engines
*/
export async function waitJobs(jobDigests: Set<string>): Promise<void> {
const promises: Array<Promise<any>> = []
for (const [digest, job] of jobsInProcess) {
Expand Down

0 comments on commit af39171

Please sign in to comment.