-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1492 from argos-ci/cache-conclusion-stats
feat: cache conclusion and stats in database
- Loading branch information
Showing
29 changed files
with
396 additions
and
232 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
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,19 @@ | ||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
export const up = async (knex) => { | ||
await knex.schema.alterTable("builds", async (table) => { | ||
table.enum("conclusion", ["no-changes", "changes-detected"]); | ||
table.jsonb("stats"); | ||
}); | ||
}; | ||
|
||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
export const down = async (knex) => { | ||
await knex.schema.alterTable("builds", async (table) => { | ||
table.dropColumn("conclusion"); | ||
table.dropColumn("stats"); | ||
}); | ||
}; |
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
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,41 @@ | ||
#!/usr/bin/env node | ||
import { callbackify } from "node:util"; | ||
|
||
import { Build } from "@/database/models/index.js"; | ||
import logger from "@/logger/index.js"; | ||
|
||
import { concludeBuildsJob } from "../conclude-job"; | ||
|
||
const main = callbackify(async () => { | ||
const batch = 500; | ||
const totalCount = await Build.query() | ||
.where("jobStatus", "complete") | ||
.whereNull("conclusion") | ||
.resultSize(); | ||
|
||
let total = 0; | ||
for (let offset = 0; offset < totalCount; offset += batch) { | ||
const nodes = await Build.query() | ||
.where("jobStatus", "complete") | ||
.whereNull("conclusion") | ||
.limit(batch) | ||
.offset(offset) | ||
.orderBy("id", "desc"); | ||
|
||
const ids = nodes.map((node) => node.id); | ||
const percentage = Math.round((total / totalCount) * 100); | ||
logger.info( | ||
`Processing ${total}/${totalCount} (${percentage}%) - Pushing ${ids.length} builds in queue`, | ||
); | ||
await concludeBuildsJob.push(...ids); | ||
total += nodes.length; | ||
} | ||
|
||
logger.info(`${total} builds pushed in queue (100% complete)`); | ||
}); | ||
|
||
main((err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); |
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 @@ | ||
import { createJob } from "@/job-core/index.js"; | ||
import logger from "@/logger/index.js"; | ||
|
||
import { concludeBuild } from "./concludeBuild.js"; | ||
|
||
export const concludeBuildsJob = createJob("conclude-builds", { | ||
complete: () => {}, | ||
error: (value, error) => { | ||
console.error("Error while processing build", value, error); | ||
}, | ||
perform: async (buildId: string) => { | ||
logger.info(`Concluding build ${buildId}`); | ||
await concludeBuild({ buildId }); | ||
logger.info(`Build ${buildId} concluded`); | ||
}, | ||
}); |
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,50 @@ | ||
import { assertNever } from "@argos/util/assertNever"; | ||
import { invariant } from "@argos/util/invariant"; | ||
|
||
import { job as buildNotificationJob } from "@/build-notification/job.js"; | ||
import { transaction } from "@/database/index.js"; | ||
import { Build, BuildConclusion, BuildNotification } from "@/database/models"; | ||
|
||
/** | ||
* Concludes the build by updating the conclusion and the stats. | ||
* Called when all diffs are processed. | ||
*/ | ||
export async function concludeBuild(input: { buildId: string }) { | ||
const { buildId } = input; | ||
const statuses = await Build.getScreenshotDiffsStatuses([buildId]); | ||
const [[conclusion], [stats]] = await Promise.all([ | ||
Build.getConclusions([buildId], statuses), | ||
Build.getStats([buildId]), | ||
]); | ||
invariant(stats !== undefined, "No stats found"); | ||
invariant(conclusion !== undefined, "No conclusion found"); | ||
// If the build is not yet concluded, we don't want to update it. | ||
if (conclusion === null) { | ||
return; | ||
} | ||
const [, buildNotification] = await transaction(async (trx) => { | ||
return Promise.all([ | ||
Build.query(trx).findById(buildId).patch({ | ||
conclusion, | ||
stats, | ||
}), | ||
BuildNotification.query(trx).insert({ | ||
buildId, | ||
type: getNotificationType(conclusion), | ||
jobStatus: "pending", | ||
}), | ||
]); | ||
}); | ||
await buildNotificationJob.push(buildNotification.id); | ||
} | ||
|
||
function getNotificationType(conclusion: BuildConclusion) { | ||
switch (conclusion) { | ||
case "changes-detected": | ||
return "diff-detected"; | ||
case "no-changes": | ||
return "no-diff-detected"; | ||
default: | ||
return assertNever(conclusion); | ||
} | ||
} |
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
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
Oops, something went wrong.