Skip to content

Commit

Permalink
Add backupActivityJSONAsFinished(…) to get a value of type `Finishe…
Browse files Browse the repository at this point in the history
…dBackupActivityJSON` for finished backups.
  • Loading branch information
lgarron committed Dec 10, 2024
1 parent 43ec4cd commit ff4f4d8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ This package is published as TypeScript source files. You will need to use a com
## API

````ts
interface BackupActivityJSON {
interface CommonBackupActivityJSONFields {
aborted: boolean;
abortReason?: string;
activityLogPath: string;
backupPlanDbId: number;
backupSetUUID: string;
Expand All @@ -23,12 +22,22 @@ interface BackupActivityJSON {
dataVersion: number;
errorCount: number;
finishedTime: number;
message: string;
subType: string;
type: string;
updatedTime: number;
uuid: string;
}
interface InProgressBackupActivityJSON extends CommonBackupActivityJSONFields {
message: string;
}
interface FinishedBackupActivityJSON extends CommonBackupActivityJSONFields {
message: "Idle";
abortReason?: string;
totalBytes: 0;
totalFiles: 0;
}
type BackupActivityJSON = InProgressBackupActivityJSON & FinishedBackupActivityJSON;
declare function backupActivityJSONAsFinished(backupActivityJSON: BackupActivityJSON): FinishedBackupActivityJSON | null;
interface ArqBackupPlanConfig {
backupSetUUID: string;
name?: string;
Expand All @@ -52,5 +61,5 @@ declare function listBackupPlans(): Promise<ArqBackupPlan[]>;

declare function setArqcCommandPath(newPath: string): void;

export { ArqBackupPlan, type ArqBackupPlanConfig, type BackupActivityJSON, acceptLicenseAgreement, listBackupPlans, pauseBackups, resumeBackups, setArqcCommandPath };
export { ArqBackupPlan, type ArqBackupPlanConfig, type BackupActivityJSON, type FinishedBackupActivityJSON, type InProgressBackupActivityJSON, acceptLicenseAgreement, backupActivityJSONAsFinished, listBackupPlans, pauseBackups, resumeBackups, setArqcCommandPath };
````
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arqc",
"version": "0.3.2",
"version": "0.3.3",
"license": "MIT",
"author": "Lucas Garron",
"description": "A wrapper for the `arqc` command.",
Expand Down
38 changes: 35 additions & 3 deletions src/ArqBackupPlan.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { $, type ShellPromise } from "bun";
import { arqcCommand } from "./arqcCommand";

export interface BackupActivityJSON {
interface CommonBackupActivityJSONFields {
// Note: `"aborted"` may still be `false` for an aborted activity when the post-backup script is running.
aborted: boolean;
abortReason?: string;
activityLogPath: string;
backupPlanDbId: number;
backupSetUUID: string;
Expand All @@ -16,12 +15,45 @@ export interface BackupActivityJSON {
dataVersion: number;
errorCount: number;
finishedTime: number;
message: string;
subType: string;
type: string;
updatedTime: number;
uuid: string;
}
export interface InProgressBackupActivityJSON
extends CommonBackupActivityJSONFields {
// We'd use `Exclude<string, "Idle">`, but that is currently treated as `string` in TypeScript
message: string;
}

export interface FinishedBackupActivityJSON
extends CommonBackupActivityJSONFields {
message: "Idle";
abortReason?: string;
totalBytes: 0;
totalFiles: 0;
}

// Note: this should be `|` rather than `&`, but this is `&` due to TypeScript
// limitations.
//
// This means that `if (backupActivityJSON.message === "Idle")` unfortunately
// cannot type narrow the type of backupActivityJSON from `BackupActivityJSON`
// to `FinishedBackupActivityJSON` inside the `if` block where the condition is
// true. We provide the `backupActivityJSONAsFinished(…)` function for this.
export type BackupActivityJSON = InProgressBackupActivityJSON &
FinishedBackupActivityJSON;

// Applies a heuristic to return `FinishedBackupActivityJSON` if and only if the backup has finished.
// Current heuristic: check if the `"message"` field is `"Idle"`.
export function backupActivityJSONAsFinished(
backupActivityJSON: BackupActivityJSON,
): FinishedBackupActivityJSON | null {
if (backupActivityJSON.message === "Idle") {
return backupActivityJSON as FinishedBackupActivityJSON;
}
return null;
}

export interface ArqBackupPlanConfig {
backupSetUUID: string;
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export {
type BackupActivityJSON,
type ArqBackupPlanConfig,
ArqBackupPlan,
backupActivityJSONAsFinished,
FinishedBackupActivityJSON,
InProgressBackupActivityJSON,
} from "./ArqBackupPlan";
export {
acceptLicenseAgreement,
Expand Down

0 comments on commit ff4f4d8

Please sign in to comment.