feat: added cron job to remove healthcheck logs older than 12h

This commit is contained in:
charles-gauthereau
2026-03-25 21:11:09 +01:00
parent a9d6481193
commit 7a4af4e1e0
6 changed files with 74 additions and 20 deletions
+41
View File
@@ -0,0 +1,41 @@
import {db} from "@/db";
import * as drizzleDb from "@/db";
import {and, eq, gte, lt} from "drizzle-orm";
export async function getHealthLast12hLogs({id}: { id: string }) {
const now = new Date()
const since = new Date(now.getTime() - 12 * 60 * 60 * 1000)
return db
.select()
.from(drizzleDb.schemas.healthcheckLog)
.where(
and(
eq(drizzleDb.schemas.healthcheckLog.objectId, id),
gte(drizzleDb.schemas.healthcheckLog.date, since)
)
)
}
export async function deleteHealthLogsOlderThan12h() {
const now = new Date()
const threshold = new Date(now.getTime() - 12 * 60 * 60 * 1000)
const logsToDelete = await db
.select()
.from(drizzleDb.schemas.healthcheckLog)
.where(
lt(drizzleDb.schemas.healthcheckLog.date, threshold)
)
console.log(`Number of logs found to delete: ${logsToDelete.length}`)
await db
.delete(drizzleDb.schemas.healthcheckLog)
.where(
lt(drizzleDb.schemas.healthcheckLog.date, threshold)
)
return logsToDelete.length
}