Skip to content

Commit c200c0b

Browse files
committed
fix(backups): integrate PR #860 without suppressing scheduling failures
2 parents 8db8ee1 + 8c8bc44 commit c200c0b

2 files changed

Lines changed: 105 additions & 5 deletions

File tree

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

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe("backupRun.transition — status must not ride a payload that can fail"
157157
const h = vi.hoisted(() => ({
158158
run: null as Record<string, unknown> | null,
159159
policy: null as Record<string, unknown> | null,
160-
services: [] as Array<{ id: string; enabled: boolean }>,
160+
services: [] as Array<Record<string, unknown>>,
161161
createdRuns: [] as Array<Record<string, unknown>>,
162162
executionRow: null as Record<string, unknown> | null,
163163
claimResults: ["claimed"] as Array<"claimed" | "project_unavailable" | "state_changed">,
@@ -360,8 +360,8 @@ describe("BackupOrchestrator.enqueue — durable batch identity", () => {
360360
mailServerId: null,
361361
};
362362
h.services = [
363-
{ id: "svc_api", enabled: true },
364-
{ id: "svc_db", enabled: true },
363+
{ id: "svc_api", enabled: true, volumes: ["api_data:/data"] },
364+
{ id: "svc_db", enabled: true, image: "postgres:16" },
365365
];
366366
const orchestrator = new BackupOrchestrator();
367367

@@ -386,6 +386,75 @@ describe("BackupOrchestrator.enqueue — durable batch identity", () => {
386386
expect(new Set(secondBatch.map((run) => run.batchId)).size).toBe(1);
387387
expect(secondBatch[0]!.batchId).not.toBe(firstBatch[0]!.batchId);
388388
});
389+
390+
it("skips stateless services without volumes or database images during fan-out", async () => {
391+
h.policy = {
392+
...(h.policy ?? {}),
393+
sourceKind: "service",
394+
projectId: "proj_1",
395+
mailServerId: null,
396+
payloadKind: "auto",
397+
};
398+
h.services = [
399+
{ id: "svc_web", enabled: true, volumes: [] },
400+
{ id: "svc_monitor", enabled: true, volumes: [] },
401+
{ id: "svc_db", enabled: true, image: "postgres:16" },
402+
];
403+
const orchestrator = new BackupOrchestrator();
404+
405+
await orchestrator.enqueue({
406+
policyId: "pol_1",
407+
trigger: { source: "cron", userId: "system" },
408+
});
409+
410+
expect(h.createdRuns).toHaveLength(1);
411+
expect(h.createdRuns[0]!.serviceId).toBe("svc_db");
412+
});
413+
414+
it("throws when project has no services with persistent storage", async () => {
415+
h.policy = {
416+
...(h.policy ?? {}),
417+
sourceKind: "service",
418+
projectId: "proj_1",
419+
mailServerId: null,
420+
payloadKind: "auto",
421+
};
422+
h.services = [
423+
{ id: "svc_web", enabled: true, volumes: [] },
424+
{ id: "svc_monitor", enabled: true, volumes: [] },
425+
];
426+
const orchestrator = new BackupOrchestrator();
427+
428+
await expect(
429+
orchestrator.enqueue({
430+
policyId: "pol_1",
431+
trigger: { source: "cron", userId: "system" },
432+
}),
433+
).rejects.toThrow(/no services with persistent storage/);
434+
435+
expect(h.createdRuns).toHaveLength(0);
436+
});
437+
438+
it.each(["custom_command", "path"])(
439+
"keeps explicit %s payloads eligible without volumes",
440+
async (payloadKind) => {
441+
h.policy = {
442+
...(h.policy ?? {}),
443+
sourceKind: "service",
444+
projectId: "proj_1",
445+
mailServerId: null,
446+
payloadKind,
447+
};
448+
h.services = [{ id: "svc_app", enabled: true, image: "node:22", volumes: [] }];
449+
450+
await new BackupOrchestrator().enqueue({
451+
policyId: "pol_1",
452+
trigger: { source: "cron", userId: "system" },
453+
});
454+
455+
expect(h.createdRuns.map((run) => run.serviceId)).toEqual(["svc_app"]);
456+
},
457+
);
389458
});
390459

391460
describe("a terminal status is final — one owner per verdict", () => {

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { notification } from "../../lib/notification-dispatcher";
6060
import { serviceHandleFor, withContainerEnv } from "./service-handle";
6161
import { resolveSourceExecutor } from "./source-platform";
6262
import crypto from "node:crypto";
63-
import { safeErrorMessage } from "@repo/core";
63+
import { detectDbImage, safeErrorMessage, ValidationError } from "@repo/core";
6464
import {
6565
boundedStorableText,
6666
sanitizeStorableStringsExceptKeys,
@@ -83,6 +83,31 @@ const TRUNCATE_ERROR = 4096;
8383
const TRUNCATE_HOOK_LOG = 64 * 1024;
8484
/** Cap on waiting for a finished hook's stdout to drain (see runHook). */
8585
const HOOK_DRAIN_TIMEOUT_MS = 500;
86+
87+
/**
88+
* A service is eligible for project-level backup fan-out if:
89+
* 1. The policy specifies custom_command or path payloads, OR
90+
* 2. The service runs a recognized database engine (PostgreSQL, MySQL, Redis, MongoDB), OR
91+
* 3. The service has declared volumes in its service definition.
92+
*
93+
* Stateless services without volumes or databases are skipped during project fan-out
94+
* so they do not fail the backup batch (#859).
95+
*/
96+
export function isBackupCandidateService(
97+
service: Pick<Service, "image" | "volumes">,
98+
policy: Pick<BackupPolicy, "payloadKind">,
99+
): boolean {
100+
if (policy.payloadKind === "custom_command" || policy.payloadKind === "path") {
101+
return true;
102+
}
103+
if (detectDbImage(service.image) !== null) {
104+
return true;
105+
}
106+
if (Array.isArray(service.volumes) && service.volumes.length > 0) {
107+
return true;
108+
}
109+
return false;
110+
}
86111
/** Short form for the notification payload + destination verify note. */
87112
const TRUNCATE_ERROR_SUMMARY = 500;
88113
/** A `PutResult.etag` in this shape is a sha256 we can compare ours against. */
@@ -207,8 +232,14 @@ export class BackupOrchestrator {
207232
if (services.length === 0) {
208233
throw new Error("Project has no services to back up — add a service or pick one.");
209234
}
235+
const candidates = services.filter((svc) => isBackupCandidateService(svc, policy));
236+
if (candidates.length === 0) {
237+
throw new ValidationError(
238+
"Project has no services with persistent storage (volumes or databases) to back up.",
239+
);
240+
}
210241
const runIds: string[] = [];
211-
for (const svc of services) {
242+
for (const svc of candidates) {
212243
try {
213244
runIds.push(
214245
await this.spawnRun(

0 commit comments

Comments
 (0)