Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

fix: resolves plans in db and cron schedule drifting issue

towfiqi88ff487d4503
diff --git a/backend/__tests__/services/PlanService.test.ts b/backend/__tests__/services/PlanService.test.ts
index 18f053e..06e4cc9 100644
--- a/backend/__tests__/services/PlanService.test.ts
+++ b/backend/__tests__/services/PlanService.test.ts
@@ -1,6 +1,3 @@
-import fs from 'fs';
-import * as fsPromises from 'fs/promises';
-import { Readable } from 'stream';
import { PlanService } from '../../src/services/PlanService';
import { PlanStore } from '../../src/stores/PlanStore';
import { StorageStore } from '../../src/stores/StorageStore';
@@ -8,7 +5,6 @@ import { DeviceStore } from '../../src/stores/DeviceStore';
import { RestoreStore } from '../../src/stores/RestoreStore';
import { BaseBackupManager } from '../../src/managers/BaseBackupManager';
import { NewPlanReq, PlanBackupSettings } from '../../src/types/plans';
-import { NotFoundError } from '../../src/utils/AppError';
import { initializeLogger } from '../../src/utils/logger';
// Mock the dependencies using Jest's mocking factory
@@ -78,7 +74,7 @@ describe('PlanService', () => {
// This prevents the background task from running and crashing other tests.
performBackupSpy = jest
.spyOn(planService, 'performBackup')
- .mockImplementation(() => Promise.resolve());
+ .mockImplementation(() => Promise.resolve(''));
});
afterEach(() => {
@@ -913,4 +909,195 @@ describe('PlanService', () => {
});
});
});
+
+ // -------------------------------------------------
+ // Tests for reconciling schedules with the database
+ // -------------------------------------------------
+
+ describe('reconcileSchedules', () => {
+ let mockCronManager: {
+ getSchedules: jest.Mock;
+ removeSchedule: jest.Mock;
+ };
+
+ beforeEach(() => {
+ mockCronManager = {
+ getSchedules: jest.fn(),
+ removeSchedule: jest.fn(),
+ };
+ // Attach the mock cronManager to the localAgent
+ (mockLocalAgent as any).cronManager = mockCronManager;
+ });
+
+ const makePlan = (id: string, overrides: Record<string, any> = {}) =>
+ ({
+ id,
+ title: `Plan ${id}`,
+ sourceId: 'main',
+ storageId: 'storage-123',
+ storagePath: '/backups/test',
+ isActive: true,
+ settings: {
+ interval: { type: 'daily', time: '10:00AM' },
+ },
+ ...overrides,
+ }) as any;
+
+ it('should remove orphaned schedules not present in the database', async () => {
+ // Arrange: CronManager has a schedule for plan-orphan, but DB has no plans
+ const schedulesMap = new Map();
+ schedulesMap.set('plan-orphan', [{ type: 'backup', options: {} }]);
+ mockCronManager.getSchedules.mockResolvedValue(schedulesMap);
+ mockCronManager.removeSchedule.mockResolvedValue(undefined);
+ mockPlanStore.getDevicePlans.mockResolvedValue([]);
+
+ // Act
+ await planService.reconcileSchedules();
+
+ // Assert
+ expect(mockCronManager.removeSchedule).toHaveBeenCalledWith('plan-orphan');
+ expect(mockCronManager.removeSchedule).toHaveBeenCalledTimes(1);
+ });
+
+ it('should recreate missing schedules for plans in the database', async () => {
+ // Arrange: DB has a plan but CronManager has no schedules
+ const plan = makePlan('plan-missing');
+ const schedulesMap = new Map();
+ mockCronManager.getSchedules.mockResolvedValue(schedulesMap);
+ mockPlanStore.getDevicePlans.mockResolvedValue([plan]);
+ mockStorageStore.getById.mockResolvedValue({
+ id: 'storage-123',
+ name: 'Test Storage',
+ type: 'local',
+ authType: 'none',
+ settings: {},
+ credentials: {},
+ defaultPath: '/',
+ } as any);
+ mockLocalAgent.createOrUpdateSchedules = jest.fn().mockResolvedValue(undefined);
+
+ // Act
+ await planService.reconcileSchedules();
+
+ // Assert
+ expect(mockLocalAgent.createOrUpdateSchedules).toHaveBeenCalledWith(
+ 'plan-missing',
+ expect.objectContaining({
+ id: 'plan-missing',
+ cronExpression: expect.any(String),
+ storage: expect.objectContaining({ name: 'Test Storage' }),
+ }),
+ 'create'
+ );
+ });
+
+ it('should make no changes when schedules and database are in sync', async () => {
+ // Arrange: DB and CronManager both have the same plan
+ const plan = makePlan('plan-synced');
+ const schedulesMap = new Map();
+ schedulesMap.set('plan-synced', [{ type: 'backup', options: {} }]);
+ mockCronManager.getSchedules.mockResolvedValue(schedulesMap);
+ mockPlanStore.getDevicePlans.mockResolvedValue([plan]);
+
+ // Act
+ await planService.reconcileSchedules();
+
+ // Assert: No removals or creations
+ expect(mockCronManager.removeSchedule).not.toHaveBeenCalled();
+ expect(mockLocalAgent.createOrUpdateSchedules).not.toHaveBeenCalled();
+ });
+
+ it('should skip plans without interval settings', async () => {
+ // Arrange: DB has a plan with no interval
+ const planNoInterval = makePlan('plan-no-interval', { settings: {} });
+ const schedulesMap = new Map();
+ mockCronManager.getSchedules.mockResolvedValue(schedulesMap);
+ mockPlanStore.getDevicePlans.mockResolvedValue([planNoInterval]);
+
+ // Act
+ await planService.reconcileSchedules();
+
+ // Assert: No schedule creation attempted
+ expect(mockLocalAgent.createOrUpdateSchedules).not.toHaveBeenCalled();
+ });
+
+ it('should skip plans whose storage cannot be resolved', async () => {
+ // Arrange: DB has a plan, but storage lookup fails
+ const plan = makePlan('plan-bad-storage');
+ const schedulesMap = new Map();
+ mockCronManager.getSchedules.mockResolvedValue(schedulesMap);
+ mockPlanStore.getDevicePlans.mockResolvedValue([plan]);
+ mockStorageStore.getById.mockResolvedValue(null); // Storage not found
+
+ // Act — should not throw
+ await planService.reconcileSchedules();
+
+ // Assert: No schedule creation attempted (getStorageDetails throws, caught inside)
+ expect(mockLocalAgent.createOrUpdateSchedules).not.toHaveBeenCalled();
+ });
+
+ it('should handle both orphans and missing schedules in one pass', async () => {
+ // Arrange: CronManager has orphan, DB has a plan missing from schedules
+ const plan = makePlan('plan-exists');
+ const schedulesMap = new Map();
+ schedulesMap.set('plan-deleted', [{ type: 'backup', options: {} }]);
+ mockCronManager.getSchedules.mockResolvedValue(schedulesMap);
+ mockCronManager.removeSchedule.mockResolvedValue(undefined);
+ mockPlanStore.getDevicePlans.mockResolvedValue([plan]);
+ mockStorageStore.getById.mockResolvedValue({
+ id: 'storage-123',
+ name: 'Test Storage',
+ type: 'local',
+ authType: 'none',
+ settings: {},
+ credentials: {},
+ defaultPath: '/',
+ } as any);
+ mockLocalAgent.createOrUpdateSchedules = jest.fn().mockResolvedValue(undefined);
+
+ // Act
+ await planService.reconcileSchedules();
+
+ // Assert: Both operations happened
+ expect(mockCronManager.removeSchedule).toHaveBeenCalledWith('plan-deleted');
+ expect(mockLocalAgent.createOrUpdateSchedules).toHaveBeenCalledWith(
+ 'plan-exists',
+ expect.objectContaining({ id: 'plan-exists' }),
+ 'create'
+ );
+ });
+
+ it('should continue reconciling other plans if one plan fails', async () => {
+ // Arrange: Two plans in DB, first one's storage lookup throws, second succeeds
+ const plan1 = makePlan('plan-fail', { storageId: 'storage-bad' });
+ const plan2 = makePlan('plan-ok', { storageId: 'storage-good' });
+ const schedulesMap = new Map();
+ mockCronManager.getSchedules.mockResolvedValue(schedulesMap);
+ mockPlanStore.getDevicePlans.mockResolvedValue([plan1, plan2]);
+ mockStorageStore.getById.mockImplementation(async (id: string) => {
+ if (id === 'storage-bad') return null; // Will cause getStorageDetails to throw
+ return {
+ id: 'storage-good',
+ name: 'Good Storage',
+ type: 'local',
+ authType: 'none',
+ settings: {},
+ credentials: {},
+ defaultPath: '/',
+ } as any;
+ });
+ mockLocalAgent.createOrUpdateSchedules = jest.fn().mockResolvedValue(undefined);
+
+ // Act — should not throw
+ await planService.reconcileSchedules();
+
+ // Assert: Only plan2's schedule was created (plan1 was skipped due to error)
+ expect(mockLocalAgent.createOrUpdateSchedules).toHaveBeenCalledTimes(1);
+ expect(mockLocalAgent.createOrUpdateSchedules).toHaveBeenCalledWith(
+ 'plan-ok',
+ expect.objectContaining({ id: 'plan-ok' }),
+ 'create'
+ );
+ });
+ });
});
diff --git a/backend/src/createApp.ts b/backend/src/createApp.ts
index a88220e..50504d8 100644
--- a/backend/src/createApp.ts
+++ b/backend/src/createApp.ts
@@ -238,6 +238,13 @@ export async function createApp(): Promise<{ app: Express }> {
res.sendFile(path.join(publicPath, 'index.html'));
});
+ // Reconcile CronManager schedules with the database (fixes drift on restart)
+ try {
+ await planService.reconcileSchedules();
+ } catch (error: any) {
+ console.error('[Startup] Schedule reconciliation failed:', error?.message);
+ }
+
// Start System task schedules
const systemTaskManager = SystemTaskManager.getInstance(SYSTEM_JOBS);
systemTaskManager.initialize();
diff --git a/backend/src/services/PlanService.ts b/backend/src/services/PlanService.ts
index b2c5307..10af83a 100644
--- a/backend/src/services/PlanService.ts
+++ b/backend/src/services/PlanService.ts
@@ -25,6 +25,7 @@ import { AppError, NotFoundError } from '../utils/AppError';
import { SourceTypes } from '../types/source';
import { sanitizeStoragePath } from '../utils/sanitizeStoragePath';
import { BackupNotification } from '../notifications/BackupNotification';
+import { ScheduleReconciler } from './ScheduleReconciler';
/**
* PlanService is the central orchestrator for all business logic related to backup plans.
@@ -32,6 +33,8 @@ import { BackupNotification } from '../notifications/BackupNotification';
* and delegates remote execution to strategies.
*/
export class PlanService {
+ protected scheduleReconciler: ScheduleReconciler;
+
constructor(
protected localAgent: BaseBackupManager,
protected planStore: PlanStore,
@@ -39,7 +42,13 @@ export class PlanService {
protected storageStore: StorageStore,
protected deviceStore: DeviceStore,
protected restoreStore: RestoreStore
- ) {}
+ ) {
+ this.scheduleReconciler = new ScheduleReconciler(
+ this.localAgent,
+ this.planStore,
+ this.storageStore
+ );
+ }
getStrategy(plan: NewPlan | Plan): BackupStrategy | any {
const isRemote = plan.sourceId !== 'main';
@@ -658,6 +667,10 @@ export class PlanService {
};
}
+ public async reconcileSchedules(): Promise<void> {
+ await this.scheduleReconciler.reconcileSchedules();
+ }
+
/**
* Resolves replication storage names from storageIds for a plan's replication settings.
*/
diff --git a/backend/src/services/ScheduleReconciler.ts b/backend/src/services/ScheduleReconciler.ts
new file mode 100644
index 0000000..35c2bad
--- /dev/null
+++ b/backend/src/services/ScheduleReconciler.ts
@@ -0,0 +1,139 @@
+import { BaseBackupManager } from '../managers/BaseBackupManager';
+import { PlanStore } from '../stores/PlanStore';
+import { StorageStore } from '../stores/StorageStore';
+import { Plan, NewPlan } from '../db/schema/plans';
+import { BackupPlanArgs } from '../types/plans';
+import { intervalToCron } from '../utils/intervalToCron';
+import { NotFoundError } from '../utils/AppError';
+
+/**
+ * Reconciles the CronManager schedules with the plans stored in the database.
+ * Removes orphaned schedules (present in CronManager but not in DB) and
+ * recreates missing schedules (present in DB but not in CronManager).
+ * Should be called once during application startup.
+ */
+
+export class ScheduleReconciler {
+ constructor(
+ protected localAgent: BaseBackupManager,
+ protected planStore: PlanStore,
+ protected storageStore: StorageStore
+ ) {}
+
+ public async reconcileSchedules(): Promise<void> {
+ const allPlans = await this.getReconciliationPlans();
+ const dbPlanIds = new Set(allPlans.map(plan => plan.id));
+ const dbPlanMap = new Map(allPlans.map(plan => [plan.id, plan]));
+
+ const schedulesMap = await this.localAgent.cronManager.getSchedules();
+ const scheduledIds = new Set(schedulesMap.keys());
+
+ const orphansRemoved = await this.removeOrphanedSchedules(scheduledIds, dbPlanIds);
+
+ let schedulesAdded = 0;
+ for (const [planId, plan] of dbPlanMap) {
+ if (scheduledIds.has(planId)) continue;
+
+ try {
+ if (await this.reconcileMissingSchedule(planId, plan)) {
+ schedulesAdded++;
+ }
+ } catch (error: any) {
+ console.error(
+ `[Reconciliation] Failed to recreate schedule for plan ${planId}: ${error?.message}`
+ );
+ }
+ }
+
+ if (orphansRemoved === 0 && schedulesAdded === 0) {
+ return;
+ }
+
+ console.log(
+ `[Reconciliation] Complete. Orphans removed: ${orphansRemoved}, Schedules added: ${schedulesAdded}.`
+ );
+ }
+
+ protected async getReconciliationPlans(): Promise<Plan[]> {
+ return (await this.planStore.getDevicePlans('main')) || [];
+ }
+
+ protected async removeOrphanedSchedules(
+ scheduledIds: Set<string>,
+ dbPlanIds: Set<string>
+ ): Promise<number> {
+ let orphansRemoved = 0;
+
+ for (const scheduledId of scheduledIds) {
+ if (!dbPlanIds.has(scheduledId)) {
+ try {
+ await this.localAgent.cronManager.removeSchedule(scheduledId);
+ orphansRemoved++;
+ console.log(`[Reconciliation] Removed orphaned schedule for plan ID: ${scheduledId}`);
+ } catch (error: any) {
+ console.error(
+ `[Reconciliation] Failed to remove orphaned schedule ${scheduledId}: ${error?.message}`
+ );
+ }
+ }
+ }
+
+ return orphansRemoved;
+ }
+
+ protected async buildReconciliationScheduleOptions(
+ planId: string,
+ plan: Plan | NewPlan
+ ): Promise<BackupPlanArgs | null> {
+ if (!plan.settings?.interval) {
+ console.warn(`[Reconciliation] Skipping plan ${planId} — no interval settings found.`);
+ return null;
+ }
+
+ const planStorage = await this.getStorageDetails(plan.storageId as string);
+ if (!planStorage.name || !planStorage.type) {
+ console.warn(`[Reconciliation] Skipping plan ${planId} — storage not found.`);
+ return null;
+ }
+
+ const cronExpression = intervalToCron(plan.settings.interval);
+ return {
+ ...plan,
+ storage: planStorage,
+ cronExpression,
+ };
+ }
+
+ protected async reconcileMissingSchedule(
+ planId: string,
+ plan: Plan | NewPlan
+ ): Promise<boolean> {
+ const backupScheduleOptions = await this.buildReconciliationScheduleOptions(planId, plan);
+ if (!backupScheduleOptions) {
+ return false;
+ }
+
+ await this.localAgent.createOrUpdateSchedules(planId, backupScheduleOptions, 'create');
+ console.log(
+ `[Reconciliation] Recreated missing schedule for plan "${plan.title}" (${planId})`
+ );
+
+ return true;
+ }
+
+ protected async getStorageDetails(storageId: string): Promise<BackupPlanArgs['storage']> {
+ const storage = await this.storageStore.getById(storageId);
+ if (!storage) {
+ throw new NotFoundError('Storage not found');
+ }
+
+ return {
+ name: storage.id === 'local' ? 'local' : storage.name,
+ type: storage.type as string,
+ authType: storage.authType as string,
+ settings: storage.settings as Record<string, string>,
+ credentials: storage.credentials as Record<string, string>,
+ defaultPath: storage.defaultPath as string,
+ };
+ }
+}
\ No newline at end of file