Skip to content

Commit 98bbf60

Browse files
committed
fix(notifications): integrate PR #866 with durable backup references
2 parents c200c0b + db9a515 commit 98bbf60

4 files changed

Lines changed: 115 additions & 26 deletions

File tree

‎apps/api/test/lib/notification-headline.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,56 @@ describe("delivered headline vs the category it was subscribed through", () => {
5959
const msg = renderMessage(delivery("service.recovered", { message: "back up" }));
6060
expect(msg.title).toBe("App recovered");
6161
});
62+
63+
it("renders policy, destination, project, and service names in backup alerts", () => {
64+
const msg = renderMessage(
65+
delivery("backup.failed", {
66+
eventType: "backup_run.failed",
67+
policyName: "Nightly Database",
68+
destinationName: "S3 Primary",
69+
projectName: "Production",
70+
serviceName: "postgres",
71+
errorMessage: "Docker stream ended mid-frame with 15433 bytes buffered",
72+
resourceType: "backup_run",
73+
resourceId: "bkr_test_123",
74+
}),
75+
);
76+
77+
expect(msg.title).toBe("Backup failed");
78+
expect(msg.body).toContain("Policy: Nightly Database");
79+
expect(msg.body).toContain("Destination: S3 Primary");
80+
expect(msg.body).toContain("Project: Production");
81+
expect(msg.body).toContain("Service: postgres");
82+
expect(msg.body).toContain("Error: Docker stream ended mid-frame with 15433 bytes buffered");
83+
expect(msg.body).toContain("Resource: backup_run (bkr_test_123)");
84+
});
85+
86+
it("renders policy and destination for successful backups", () => {
87+
const msg = renderMessage(
88+
delivery("backup.succeeded", {
89+
eventType: "backup_run.succeeded",
90+
policyName: "Weekly Volume",
91+
destinationName: "Offsite MinIO",
92+
projectName: "App",
93+
serviceName: "redis",
94+
resourceType: "backup_run",
95+
resourceId: "bkr_test_456",
96+
}),
97+
);
98+
99+
expect(msg.title).toBe("Backup succeeded");
100+
expect(msg.body).toContain("Policy: Weekly Volume");
101+
expect(msg.body).toContain("Destination: Offsite MinIO");
102+
expect(msg.body).toContain("Project: App");
103+
expect(msg.body).toContain("Service: redis");
104+
});
105+
106+
it("keeps durable backup references when names cannot be resolved", () => {
107+
const msg = renderMessage(delivery("backup.failed", {
108+
policyId: "pol_1",
109+
destinationId: "dst_1",
110+
}));
111+
expect(msg.body).toContain("Policy: pol_1");
112+
expect(msg.body).toContain("Destination: dst_1");
113+
});
62114
});

‎apps/api/test/modules/backups/backup-run-durability.test.ts‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ const h = vi.hoisted(() => ({
170170
hookExit: { code: 0 as number | null, stderr: "" },
171171
artifactMetadata: {} as Record<string, unknown>,
172172
notifications: [] as Array<{ eventType: string; payload: Record<string, unknown> }>,
173+
destinationUnavailable: false,
173174
verifyNotes: [] as Array<string | undefined>,
174175
}));
175176

@@ -198,12 +199,15 @@ vi.mock("@repo/db", () => ({
198199
findById: async () => h.policy,
199200
},
200201
backupDestination: {
201-
findById: async () => ({
202-
id: "dst_1",
203-
organizationId: "org_1",
204-
name: "S3 primary",
205-
pathPrefix: null,
206-
}),
202+
findById: async () => {
203+
if (h.destinationUnavailable) throw new Error("Destination lookup unavailable");
204+
return {
205+
id: "dst_1",
206+
organizationId: "org_1",
207+
name: "S3 primary",
208+
pathPrefix: null,
209+
};
210+
},
207211
setLastVerified: async (_id: string, _ok: boolean, note?: string) => {
208212
h.verifyNotes.push(note);
209213
},
@@ -348,6 +352,7 @@ beforeEach(() => {
348352
h.hookExit = { code: 0, stderr: "" };
349353
h.artifactMetadata = {};
350354
h.notifications.length = 0;
355+
h.destinationUnavailable = false;
351356
h.verifyNotes.length = 0;
352357
});
353358

@@ -500,6 +505,26 @@ describe("a terminal status is final — one owner per verdict", () => {
500505
});
501506

502507
describe("BackupOrchestrator.execute — hook output cannot fail the backup", () => {
508+
it("delivers failure references when policy and destination lookup are unavailable", async () => {
509+
const pg = wireRun();
510+
h.policy = null;
511+
h.run!.destinationId = "dst_1";
512+
h.destinationUnavailable = true;
513+
514+
await new BackupOrchestrator().execute("bkr_live");
515+
516+
expect(pg.row.status).toBe("failed");
517+
expect(h.notifications).toContainEqual(expect.objectContaining({
518+
organizationId: "org_1",
519+
eventType: "backup_run.failed",
520+
payload: expect.objectContaining({
521+
policyId: "pol_1",
522+
destinationId: "dst_1",
523+
errorMessage: "Policy pol_1 disappeared",
524+
}),
525+
}));
526+
});
527+
503528
it("runs one pipeline for duplicate deliveries and acknowledges only its owner", async () => {
504529
const pg = wireRun();
505530
h.claimResults = ["claimed", "state_changed"];

‎packages/platform/src/engine/lib/notification-workers.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ export function renderMessage(delivery: NotificationDelivery): RenderedMessage {
6262
const description = own?.description ?? cat?.description;
6363
if (description) lines.push(description);
6464
if (payload.message) lines.push(String(payload.message));
65+
if (payload.projectName) lines.push(`Project: ${payload.projectName}`);
66+
if (payload.serviceName) lines.push(`Service: ${payload.serviceName}`);
67+
if (payload.policyName || payload.policyId) {
68+
lines.push(`Policy: ${payload.policyName ?? payload.policyId}`);
69+
}
70+
if (payload.destinationName || payload.destinationId) {
71+
lines.push(`Destination: ${payload.destinationName ?? payload.destinationId}`);
72+
}
6573

6674
if (payload.branch) lines.push(`Branch: ${payload.branch}`);
6775
if (payload.commitSha) {

‎packages/platform/src/engine/modules/backups/backup.orchestrator.ts‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ export class BackupOrchestrator {
348348
this.publishTransition(runId, "preparing");
349349

350350
let policy = null as Awaited<ReturnType<typeof repos.backupPolicy.findById>> | null;
351+
let ctx: RunContext | null = null;
351352
let executor: BackupExecutor | null = null;
352353
let serviceHandle: ServiceHandle | null = null;
353354
// The runtime the BackupExecutor wraps. Held for the whole run (it shells into
@@ -396,7 +397,6 @@ export class BackupOrchestrator {
396397
// 3. Materialize the SOURCE — a deployed project service, or a bare
397398
// mail server. Both yield an opaque ServiceHandle + an executor +
398399
// the key/manifest metadata the shared pipeline below needs.
399-
let ctx: RunContext;
400400
if (policy.sourceKind === "mail_server") {
401401
if (!policy.mailServerId) throw new Error("mail_server policy has no mailServerId");
402402
const built = await this.buildMailSource(
@@ -654,6 +654,8 @@ export class BackupOrchestrator {
654654
payload: {
655655
projectName: ctx.projectName,
656656
serviceName: ctx.serviceName,
657+
policyId: policy.id,
658+
destinationId: destinationRow.id,
657659
destinationName: destinationRow.name,
658660
bytesTransferred: totalBytes,
659661
artifactCount: artifactsRecorded.length,
@@ -739,25 +741,27 @@ export class BackupOrchestrator {
739741
bytesTransferred: 0,
740742
});
741743

742-
// Fan-out to subscribers. We re-fetch destination if needed —
743-
// the catch block may have lost the closure depending on where
744-
// we threw, so look it up by policy.
745-
if (policy?.destinationId) {
746-
const destForNotify = await repos.backupDestination
747-
.findById(policy.destinationId)
748-
.catch(() => null);
749-
if (destForNotify) {
750-
notification.emit({
751-
organizationId: destForNotify.organizationId,
752-
eventType: "backup_run.failed",
753-
resourceType: "backup_run",
754-
resourceId: runId,
755-
payload: {
756-
destinationName: destForNotify.name,
757-
errorMessage: summary,
758-
},
759-
});
760-
}
744+
// Fan-out to subscribers. Look up destination by policy or run.
745+
const destId = policy?.destinationId ?? run.destinationId;
746+
const destForNotify = destId
747+
? await repos.backupDestination.findById(destId).catch(() => null)
748+
: null;
749+
const organizationId = run.organizationId ?? destForNotify?.organizationId;
750+
if (organizationId) {
751+
notification.emit({
752+
organizationId,
753+
eventType: "backup_run.failed",
754+
resourceType: "backup_run",
755+
resourceId: runId,
756+
payload: {
757+
destinationName: destForNotify?.name ?? null,
758+
destinationId: destId ?? null,
759+
policyId: policy?.id ?? run.policyId ?? null,
760+
projectName: ctx?.projectName ?? null,
761+
serviceName: ctx?.serviceName ?? null,
762+
errorMessage: summary,
763+
},
764+
});
761765
}
762766
} finally {
763767
disposeRuntime(sourceRuntime);

0 commit comments

Comments
 (0)