feat: add PostgreSQL persistence for permanent shareable links
- Add PostgreSQL service to docker-compose with health checks
- Add migrate service that runs prisma migrate deploy on startup
- Integrate Prisma client in worker: checks for existing generations
(same repo+commit) before regenerating, writes to Postgres on completion
- Update /api/docs/[id] with Redis → PostgreSQL fallback + cache repopulation
- Update Dockerfile: prisma generate in build, copy Prisma engine to worker/web
- Add @codeboard/database dependency to web and worker packages
- Add initial SQL migration for Generation and User tables
- Change removeOnComplete to { age: 3600 } for job retention
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { Job } from "bullmq";
|
||||
import IORedis from "ioredis";
|
||||
import { prisma } from "@codeboard/database";
|
||||
import { cloneRepository } from "./jobs/clone.js";
|
||||
import { parseRepository } from "./jobs/parse.js";
|
||||
import { generateDocs } from "./jobs/generate.js";
|
||||
@@ -38,6 +39,33 @@ export async function processGenerationJob(
|
||||
try {
|
||||
await updateProgress(generationId, "CLONING", 10, "Cloning repository...");
|
||||
const cloneResult = await cloneRepository(repoUrl);
|
||||
const commitHash = cloneResult.metadata.lastCommit;
|
||||
|
||||
const existingGeneration = await prisma.generation.findUnique({
|
||||
where: {
|
||||
repoUrl_commitHash: {
|
||||
repoUrl,
|
||||
commitHash
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (existingGeneration && existingGeneration.result) {
|
||||
const docs = existingGeneration.result as any;
|
||||
docs.id = generationId;
|
||||
docs.repoUrl = repoUrl;
|
||||
docs.repoName = existingGeneration.repoName;
|
||||
|
||||
await redis.set(
|
||||
`codeboard:result:${generationId}`,
|
||||
JSON.stringify(docs),
|
||||
"EX",
|
||||
86400
|
||||
);
|
||||
|
||||
await updateProgress(generationId, "COMPLETED", 100, "Using cached documentation!");
|
||||
return { generationId, duration: 0, repoName: existingGeneration.repoName, cached: true };
|
||||
}
|
||||
|
||||
await updateProgress(
|
||||
generationId,
|
||||
@@ -72,9 +100,22 @@ export async function processGenerationJob(
|
||||
86400
|
||||
);
|
||||
|
||||
await prisma.generation.create({
|
||||
data: {
|
||||
id: generationId,
|
||||
repoUrl,
|
||||
repoName: cloneResult.metadata.name,
|
||||
commitHash,
|
||||
status: "COMPLETED",
|
||||
progress: 100,
|
||||
result: docs as any,
|
||||
duration
|
||||
}
|
||||
});
|
||||
|
||||
await updateProgress(generationId, "COMPLETED", 100, "Done!");
|
||||
|
||||
return { generationId, duration, repoName: cloneResult.metadata.name };
|
||||
return { generationId, duration, repoName: cloneResult.metadata.name, cached: false };
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Unknown error";
|
||||
await updateProgress(generationId, "FAILED", 0, message);
|
||||
|
||||
Reference in New Issue
Block a user