mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
fix: clean up failed and ongoing backups
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user