fix: sync build status from worker to MongoDB on download and list
- Download route now checks worker status if DB says queued/building - Builds list endpoint syncs stale builds with worker on fetch - Fixes: download button showing but APK 'not downloadable' - Fixes: builds staying stuck as 'queued' in DB after completion
This commit is contained in:
1 parent
c76eda9109
commit
d5ee698555
2 files changed
+60
-1
No files matched your search
@@ -22,7 +22,37 @@ export async function GET(
|
||||
}
|
||||
|
||||
if (build.status !== "completed") {
|
||||
return NextResponse.json({ error: "Build not ready" }, { status: 400 });
|
||||
// DB may be stale if WebSocket relayed status without syncing DB
|
||||
// Check the build worker directly and sync
|
||||
if (build.type === "apk" && (build.status === "queued" || build.status === "building")) {
|
||||
try {
|
||||
const workerRes = await fetch(`${BUILD_WORKER_URL}/build/${id}/status`);
|
||||
if (workerRes.ok) {
|
||||
const workerStatus = await workerRes.json();
|
||||
if (workerStatus.status === "completed") {
|
||||
await db.collection("builds").updateOne(
|
||||
{ _id: new ObjectId(id) },
|
||||
{ $set: { status: "completed", completedAt: new Date(), fileSize: workerStatus.fileSize, apkSize: workerStatus.fileSize, downloadUrl: `/api/build/${id}/download` } }
|
||||
);
|
||||
build.status = "completed";
|
||||
} else if (workerStatus.status === "failed") {
|
||||
await db.collection("builds").updateOne(
|
||||
{ _id: new ObjectId(id) },
|
||||
{ $set: { status: "failed", completedAt: new Date(), errorMessage: workerStatus.message } }
|
||||
);
|
||||
return NextResponse.json({ error: workerStatus.message || "Build failed" }, { status: 400 });
|
||||
} else {
|
||||
return NextResponse.json({ error: "Build still in progress" }, { status: 400 });
|
||||
}
|
||||
} else {
|
||||
return NextResponse.json({ error: "Build not ready" }, { status: 400 });
|
||||
}
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Build not ready" }, { status: 400 });
|
||||
}
|
||||
} else {
|
||||
return NextResponse.json({ error: "Build not ready" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
const projectName = (build.projectName as string) || "my-app";
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getSession } from "@/lib/auth";
|
||||
import { getDb } from "@/lib/db";
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
const BUILD_WORKER_URL = process.env.BUILD_WORKER_URL || "http://localhost:3001";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await getSession();
|
||||
@@ -27,6 +30,32 @@ export async function GET(req: NextRequest) {
|
||||
db.collection("builds").countDocuments(filter),
|
||||
]);
|
||||
|
||||
// Sync any queued/building APK builds with the worker
|
||||
for (const b of builds) {
|
||||
if (b.type === "apk" && (b.status === "queued" || b.status === "building")) {
|
||||
try {
|
||||
const workerRes = await fetch(`${BUILD_WORKER_URL}/build/${b._id.toString()}/status`);
|
||||
if (workerRes.ok) {
|
||||
const ws = await workerRes.json();
|
||||
if (ws.status !== b.status) {
|
||||
const update: Record<string, unknown> = { status: ws.status };
|
||||
if (ws.status === "completed") {
|
||||
update.completedAt = new Date();
|
||||
update.fileSize = ws.fileSize;
|
||||
update.apkSize = ws.fileSize;
|
||||
update.downloadUrl = `/api/build/${b._id}/download`;
|
||||
} else if (ws.status === "failed") {
|
||||
update.completedAt = new Date();
|
||||
update.errorMessage = ws.message;
|
||||
}
|
||||
await db.collection("builds").updateOne({ _id: b._id }, { $set: update });
|
||||
Object.assign(b, update);
|
||||
}
|
||||
}
|
||||
} catch { /* worker unreachable */ }
|
||||
}
|
||||
}
|
||||
|
||||
const data = builds.map((b) => ({
|
||||
_id: b._id.toString(),
|
||||
projectId: b.projectId,
|
||||
|
||||
Reference in new issue
Block a user