fix: clean up failed and ongoing backups

This commit is contained in:
charles-gauthereau
2026-06-24 08:32:40 +02:00
parent c7d10efefe
commit e31245b492
2 changed files with 18 additions and 4 deletions
-1
View File
@@ -108,7 +108,6 @@ export const PATCH = withAgentCheck(async (request: Request, {params, agent}: {
}) => { }) => {
try { try {
const body: BodyPatch = await request.json(); const body: BodyPatch = await request.json();
log.info({data: body}, "Body from PATH in backup route");
const status = body.status const status = body.status
const backupId = body.backupId const backupId = body.backupId
+18 -3
View File
@@ -8,21 +8,36 @@ const log = logger.child({module: "tasks/cleaning"});
export const backupCleanTask = async () => { export const backupCleanTask = async () => {
try { try {
const backups = await db.query.backup.findMany({ const ongoingBackups = await db.query.backup.findMany({
where: and( where: and(
isNotNull(drizzleDb.schemas.backup.deletedAt), isNotNull(drizzleDb.schemas.backup.deletedAt),
eq(drizzleDb.schemas.backup.status, "ongoing") eq(drizzleDb.schemas.backup.status, "ongoing")
) )
}); });
log.debug(`Backups to clean: ${backups.length}`); log.debug(`Backups to clean: ${ongoingBackups.length}`);
for (const backup of backups) { for (const backup of ongoingBackups) {
await db.update(drizzleDb.schemas.backup).set(withUpdatedAt({ await db.update(drizzleDb.schemas.backup).set(withUpdatedAt({
status: "failed", status: "failed",
})) }))
.where(eq(drizzleDb.schemas.backup.id, backup.id)); .where(eq(drizzleDb.schemas.backup.id, backup.id));
} }
const failedBackups = await db.query.backup.findMany({
where: and(
isNull(drizzleDb.schemas.backup.deletedAt),
eq(drizzleDb.schemas.backup.status, "failed")
)
});
log.debug(`Failed backups to clean: ${failedBackups.length}`);
for (const backup of failedBackups) {
await db.update(drizzleDb.schemas.backup).set(withUpdatedAt({
deletedAt: new Date(),
}))
.where(eq(drizzleDb.schemas.backup.id, backup.id));
}
} catch (e: any) { } catch (e: any) {
log.error({name: "backupCleanTask", error: e},`Backup cleanup failed`); log.error({name: "backupCleanTask", error: e},`Backup cleanup failed`);
throw e; throw e;