A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
chore: use the reported event time for end timestamps when present
towfiqiec0648b2e574
diff --git a/backend/__tests__/services/BackupEventService.test.ts b/backend/__tests__/services/BackupEventService.test.tsindex b37454c..703a69d 100644--- a/backend/__tests__/services/BackupEventService.test.ts+++ b/backend/__tests__/services/BackupEventService.test.ts@@ -313,6 +313,46 @@ describe('BackupEventService', () => {}));});++ it('should skip a replayed completion for an already finalized backup', async () => {+ // Arrange+ mockBackupStore.getById.mockResolvedValue({+ id: backupId,+ status: 'completed',+ inProgress: false,+ } as any);+ const completeEvent = { planId, backupId, success: true, summary: mockCompletionStats };++ // Act+ await backupEventService.onBackupComplete(completeEvent);++ // Assert+ expect(mockBackupStore.update).not.toHaveBeenCalled();+ expect(mockBackupNotification.send).not.toHaveBeenCalled();+ });++ it('should use occurredAt for the ended time when present', async () => {+ // Arrange+ mockPlanStore.getById.mockResolvedValue(mockPlan);+ mockBackupStore.update.mockResolvedValue({ id: backupId } as any);+ const occurredAt = 1700000000;+ const completeEvent = {+ planId,+ backupId,+ success: true,+ summary: mockCompletionStats,+ occurredAt,+ };++ // Act+ await backupEventService.onBackupComplete(completeEvent);++ // Assert+ expect(mockBackupStore.update).toHaveBeenCalledWith(+ backupId,+ expect.objectContaining({ ended: new Date(occurredAt * 1000) })+ );+ });});describe('onBackupError', () => {@@ -492,6 +532,56 @@ describe('BackupEventService', () => {}));});++ it('should not move lastBackupTime backwards for a replayed late event', async () => {+ // Arrange+ const occurredAt = 1700000000;+ mockPlanStore.getById.mockResolvedValue({+ id: 'plan-123',+ lastBackupTime: new Date((occurredAt + 3600) * 1000),+ } as any);+ const statsEvent = {+ planId: 'plan-123',+ backupId: 'backup-abc',+ total_size: 1024,+ snapshots: ['snap1'],+ occurredAt,+ };++ // Act+ await backupEventService.onBackupStatsUpdate(statsEvent as any);++ // Assert+ expect(mockPlanStore.update).toHaveBeenCalledWith(+ 'plan-123',+ expect.not.objectContaining({ lastBackupTime: expect.anything() })+ );+ });++ it('should set lastBackupTime from occurredAt when it is newer', async () => {+ // Arrange+ const occurredAt = 1700000000;+ mockPlanStore.getById.mockResolvedValue({+ id: 'plan-123',+ lastBackupTime: new Date((occurredAt - 3600) * 1000),+ } as any);+ const statsEvent = {+ planId: 'plan-123',+ backupId: 'backup-abc',+ total_size: 1024,+ snapshots: ['snap1'],+ occurredAt,+ };++ // Act+ await backupEventService.onBackupStatsUpdate(statsEvent as any);++ // Assert+ expect(mockPlanStore.update).toHaveBeenCalledWith(+ 'plan-123',+ expect.objectContaining({ lastBackupTime: new Date(occurredAt * 1000) })+ );+ });});describe('onPruneEnd', () => {diff --git a/backend/src/services/events/BackupEventService.ts b/backend/src/services/events/BackupEventService.tsindex 915d4bb..341fb7e 100644--- a/backend/src/services/events/BackupEventService.ts+++ b/backend/src/services/events/BackupEventService.ts@@ -20,6 +20,7 @@ import {BackupStatUpdateEvent,} from '../../types/events';import { generateResticRepoPath } from '../../utils/restic/helpers';+import { eventDate } from '../../utils/eventTime';import { BackupVerifiedResult, PlanReplicationStorage, PlanStorageItem } from '../../types/plans';import { handleResticCheckResult } from '../../utils/restic/restic';@@ -183,12 +184,27 @@ export class BackupEventService {return backup?.status === 'cancelled';}+ /**+ * A finished backup is terminal. A duplicate or replayed completion/failure+ * event must not rewrite the row or send the notification again. Cancelled+ * is excluded: a genuine success that beat the kill must still be recorded.+ */+ protected async wasFinalized(backupId: string): Promise<boolean> {+ const backup = await this.backupStore.getById(backupId);+ return (+ !!backup && !backup.inProgress && (backup.status === 'completed' || backup.status === 'failed')+ );+ }+async onBackupComplete(data: BackupCompleteEvent): Promise<void> {const { planId, success, backupId, summary } = data;const progressFile = `${this.progressDir}/backup-${backupId}.json`;let summaryData: any = null;let totalDuration = 0;try {+ if (await this.wasFinalized(backupId)) {+ return;+ }if (!success && (await this.wasCancelled(backupId))) {return;}@@ -224,7 +240,7 @@ export class BackupEventService {inProgress: false,status: success ? 'completed' : 'failed',success: !!success,- ended: sql`(unixepoch())` as any,+ ended: eventDate((data as { occurredAt?: number }).occurredAt),completionStats: summaryData ? summaryData : null,...(success ? { errorMsg: null } : {}),};@@ -296,7 +312,7 @@ export class BackupEventService {inProgress: false,status: 'cancelled',success: false,- ended: sql`(unixepoch())` as any,+ ended: eventDate((data as { occurredAt?: number }).occurredAt),});if (!backup) {throw new Error('Failed to update Backup entry in Database.');@@ -343,6 +359,9 @@ export class BackupEventService {async onBackupFailure(data: { planId: string; backupId: string; error: string }): Promise<void> {const { planId, backupId, error } = data;try {+ if (await this.wasFinalized(backupId)) {+ return;+ }if (await this.wasCancelled(backupId)) {return;}@@ -353,7 +372,7 @@ export class BackupEventService {errorMsg: error,success: false,inProgress: false,- ended: sql`(unixepoch())` as any,+ ended: eventDate((data as { occurredAt?: number }).occurredAt),});if (!backup) {throw new Error('Failed to update Backup entry in Database.');@@ -390,13 +409,18 @@ export class BackupEventService {const { total_size, snapshots = [], backupId, planId, error = '', mirrors } = data;if (planId && total_size && !Number.isNaN(total_size) && snapshots.length > 0) {try {+ const occurredAt = (data as { occurredAt?: number }).occurredAt;+ const eventTime = occurredAt ? new Date(occurredAt * 1000) : new Date();+ const plan = await this.planStore.getById(planId);+ // A replayed late event must not move lastBackupTime backwards+ const isNewer = !plan?.lastBackupTime || eventTime >= plan.lastBackupTime;await this.planStore.update(data.planId, {stats: {size: total_size,snapshots: snapshots,...(mirrors ? { mirrors } : {}),},- lastBackupTime: sql`(unixepoch())` as any,+ ...(isNewer ? { lastBackupTime: eventTime } : {}),});planLogger('update', planId, backupId).info(`Updated plan stats for Backup Plan. Total Size: ${total_size} bytes, Total Snapshots: ${snapshots.length || 0} `diff --git a/backend/src/services/events/DownloadEventService.ts b/backend/src/services/events/DownloadEventService.tsindex 073964f..92442ba 100644--- a/backend/src/services/events/DownloadEventService.ts+++ b/backend/src/services/events/DownloadEventService.ts@@ -1,7 +1,8 @@-import { BackupStore } from '../../stores/BackupStore';+import { BackupStore } from '../../stores/BackupStore';import { planLogger } from '../../utils/logger';import { DownloadCompleteEvent, DownloadErrorEvent, DownloadStartEvent } from '../../types/events';import { BaseSnapshotManager } from '../../managers/BaseSnapshotManager';+import { eventSec } from '../../utils/eventTime';export class DownloadEventService {constructor(@@ -45,7 +46,7 @@ export class DownloadEventService {...backup.download,status: 'failed',error: error,- ended: Math.floor(Date.now() / 1000),+ ended: eventSec((eventPayload as { occurredAt?: number }).occurredAt),},});@@ -74,7 +75,7 @@ export class DownloadEventService {...backup.download,status: 'complete',error: '',- ended: Math.floor(Date.now() / 1000),+ ended: eventSec((eventPayload as { occurredAt?: number }).occurredAt),},});diff --git a/backend/src/services/events/ReplicationEventService.ts b/backend/src/services/events/ReplicationEventService.tsindex 3690d16..5498a4f 100644--- a/backend/src/services/events/ReplicationEventService.ts+++ b/backend/src/services/events/ReplicationEventService.ts@@ -1,4 +1,4 @@-import { BaseBackupManager } from '../../managers/BaseBackupManager';+import { BaseBackupManager } from '../../managers/BaseBackupManager';import { BackupStore } from '../../stores/BackupStore';import { StorageStore } from '../../stores/StorageStore';import { PlanStore } from '../../stores/PlanStore';@@ -13,6 +13,7 @@ import {ResolvedReplicationStorage,} from '../../types/events';import { ReplicationHandler } from '../../managers/handlers/ReplicationHandler';+import { eventMs } from '../../utils/eventTime';import { ProgressManager } from '../../managers/ProgressManager';import { appPaths } from '../../utils/AppPaths';@@ -223,7 +224,7 @@ export class ReplicationEventService {storagePath: data.storagePath,storageType: data.storageType,status: 'started',- started: Date.now(),+ started: eventMs((data as { occurredAt?: number }).occurredAt),});planLogger('replication', data.planId, data.backupId).info(`Replication started for storage ${data.storageName} (${data.storageId})`@@ -243,7 +244,7 @@ export class ReplicationEventService {storagePath: data.storagePath,storageType: data.storageType,status: data.success ? 'completed' : 'failed',- ended: Date.now(),+ ended: eventMs((data as { occurredAt?: number }).occurredAt),error: data.error,});diff --git a/backend/src/services/events/RestoreEventService.ts b/backend/src/services/events/RestoreEventService.tsindex 8aacefc..a94eda1 100644--- a/backend/src/services/events/RestoreEventService.ts+++ b/backend/src/services/events/RestoreEventService.ts@@ -1,5 +1,4 @@-import { sql } from 'drizzle-orm';-import { PlanStore } from '../../stores/PlanStore';+import { PlanStore } from '../../stores/PlanStore';import { BackupStore } from '../../stores/BackupStore';import { RestoreStore } from '../../stores/RestoreStore';import { readFile } from 'fs/promises';@@ -9,6 +8,7 @@ import { NewRestore, restoreInsertSchema } from '../../db/schema/restores';import { SourceTypes } from '../../types/source';import { RestoreCompleteEvent, RestoreErrorEvent, RestoreStartEvent } from '../../types/events';import { BaseRestoreManager } from '../../managers/BaseRestoreManager';+import { eventDate } from '../../utils/eventTime';export class RestoreEventService {constructor(@@ -115,7 +115,7 @@ export class RestoreEventService {status: 'error',errorMsg: error,inProgress: true,- ended: sql`(unixepoch())` as any,+ ended: eventDate((eventPayload as { occurredAt?: number }).occurredAt),});}@@ -144,7 +144,7 @@ export class RestoreEventService {status: 'failed',errorMsg: error,inProgress: false,- ended: sql`(unixepoch())` as any,+ ended: eventDate((eventPayload as { occurredAt?: number }).occurredAt),});}@@ -178,7 +178,7 @@ export class RestoreEventService {await this.restoreStore.update(restoreId, {status: success ? 'completed' : 'failed',inProgress: false,- ended: sql`(unixepoch())` as any,+ ended: eventDate((eventPayload as { occurredAt?: number }).occurredAt),completionStats: progressData?.data,});diff --git a/backend/src/types/events.ts b/backend/src/types/events.tsindex eeeff24..4b4a18d 100644--- a/backend/src/types/events.ts+++ b/backend/src/types/events.ts@@ -1,4 +1,4 @@-import { RestoreConfig, RestoreStats } from '../types/restores';+import { RestoreConfig, RestoreStats } from '../types/restores';import { BackupCompletionStats, BackupTaskStats } from './backups';// Event emitted before the dry-run to initialize the backup entry in the database.diff --git a/backend/src/utils/eventTime.ts b/backend/src/utils/eventTime.tsnew file mode 100644index 0000000..5c7d4df--- /dev/null+++ b/backend/src/utils/eventTime.ts@@ -0,0 +1,13 @@+import { sql } from 'drizzle-orm';++/** For integer columns with { mode: 'timestamp' } */+export const eventDate = (occurredAt?: number): any =>+ occurredAt ? new Date(occurredAt * 1000) : (sql`(unixepoch())` as any);++/** For millisecond-epoch values */+export const eventMs = (occurredAt?: number): number =>+ occurredAt ? occurredAt * 1000 : Date.now();++/** For second-epoch values */+export const eventSec = (occurredAt?: number): number =>+ occurredAt ?? Math.floor(Date.now() / 1000);