From 88a76d2ccf66c744f989e353aa7f47ca5dca7e17 Mon Sep 17 00:00:00 2001 From: Javi Marti Date: Thu, 6 Feb 2025 01:19:16 +0100 Subject: [PATCH] feat: add clean zombie builds scripts --- Dockerfile | 4 +++- package.json | 3 ++- scripts/zombie.ts | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 scripts/zombie.ts diff --git a/Dockerfile b/Dockerfile index c5251d9..7b27a3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ COPY ./package.json /usr/local/app/ COPY ./package-lock.json /usr/local/app/ COPY ./src /usr/local/app/src COPY ./prisma /usr/local/app/prisma +COPY ./scripts /usr/local/app/scripts RUN npm install RUN npm run build @@ -16,6 +17,7 @@ WORKDIR /usr/local/app COPY --from=build /usr/local/app/node_modules /usr/local/app/node_modules COPY --from=build /usr/local/app/dist /usr/local/app/dist COPY --from=build /usr/local/app/prisma /usr/local/app/prisma +COPY --from=build /usr/local/app/scripts /usr/local/app/scripts RUN apt-get update && apt-get install -y git @@ -28,4 +30,4 @@ RUN npx prisma generate EXPOSE 8080 -CMD npx prisma migrate deploy && node dist/index.mjs \ No newline at end of file +CMD npx prisma migrate deploy && npm run zombie && node dist/index.mjs \ No newline at end of file diff --git a/package.json b/package.json index f536ce2..51745a5 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ }, "scripts": { "start": "tsx watch src/index.ts", - "build": "pkgroll ." + "build": "pkgroll .", + "zombie": "tsx scripts/zombie.ts" }, "author": "", "license": "ISC" diff --git a/scripts/zombie.ts b/scripts/zombie.ts new file mode 100644 index 0000000..8657615 --- /dev/null +++ b/scripts/zombie.ts @@ -0,0 +1,25 @@ +// This script cleans any zombie builds that are left in BUILDING status + +import { PrismaClient } from "@prisma/client"; + +const prisma = new PrismaClient(); + +const clearZombieBuilds = async () => { + const builds = await prisma.build.findMany({ where: { status: "BUILDING" } }); + + await prisma.$transaction( + builds.map((build) => + prisma.build.update({ + where: { id: build.id }, + data: { status: "FAILED", finishedAt: build.updatedAt }, + }) + ) + ); + + return builds.length; +}; + +clearZombieBuilds().then((builds) => { + console.log(`Sucessfully fixed ${builds} builds`); + process.exit(0); +});