feat: initial scaffold - backend, frontend, Prisma schema, Docker
- Prisma schema: User, Session, RepoConfig, Preview, Job, WebhookToken, NoConfigComment, AdminSettings - Backend: auth (login/logout/me/first-user setup), webhook handler with HMAC verification, EC2 service, SSH service, deploy pipeline, job queue worker, cron workers - Frontend: Login with first-user detection, Dashboard, PreviewDetail with live log streaming, Settings, Repos config, Admin panel, SetupWizard, Privacy page - Docker Compose and Dockerfile for self-hosted deployment - Uses bcryptjs for Node 24 compatibility Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import cron from "node-cron";
|
||||
import { prisma } from "../lib/db";
|
||||
import { createLogger } from "../lib/logger";
|
||||
import { getAdminSettings } from "../lib/adminSettings";
|
||||
|
||||
const log = createLogger("CRON");
|
||||
|
||||
export function startCronWorkers() {
|
||||
// Inactivity check every 30 minutes
|
||||
cron.schedule("*/30 * * * *", async () => {
|
||||
try {
|
||||
await checkInactivity();
|
||||
} catch (e) {
|
||||
log.error({ e }, "Inactivity check error");
|
||||
}
|
||||
});
|
||||
|
||||
// Daily cleanup
|
||||
cron.schedule("0 3 * * *", async () => {
|
||||
try {
|
||||
await dailyCleanup();
|
||||
} catch (e) {
|
||||
log.error({ e }, "Daily cleanup error");
|
||||
}
|
||||
});
|
||||
|
||||
log.info("Cron workers started");
|
||||
}
|
||||
|
||||
async function checkInactivity() {
|
||||
const settings = await getAdminSettings();
|
||||
const now = new Date();
|
||||
|
||||
const running = await prisma.preview.findMany({
|
||||
where: { status: "RUNNING" },
|
||||
});
|
||||
|
||||
for (const preview of running) {
|
||||
const inactivityMs = settings.maxConcurrentInstancesPerUser; // will use actual inactivityHours from repoConfig
|
||||
const repoConfig = await prisma.repoConfig.findUnique({ where: { id: preview.repoConfigId } });
|
||||
if (!repoConfig) continue;
|
||||
|
||||
const deadline = new Date(preview.lastActivityAt.getTime() + repoConfig.inactivityHours * 3600 * 1000);
|
||||
if (now >= deadline) {
|
||||
log.info({ previewId: preview.id }, "Preview inactive, enqueuing INACTIVITY_STOP");
|
||||
await prisma.job.create({
|
||||
data: {
|
||||
previewId: preview.id,
|
||||
type: "INACTIVITY_STOP",
|
||||
status: "PENDING",
|
||||
payload: {},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function dailyCleanup() {
|
||||
const settings = await getAdminSettings();
|
||||
const cutoff = new Date(Date.now() - settings.previewRetentionDays * 86400 * 1000);
|
||||
|
||||
const old = await prisma.preview.findMany({
|
||||
where: {
|
||||
status: { in: ["STOPPED", "FAILED"] },
|
||||
stoppedAt: { lt: cutoff },
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
for (const { id } of old) {
|
||||
await prisma.job.deleteMany({ where: { previewId: id } });
|
||||
await prisma.preview.delete({ where: { id } });
|
||||
}
|
||||
|
||||
if (old.length > 0) log.info({ count: old.length }, "Cleaned up old previews");
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { prisma } from "../lib/db";
|
||||
import { createLogger } from "../lib/logger";
|
||||
import { runDeploy, stopPreview, signalAbort } from "../services/deploy";
|
||||
|
||||
const log = createLogger("JOB_WORKER");
|
||||
let running = false;
|
||||
|
||||
export async function startJobWorker() {
|
||||
if (running) return;
|
||||
running = true;
|
||||
log.info("Job worker started");
|
||||
|
||||
await resetStuckJobs();
|
||||
pollLoop();
|
||||
}
|
||||
|
||||
async function resetStuckJobs() {
|
||||
const count = await prisma.job.updateMany({
|
||||
where: { status: "RUNNING" },
|
||||
data: { status: "PENDING", startedAt: null },
|
||||
});
|
||||
if (count.count > 0) log.info({ count: count.count }, "Reset stuck running jobs to PENDING");
|
||||
}
|
||||
|
||||
async function pollLoop() {
|
||||
while (running) {
|
||||
try {
|
||||
await processPendingJobs();
|
||||
} catch (e) {
|
||||
log.error({ e }, "Job worker poll error");
|
||||
}
|
||||
await sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
const activePreviewJobs = new Map<number, number>();
|
||||
|
||||
async function processPendingJobs() {
|
||||
const pending = await prisma.job.findMany({
|
||||
where: { status: "PENDING" },
|
||||
orderBy: { createdAt: "asc" },
|
||||
take: 20,
|
||||
});
|
||||
|
||||
for (const job of pending) {
|
||||
const previewId = job.previewId;
|
||||
if (!previewId) continue;
|
||||
|
||||
if (activePreviewJobs.has(previewId)) {
|
||||
const existingJobId = activePreviewJobs.get(previewId)!;
|
||||
|
||||
if (job.type === "DEPLOY") {
|
||||
log.info({ previewId, newJobId: job.id, abortingJobId: existingJobId }, "New deploy cancels existing");
|
||||
signalAbort(previewId);
|
||||
await sleep(500);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
activePreviewJobs.set(previewId, job.id);
|
||||
|
||||
processJob(job).finally(() => {
|
||||
if (activePreviewJobs.get(previewId) === job.id) {
|
||||
activePreviewJobs.delete(previewId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function processJob(job: { id: number; type: string; previewId: number | null; payload: any }) {
|
||||
log.info({ jobId: job.id, type: job.type, previewId: job.previewId }, "Processing job");
|
||||
|
||||
try {
|
||||
if (job.type === "DEPLOY") {
|
||||
await runDeploy(job.id);
|
||||
} else if (job.type === "STOP" || job.type === "INACTIVITY_STOP") {
|
||||
if (job.previewId) {
|
||||
await prisma.job.update({ where: { id: job.id }, data: { status: "RUNNING", startedAt: new Date() } });
|
||||
await stopPreview(job.previewId);
|
||||
await prisma.job.update({ where: { id: job.id }, data: { status: "DONE", finishedAt: new Date() } });
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
log.error({ e, jobId: job.id }, "Job processing error");
|
||||
await prisma.job.update({
|
||||
where: { id: job.id },
|
||||
data: { status: "FAILED", error: e.message, finishedAt: new Date() },
|
||||
}).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
export function stopJobWorker() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise(r => setTimeout(r, ms));
|
||||
}
|
||||
Reference in New Issue
Block a user