A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
chore: resolves broken backup/restore cancel for remote devices
towfiqicec516cb1a55
diff --git a/backend/__tests__/services/BackupEventService.test.ts b/backend/__tests__/services/BackupEventService.test.tsindex 1d29d60..b37454c 100644--- a/backend/__tests__/services/BackupEventService.test.ts+++ b/backend/__tests__/services/BackupEventService.test.ts@@ -375,6 +375,99 @@ describe('BackupEventService', () => {});});+ describe('cancelled backups', () => {+ const planId = 'plan-123';+ const backupId = 'backup-abc';++ const markRowCancelled = () =>+ mockBackupStore.getById.mockResolvedValue({ id: backupId, status: 'cancelled' } as any);++ describe('onBackupCancelled', () => {+ it('should mark the backup as "cancelled" and end it', async () => {+ // Arrange+ mockBackupStore.update.mockResolvedValue({ id: backupId } as any);++ // Act+ await backupEventService.onBackupCancelled({ planId, backupId });++ // Assert+ expect(mockBackupStore.update).toHaveBeenCalledWith(+ backupId,+ expect.objectContaining({+ status: 'cancelled',+ success: false,+ inProgress: false,+ })+ );+ });+ });++ describe('guard against late failure events', () => {+ it('should ignore a failed onBackupComplete for a cancelled backup', async () => {+ // Arrange+ markRowCancelled();++ // Act+ await backupEventService.onBackupComplete({ planId, backupId, success: false } as any);++ // Assert+ expect(mockBackupStore.update).not.toHaveBeenCalled();+ });++ it('should still record a genuine success for a cancelled backup', async () => {+ // Arrange+ markRowCancelled();+ mockBackupStore.update.mockResolvedValue({ id: backupId } as any);++ // Act+ await backupEventService.onBackupComplete({ planId, backupId, success: true } as any);++ // Assert+ expect(mockBackupStore.update).toHaveBeenCalledWith(+ backupId,+ expect.objectContaining({ status: 'completed' })+ );+ });++ it('should ignore onBackupError for a cancelled backup', async () => {+ // Arrange+ markRowCancelled();++ // Act+ await backupEventService.onBackupError({ planId, backupId, error: 'killed' });++ // Assert+ expect(mockBackupStore.update).not.toHaveBeenCalled();+ });++ it('should ignore onBackupFailure for a cancelled backup', async () => {+ // Arrange+ markRowCancelled();++ // Act+ await backupEventService.onBackupFailure({ planId, backupId, error: 'killed' });++ // Assert+ expect(mockBackupStore.update).not.toHaveBeenCalled();+ });++ it('should record a failure normally when the backup was not cancelled', async () => {+ // Arrange+ mockBackupStore.getById.mockResolvedValue({ id: backupId, status: 'started' } as any);+ mockBackupStore.update.mockResolvedValue({ id: backupId } as any);++ // Act+ await backupEventService.onBackupError({ planId, backupId, error: 'real failure' });++ // Assert+ expect(mockBackupStore.update).toHaveBeenCalledWith(backupId, {+ status: 'retrying',+ errorMsg: 'real failure',+ });+ });+ });+ });+describe('onBackupStatsUpdate', () => {it('should update plan stats if data is valid', async () => {// Arrangediff --git a/backend/__tests__/services/DownloadEventService.test.ts b/backend/__tests__/services/DownloadEventService.test.tsindex 1116a26..74fce81 100644--- a/backend/__tests__/services/DownloadEventService.test.ts+++ b/backend/__tests__/services/DownloadEventService.test.ts@@ -109,7 +109,9 @@ describe('DownloadEventService', () => {});});- it('should handle missing backup gracefully (backup is null)', async () => {+ it('should not rebuild the download when none is tracked', async () => {+ // A cancel clears the download. A late failure report must not+ // recreate it, or the cancelled run looks like a real failure.// ArrangemockBackupStore.getById.mockResolvedValue(null);mockBackupStore.update.mockResolvedValue({} as any);@@ -119,13 +121,7 @@ describe('DownloadEventService', () => {// Assertexpect(mockBackupStore.getById).toHaveBeenCalledWith(backupId);- expect(mockBackupStore.update).toHaveBeenCalledWith(backupId, {- download: {- status: 'failed',- error: errorMsg,- ended: expect.any(Number),- },- });+ expect(mockBackupStore.update).not.toHaveBeenCalled();});it('should log an error and not throw if an exception occurs', async () => {@@ -166,7 +162,8 @@ describe('DownloadEventService', () => {});});- it('should handle missing backup gracefully (backup is null)', async () => {+ it('should not rebuild the download when none is tracked', async () => {+ // A cancel clears the download, so there is nothing to complete.// ArrangemockBackupStore.getById.mockResolvedValue(null);mockBackupStore.update.mockResolvedValue({} as any);@@ -176,13 +173,7 @@ describe('DownloadEventService', () => {// Assertexpect(mockBackupStore.getById).toHaveBeenCalledWith(backupId);- expect(mockBackupStore.update).toHaveBeenCalledWith(backupId, {- download: {- status: 'complete',- error: '',- ended: expect.any(Number),- },- });+ expect(mockBackupStore.update).not.toHaveBeenCalled();});it('should log an error and not throw if an exception occurs', async () => {diff --git a/backend/__tests__/services/RestoreEventService.test.ts b/backend/__tests__/services/RestoreEventService.test.tsindex 6d97bf1..4047e17 100644--- a/backend/__tests__/services/RestoreEventService.test.ts+++ b/backend/__tests__/services/RestoreEventService.test.ts@@ -176,6 +176,62 @@ describe('RestoreEventService', () => {});});+ describe('cancelled restores', () => {+ const restoreId = 'restore-xyz';+ const baseEvent = { planId: 'plan-123', backupId: 'backup-abc', restoreId };++ const markRowCancelled = () =>+ mockRestoreStore.getById.mockResolvedValue({ id: restoreId, status: 'cancelled' } as any);++ it('should ignore onRestoreError for a cancelled restore', async () => {+ // Arrange+ markRowCancelled();++ // Act+ await restoreEventService.onRestoreError({ ...baseEvent, error: 'killed' });++ // Assert+ expect(mockRestoreStore.update).not.toHaveBeenCalled();+ });++ it('should ignore onRestoreFailed for a cancelled restore', async () => {+ // Arrange+ markRowCancelled();++ // Act+ await restoreEventService.onRestoreFailed({ ...baseEvent, error: 'killed' });++ // Assert+ expect(mockRestoreStore.update).not.toHaveBeenCalled();+ });++ it('should ignore a failed onRestoreComplete for a cancelled restore', async () => {+ // Arrange+ markRowCancelled();++ // Act+ await restoreEventService.onRestoreComplete({ ...baseEvent, success: false } as any);++ // Assert+ expect(mockRestoreStore.update).not.toHaveBeenCalled();+ });++ it('should record a failure normally when the restore was not cancelled', async () => {+ // Arrange+ mockRestoreStore.getById.mockResolvedValue({ id: restoreId, status: 'started' } as any);+ mockRestoreStore.update.mockResolvedValue({ id: restoreId } as any);++ // Act+ await restoreEventService.onRestoreFailed({ ...baseEvent, error: 'real failure' });++ // Assert+ expect(mockRestoreStore.update).toHaveBeenCalledWith(+ restoreId,+ expect.objectContaining({ status: 'failed', errorMsg: 'real failure' })+ );+ });+ });+describe('onRestoreFailed', () => {const failedEvent: RestoreErrorEvent = {planId: 'plan-123',diff --git a/backend/src/services/events/BackupEventService.ts b/backend/src/services/events/BackupEventService.tsindex 5c6a15c..915d4bb 100644--- a/backend/src/services/events/BackupEventService.ts+++ b/backend/src/services/events/BackupEventService.ts@@ -173,12 +173,26 @@ export class BackupEventService {}}+ /**+ * A cancelled backup is terminal: the user asked for it and cancelBackup+ * already recorded it. A remote agent still reports its killed run as an+ * ordinary failure, so those late events must not overwrite the status.+ */+ protected async wasCancelled(backupId: string): Promise<boolean> {+ const backup = await this.backupStore.getById(backupId);+ return backup?.status === 'cancelled';+ }+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 (!success && (await this.wasCancelled(backupId))) {+ return;+ }+// If remote agent summary data is already provided, use itif (summary && summary.message_type === 'summary') {summaryData = summary;@@ -270,8 +284,40 @@ export class BackupEventService {}}+ /**+ * A remote agent reports a user cancellation with its own event, so the+ * backup is not recorded as a failure. cancelBackup normally wrote the same+ * status already; this also covers a cancel that the agent started itself.+ */+ async onBackupCancelled(data: { planId: string; backupId: string }): Promise<void> {+ const { planId, backupId } = data;+ try {+ const backup = await this.backupStore.update(backupId, {+ inProgress: false,+ status: 'cancelled',+ success: false,+ ended: sql`(unixepoch())` as any,+ });+ if (!backup) {+ throw new Error('Failed to update Backup entry in Database.');+ }++ planLogger('backup', planId, backupId).info(`Backup was cancelled by the user.`);+ } catch (error: any) {+ planLogger('backup', planId, backupId).error(+ `Error processing backup cancellation for Backup. Reason : ${+ error?.message.toString() || 'Unknown Error'+ }`+ );+ }+ }+async onBackupError(data: BackupErrorEvent): Promise<void> {try {+ if (await this.wasCancelled(data.backupId)) {+ return;+ }+// Update DB with Errorconst backup = await this.backupStore.update(data.backupId, {status: 'retrying',@@ -297,6 +343,10 @@ export class BackupEventService {async onBackupFailure(data: { planId: string; backupId: string; error: string }): Promise<void> {const { planId, backupId, error } = data;try {+ if (await this.wasCancelled(backupId)) {+ return;+ }+// Update DB with Errorconst backup = await this.backupStore.update(backupId, {status: 'failed',diff --git a/backend/src/services/events/DownloadEventService.ts b/backend/src/services/events/DownloadEventService.tsindex b253d46..073964f 100644--- a/backend/src/services/events/DownloadEventService.ts+++ b/backend/src/services/events/DownloadEventService.ts@@ -34,9 +34,15 @@ export class DownloadEventService {const { backupId, planId, error } = eventPayload;try {const backup = await this.backupStore.getById(backupId);+ // A cancel cleared the download. Do not rebuild it from a late+ // failure report, or the killed run looks like a real failure.+ if (!backup?.download) {+ return;+ }+await this.backupStore.update(backupId, {download: {- ...(backup?.download || {}),+ ...backup.download,status: 'failed',error: error,ended: Math.floor(Date.now() / 1000),@@ -58,9 +64,14 @@ export class DownloadEventService {const { backupId, planId, success } = eventPayload;try {const backup = await this.backupStore.getById(backupId);+ // A cancel cleared the download, so there is nothing to complete.+ if (!backup?.download) {+ return;+ }+await this.backupStore.update(backupId, {download: {- ...(backup?.download || {}),+ ...backup.download,status: 'complete',error: '',ended: Math.floor(Date.now() / 1000),diff --git a/backend/src/services/events/RestoreEventService.ts b/backend/src/services/events/RestoreEventService.tsindex 8d263af..8aacefc 100644--- a/backend/src/services/events/RestoreEventService.ts+++ b/backend/src/services/events/RestoreEventService.ts@@ -90,12 +90,26 @@ export class RestoreEventService {}}+ /**+ * A cancelled restore is terminal: the user asked for it and cancelRestore+ * already recorded it. A remote agent still reports its killed run as an+ * ordinary failure, so those late events must not overwrite the status.+ */+ protected async wasCancelled(restoreId: string): Promise<boolean> {+ const restore = await this.restoreStore.getById(restoreId);+ return restore?.status === 'cancelled';+ }+async onRestoreError(eventPayload: RestoreErrorEvent) {const { backupId, restoreId, planId, error } = eventPayload;if (!backupId || !planId || !error) {return;}try {+ if (restoreId && (await this.wasCancelled(restoreId))) {+ return;+ }+if (restoreId) {await this.restoreStore.update(restoreId, {status: 'error',@@ -121,6 +135,10 @@ export class RestoreEventService {return;}try {+ if (await this.wasCancelled(restoreId)) {+ return;+ }+if (restoreId) {await this.restoreStore.update(restoreId, {status: 'failed',@@ -146,6 +164,10 @@ export class RestoreEventService {return;}try {+ if (!success && (await this.wasCancelled(restoreId))) {+ return;+ }+const progressFile = `${appPaths.getProgressDir()}/restore-${restoreId}.json`;let progressData;diff --git a/backend/src/strategies/snapshot/RemoteStrategy.ts b/backend/src/strategies/snapshot/RemoteStrategy.tsindex 47f6814..1e98ca8 100644--- a/backend/src/strategies/snapshot/RemoteStrategy.ts+++ b/backend/src/strategies/snapshot/RemoteStrategy.ts@@ -27,8 +27,8 @@ export class RemoteStrategy implements SnapshotStrategy {return await this.publishCommand('GET_SNAPSHOT_DOWNLOAD', { planId, backupId });}- async cancelSnapshotDownload(backupId: string) {- return await this.publishCommand('CANCEL_SNAPSHOT_DOWNLOAD', { backupId });+ async cancelSnapshotDownload(planId: string, backupId: string) {+ return await this.publishCommand('CANCEL_SNAPSHOT_DOWNLOAD', { planId, backupId });}async getSnapshotFiles(