feat: add Job Monitor page with cron job logging and Selenium queue status
This commit is contained in:
61
apps/Backend/src/storage/cron-job-log-storage.ts
Normal file
61
apps/Backend/src/storage/cron-job-log-storage.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { prisma as db } from "@repo/db/client";
|
||||
|
||||
export type CronJobStatus = "success" | "failed" | "skipped";
|
||||
|
||||
export interface CronJobLogEntry {
|
||||
id: number;
|
||||
jobName: string;
|
||||
status: string;
|
||||
startedAt: Date;
|
||||
completedAt: Date | null;
|
||||
durationMs: number | null;
|
||||
errorMessage: string | null;
|
||||
}
|
||||
|
||||
export const cronJobLogStorage = {
|
||||
async createJobLog(
|
||||
jobName: string,
|
||||
startedAt: Date
|
||||
): Promise<CronJobLogEntry> {
|
||||
return db.cronJobLog.create({
|
||||
data: { jobName, status: "running", startedAt },
|
||||
});
|
||||
},
|
||||
|
||||
async completeJobLog(
|
||||
id: number,
|
||||
status: CronJobStatus,
|
||||
completedAt: Date,
|
||||
errorMessage?: string
|
||||
): Promise<CronJobLogEntry> {
|
||||
const durationMs = completedAt.getTime() - (await db.cronJobLog.findUniqueOrThrow({ where: { id } })).startedAt.getTime();
|
||||
return db.cronJobLog.update({
|
||||
where: { id },
|
||||
data: { status, completedAt, durationMs, errorMessage: errorMessage ?? null },
|
||||
});
|
||||
},
|
||||
|
||||
async getRecentLogs(limit = 50): Promise<CronJobLogEntry[]> {
|
||||
return db.cronJobLog.findMany({
|
||||
orderBy: { startedAt: "desc" },
|
||||
take: limit,
|
||||
});
|
||||
},
|
||||
|
||||
async getLastRunPerJob(): Promise<CronJobLogEntry[]> {
|
||||
// Get the most recent log entry for each distinct jobName
|
||||
const jobs = await db.cronJobLog.findMany({
|
||||
distinct: ["jobName"],
|
||||
orderBy: { startedAt: "desc" },
|
||||
});
|
||||
return jobs;
|
||||
},
|
||||
|
||||
async getFailedLogs(limit = 20): Promise<CronJobLogEntry[]> {
|
||||
return db.cronJobLog.findMany({
|
||||
where: { status: "failed" },
|
||||
orderBy: { startedAt: "desc" },
|
||||
take: limit,
|
||||
});
|
||||
},
|
||||
};
|
||||
@@ -16,6 +16,7 @@ import { cloudStorageStorage } from './cloudStorage-storage';
|
||||
import { paymentsReportsStorage } from './payments-reports-storage';
|
||||
import { patientDocumentsStorage } from './patientDocuments-storage';
|
||||
import * as exportPaymentsReportsStorage from "./export-payments-reports-storage";
|
||||
import { cronJobLogStorage } from "./cron-job-log-storage";
|
||||
|
||||
|
||||
export const storage = {
|
||||
@@ -34,7 +35,8 @@ export const storage = {
|
||||
...cloudStorageStorage,
|
||||
...paymentsReportsStorage,
|
||||
...patientDocumentsStorage,
|
||||
...exportPaymentsReportsStorage,
|
||||
...exportPaymentsReportsStorage,
|
||||
...cronJobLogStorage,
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user