From 613dd33ac9bc53f237d508844fdcd5b1f471c47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Sun, 8 Sep 2024 22:58:47 +0200 Subject: [PATCH] fix: process build only once --- .../src/api/handlers/finalizeBuilds.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/apps/backend/src/api/handlers/finalizeBuilds.ts b/apps/backend/src/api/handlers/finalizeBuilds.ts index 9849f2be..88ea4f3f 100644 --- a/apps/backend/src/api/handlers/finalizeBuilds.ts +++ b/apps/backend/src/api/handlers/finalizeBuilds.ts @@ -67,25 +67,32 @@ export const finalizeBuilds: CreateAPIHandler = ({ post }) => { .where("builds.externalId", parallelNonce) .where("builds.totalBatch", -1); - await transaction(async (trx) => { - await Promise.all( + const finalized = await transaction(async (trx) => { + return Promise.all( builds.map(async (build) => { invariant(build.compareScreenshotBucket); + const willFinalize = !build.compareScreenshotBucket.complete; await Promise.all([ - !build.compareScreenshotBucket.complete - ? finalizeBuildService({ build, trx }) - : null, + willFinalize ? finalizeBuildService({ build, trx }) : null, // If the build was marked as partial, then it was obviously an error, we unmark it. build.partial ? Build.query(trx).where("id", build.id).patch({ partial: false }) : null, ]); + + return willFinalize; }), ); }); - await Promise.all(builds.map((build) => buildJob.push(build.id))); + await Promise.all( + builds.map(async (build, index) => { + if (finalized[index]) { + await buildJob.push(build.id); + } + }), + ); const buildResponses = await serializeBuilds(builds); res.send({ builds: buildResponses });