mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
fix: refactoring logs and adding pino logger for production and dev. (#236)
This commit is contained in:
@@ -2,6 +2,9 @@ import {db} from "@/db";
|
||||
import {and, eq, isNotNull, isNull} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {withUpdatedAt} from "@/db/utils";
|
||||
import {logger} from "@/lib/logger";
|
||||
|
||||
const log = logger.child({module: "tasks/cleaning"});
|
||||
|
||||
export const backupCleanTask = async () => {
|
||||
try {
|
||||
@@ -11,8 +14,7 @@ export const backupCleanTask = async () => {
|
||||
eq(drizzleDb.schemas.backup.status, "ongoing")
|
||||
)
|
||||
});
|
||||
|
||||
console.log(`Backups to clean: ${backups.length}`);
|
||||
log.info(`Backups to clean: ${backups.length}`);
|
||||
|
||||
for (const backup of backups) {
|
||||
await db.update(drizzleDb.schemas.backup).set(withUpdatedAt({
|
||||
@@ -22,7 +24,7 @@ export const backupCleanTask = async () => {
|
||||
}
|
||||
|
||||
} catch (e: any) {
|
||||
console.error("Backup cleanup failed:", e);
|
||||
log.info({name: "backupCleanTask", error: e},`Backup cleanup failed`);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
@@ -5,7 +5,9 @@ import {enforceRetentionGFS} from "@/lib/tasks/database/retention-gsf";
|
||||
import {retentionPolicy} from "@/db/schema/07_database";
|
||||
import {isNull} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {logger} from "@/lib/logger";
|
||||
|
||||
const log = logger.child({module: "tasks/database"});
|
||||
|
||||
export const retentionCleanTask = async () => {
|
||||
try {
|
||||
@@ -17,13 +19,13 @@ export const retentionCleanTask = async () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
console.log(`Retention databases number: ${databases.length}`);
|
||||
log.info(`Retention databases number: ${databases.length}`);
|
||||
for (const db of databases) {
|
||||
if (!db.retentionPolicy) continue;
|
||||
await enforceRetention(db.id, db.retentionPolicy);
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error("Retention cleanup failed:", e);
|
||||
log.error({error: e},"Retention cleanup failed");
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
@@ -32,7 +34,9 @@ export async function enforceRetention(
|
||||
databaseId: string,
|
||||
policy: typeof retentionPolicy.$inferSelect
|
||||
) {
|
||||
console.log(`Retention started for ${databaseId}`);
|
||||
;
|
||||
log.info({name: "enforceRetention"},`Retention started for ${databaseId}`);
|
||||
|
||||
switch (policy.type) {
|
||||
case "count":
|
||||
await enforceRetentionCount(databaseId, policy.count ?? 7);
|
||||
|
||||
@@ -2,10 +2,12 @@ import * as drizzleDb from "@/db";
|
||||
import {db} from "@/db";
|
||||
import {and, desc, eq, isNull} from "drizzle-orm";
|
||||
import {deleteBackupCronAction} from "@/lib/tasks/database/utils/delete";
|
||||
import {toast} from "sonner";
|
||||
import {logger} from "@/lib/logger";
|
||||
|
||||
const log = logger.child({module: "tasks/database/retention-count"});
|
||||
|
||||
export async function enforceRetentionCount(databaseId: string, count: number) {
|
||||
console.log(`[Retention Count] - ${databaseId} : started`);
|
||||
log.info({ name: "enforceRetentionCount"}, `Retention count started for databaseId: ${databaseId}`);
|
||||
const backups = await db.query.backup.findMany({
|
||||
where: and(eq(drizzleDb.schemas.backup.databaseId, databaseId), isNull(drizzleDb.schemas.backup.deletedAt)),
|
||||
orderBy: desc(drizzleDb.schemas.backup.createdAt),
|
||||
@@ -19,7 +21,7 @@ export async function enforceRetentionCount(databaseId: string, count: number) {
|
||||
});
|
||||
|
||||
const toDelete = backups.slice(count);
|
||||
console.log(`[Retention Count] - ${databaseId} : ${toDelete.length} backups to delete`);
|
||||
log.info({ name: "enforceRetentionCount"}, `Found ${toDelete.length} backups to delete for databaseId: ${databaseId}`);
|
||||
|
||||
for (const b of toDelete) {
|
||||
const result = await deleteBackupCronAction({
|
||||
@@ -29,9 +31,9 @@ export async function enforceRetentionCount(databaseId: string, count: number) {
|
||||
|
||||
const inner = result?.data;
|
||||
if (inner?.success) {
|
||||
console.log(`[Retention Count] - (databaseId:${b.databaseId}) - (backupId: ${b.id}) : successfully deleted`);
|
||||
log.info({ name: "enforceRetentionCount"}, `(databaseId:${b.databaseId}) - (backupId: ${b.id}) : successfully deleted`);
|
||||
} else {
|
||||
console.log(`[Retention Count] - (databaseId:${b.databaseId}) - (backupId: ${b.id}) : an error occurred - ${inner?.actionError?.message}`);
|
||||
log.info({ name: "enforceRetentionCount"}, `(databaseId:${b.databaseId}) - (backupId: ${b.id}) : an error occurred - ${inner?.actionError?.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,13 @@ import {db} from "@/db";
|
||||
import {eq, lt, and, desc, isNull} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {deleteBackupCronAction} from "@/lib/tasks/database/utils/delete";
|
||||
import {logger} from "@/lib/logger";
|
||||
|
||||
const log = logger.child({module: "tasks/database/retention-days"});
|
||||
|
||||
export async function enforceRetentionDays(databaseId: string, days: number) {
|
||||
console.log(`Enforce Retention Days starting for ${databaseId}`);
|
||||
log.info({ name: "enforceRetentionDays"}, `Enforce Retention Days starting for ${databaseId}`);
|
||||
|
||||
const cutoff = new Date(Date.now() - days * 86400000);
|
||||
|
||||
const expiredBackups = await db.query.backup.findMany({
|
||||
@@ -31,9 +35,9 @@ export async function enforceRetentionDays(databaseId: string, days: number) {
|
||||
|
||||
const inner = result?.data;
|
||||
if (inner?.success) {
|
||||
console.log(`[Retention Days] - (databaseId:${backup.databaseId}) - (backupId: ${backup.id}) : successfully deleted`);
|
||||
log.info({ name: "enforceRetentionDays"}, `(databaseId:${backup.databaseId}) - (backupId: ${backup.id}) : successfully deleted`);
|
||||
} else {
|
||||
console.log(`[Retention Days] - (databaseId:${backup.databaseId}) - (backupId: ${backup.id}) : an error occurred - ${inner?.actionError?.message}`);
|
||||
log.info({ name: "enforceRetentionDays"}, `(databaseId:${backup.databaseId}) - (backupId: ${backup.id}) : an error occurred - ${inner?.actionError?.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ import {subDays, subWeeks, subMonths, subYears, startOfWeek, startOfMonth, start
|
||||
import {eq, desc, isNull, and} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {deleteBackupCronAction} from "@/lib/tasks/database/utils/delete";
|
||||
import {logger} from "@/lib/logger";
|
||||
|
||||
const log = logger.child({module: "tasks/database/retention-gsf"});
|
||||
|
||||
export async function enforceRetentionGFS(databaseId: string, gfsSettings: {
|
||||
daily: number;
|
||||
@@ -10,7 +13,7 @@ export async function enforceRetentionGFS(databaseId: string, gfsSettings: {
|
||||
monthly: number;
|
||||
yearly: number;
|
||||
}) {
|
||||
console.log(`Enforce Retention GFS starting for ${databaseId}`);
|
||||
log.info({ name: "enforceRetentionGFS"}, `Retention GFS started for databaseId: ${databaseId}`);
|
||||
|
||||
const backups = await db.query.backup.findMany({
|
||||
where: and(eq(drizzleDb.schemas.backup.databaseId, databaseId), isNull(drizzleDb.schemas.backup.deletedAt)),
|
||||
@@ -69,9 +72,9 @@ export async function enforceRetentionGFS(databaseId: string, gfsSettings: {
|
||||
|
||||
const inner = result?.data;
|
||||
if (inner?.success) {
|
||||
console.log(`[Retention GFS] - (databaseId:${b.databaseId}) - (backupId: ${b.id}) : successfully deleted`);
|
||||
log.info({ name: "enforceRetentionGFS"}, `(databaseId:${b.databaseId}) - (backupId: ${b.id}) : successfully deleted`);
|
||||
} else {
|
||||
console.log(`[Retention GFS] - (databaseId:${b.databaseId}) - (backupId: ${b.id}) : an error occurred - ${inner?.actionError?.message}`);
|
||||
log.info({ name: "enforceRetentionGFS"}, `(databaseId:${b.databaseId}) - (backupId: ${b.id}) : an error occurred - ${inner?.actionError?.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-8
@@ -7,41 +7,44 @@ import {
|
||||
checkDatabasesHealthError,
|
||||
deleteHealthLogsOlderThan12h
|
||||
} from "@/db/services/healthcheck";
|
||||
import {logger} from "@/lib/logger";
|
||||
|
||||
const log = logger.child({module: "tasks"});
|
||||
|
||||
export const retentionJob = cron.schedule(env.RETENTION_CRON, async () => {
|
||||
try {
|
||||
console.log("Retention Job : Starting task");
|
||||
log.info({ job: "cron", action: "start", name: "retentionJob" }, "Retention Job started");
|
||||
await retentionCleanTask();
|
||||
} catch (err) {
|
||||
console.error(`[CRON] Error:`, err);
|
||||
log.error({ job: "cron", name: "retentionJob", error: err }, "Retention Job Error");
|
||||
}
|
||||
});
|
||||
|
||||
export const cleaningJob = cron.schedule("* * * * *", async () => {
|
||||
try {
|
||||
console.log("Cleaning Job : Starting task");
|
||||
log.info({ job: "cron", action: "start", name: "cleaningJob" }, "Cleaning Job started");
|
||||
await backupCleanTask();
|
||||
} catch (err) {
|
||||
console.error(`[CRON] Error:`, err);
|
||||
log.error({ job: "cron", name: "cleaningJob", error: err }, "Cleaning Job Error");
|
||||
}
|
||||
});
|
||||
|
||||
export const cleaningHealthcheckLogsJob = cron.schedule(env.CLEANING_HEALTHCHECK_LOGS_CRON, async () => {
|
||||
try {
|
||||
console.log("Cleaning Healthcheck Logs Job : Starting task");
|
||||
log.info({ job: "cron", action: "start", name: "cleaningHealthcheckLogsJob" }, "Cleaning Health Logs Job started");
|
||||
await deleteHealthLogsOlderThan12h();
|
||||
} catch (err) {
|
||||
console.error(`[CRON] Error:`, err);
|
||||
log.error({ job: "cron", name: "cleaningHealthcheckLogsJob", error: err }, "Cleaning Health Logs Job Error");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const healthcheckAgentAndDatabaseJob = cron.schedule(env.HEALTHCHECK_CRON, async () => {
|
||||
try {
|
||||
console.log("Healthcheck Job : Starting task");
|
||||
log.info({ job: "cron", action: "start", name: "healthcheckAgentAndDatabaseJob" }, "Healthcheck Jobs started");
|
||||
await checkAgentsHealthError();
|
||||
await checkDatabasesHealthError()
|
||||
} catch (err) {
|
||||
console.error(`[CRON] Error:`, err);
|
||||
log.error({ job: "cron", name: "healthcheckAgentAndDatabaseJob", error: err }, "Healthcheck Jobs Error");
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user