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");
|
||||
}
|
||||
Reference in New Issue
Block a user