Skip to content

Commit

Permalink
fix: retry failed performance scans
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Mar 1, 2024
1 parent ed32bed commit 745f8fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/puppeteer/tasks/lighthouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const runLighthouseTask: PuppeteerTask = async (props) => {
}
}

let report = samples[0]
let report: LH.Result = samples[0]
if (samples.length > 1) {
try {
report = computeMedianRun(samples)
Expand All @@ -153,6 +153,12 @@ export const runLighthouseTask: PuppeteerTask = async (props) => {
routeReport.tasks.runLighthouseTask = 'failed'
}

if (report.categories.performance && !report.categories.performance.score) {
logger.warn(`Lighthouse failed to performance checks for "${routeReport.route.path}", adding back to queue.`)
routeReport.tasks.runLighthouseTask = 'failed-retry'
return routeReport
}

// we need to export all base64 data to improve the stability of the client
if (report.audits?.['final-screenshot']?.details?.data)
await fs.writeFile(join(routeReport.artifactPath, ReportArtifacts.screenshot), base64ToBuffer(report.audits['final-screenshot'].details.data))
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/puppeteer/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function createUnlighthouseWorker(tasks: Record<UnlighthouseTask, T

const routeReports = new Map<string, UnlighthouseRouteReport>()
const ignoredRoutes = new Set<string>()
const retriedRoutes = new Set<string>()

const monitor: () => UnlighthouseWorkerStats = () => {
const now = Date.now()
Expand Down Expand Up @@ -166,6 +167,16 @@ export async function createUnlighthouseWorker(tasks: Record<UnlighthouseTask, T
}
if (response.tasks[taskName] === 'failed')
return
if (response.tasks[taskName] === 'failed-retry') {
// only requeue each report once
if (!retriedRoutes.has(id)) {
retriedRoutes.add(id)
routeReports.delete(id)
queueRoute(routeReport.route)
}
return
}


response.tasks[taskName] = 'completed'
routeReports.set(id, response)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export type UnlighthouseTask = 'inspectHtmlTask' | 'runLighthouseTask'
/**
* Each task ran by unlighthouse (extractHtmlPayload, runLighthouseTask) has a specific status which we can expose.
*/
export type UnlighthouseTaskStatus = 'waiting' | 'in-progress' | 'completed' | 'failed' | 'ignore'
export type UnlighthouseTaskStatus = 'waiting' | 'in-progress' | 'completed' | 'failed' | 'ignore' | 'failed-retry'

/**
* A fairly rigid representation of the puppeteer cluster task results (extractHtmlPayload, runLighthouseTask), combined
Expand Down

0 comments on commit 745f8fe

Please sign in to comment.