fix: backups that were never run manually are not picked up by job

This commit is contained in:
Nicolas Meienberger 2025-12-20 11:14:38 +01:00
parent 24fd18825a
commit cbc9da0e26
2 changed files with 29 additions and 2 deletions

View file

@ -4,6 +4,7 @@ import { createTestVolume } from "~/test/helpers/volume";
import { createTestBackupSchedule } from "~/test/helpers/backup";
import { createTestRepository } from "~/test/helpers/repository";
import { generateBackupOutput } from "~/test/helpers/restic";
import { faker } from "@faker-js/faker";
import * as spawnModule from "~/server/utils/spawn";
const resticBackupMock = mock(() => Promise.resolve({ exitCode: 0, stdout: "", stderr: "" }));
@ -150,3 +151,26 @@ describe("execute backup", () => {
expect(updatedSchedule.lastBackupStatus).toBe("error");
});
});
describe("getSchedulesToExecute", () => {
test("should return schedules with NULL lastBackupStatus", async () => {
// arrange
const volume = await createTestVolume();
const repository = await createTestRepository();
const schedule = await createTestBackupSchedule({
volumeId: volume.id,
repositoryId: repository.id,
enabled: true,
cronExpression: "* * * * *",
lastBackupStatus: null,
nextBackupAt: faker.date.past().getTime(),
});
// act
const schedulesToExecute = await backupsService.getSchedulesToExecute();
// assert
expect(schedulesToExecute).toContain(schedule.id);
});
});

View file

@ -1,4 +1,4 @@
import { and, asc, eq, ne } from "drizzle-orm";
import { and, asc, eq, isNull, ne, or } from "drizzle-orm";
import cron from "node-cron";
import { CronExpressionParser } from "cron-parser";
import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced";
@ -393,7 +393,10 @@ const executeBackup = async (scheduleId: number, manual = false) => {
const getSchedulesToExecute = async () => {
const now = Date.now();
const schedules = await db.query.backupSchedulesTable.findMany({
where: and(eq(backupSchedulesTable.enabled, true), ne(backupSchedulesTable.lastBackupStatus, "in_progress")),
where: and(
eq(backupSchedulesTable.enabled, true),
or(ne(backupSchedulesTable.lastBackupStatus, "in_progress"), isNull(backupSchedulesTable.lastBackupStatus)),
),
});
const schedulesToRun: number[] = [];