| 1 | /** |
| 2 | * Nightly backups (`RunnerService.startBackups`): each sweep claims a few |
| 3 | * of the backups the repos service queued and starts a sandbox for each, |
| 4 | * which runs the runner's `backup` mode (crates/runner backup.rs). The |
| 5 | * flow is in `crates/contracts/src/backups.rs`. Pure, so it is tested on |
| 6 | * its own. |
| 7 | */ |
| 8 | import type { BackupClaim } from "@g1t/contracts"; |
| 9 | |
| 10 | /** A backup's time cap: a clone and a bundle of at most 1 GB. */ |
| 11 | export const BACKUP_MINUTES = 60; |
| 12 | |
| 13 | /** How many backups one sweep starts, and how many may run at once. */ |
| 14 | export type BackupPace = { perSweep: number; running: number }; |
| 15 | |
| 16 | const DEFAULT_PACE: BackupPace = { perSweep: 4, running: 6 }; |
| 17 | |
| 18 | function count(value: string | undefined, fallback: number): number { |
| 19 | if (value === undefined || value.trim() === "") return fallback; |
| 20 | const parsed = Number(value); |
| 21 | return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * BACKUPS_PER_SWEEP and BACKUPS_RUNNING, or their defaults. `0` per sweep |
| 26 | * starts none: backups are off. |
| 27 | */ |
| 28 | export function backupPace(perSweep: string | undefined, running: string | undefined): BackupPace { |
| 29 | return { |
| 30 | perSweep: count(perSweep, DEFAULT_PACE.perSweep), |
| 31 | running: count(running, DEFAULT_PACE.running), |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | /** One sandbox per job: asking twice starts nothing twice. */ |
| 36 | export function backupSandboxName(claim: BackupClaim): string { |
| 37 | return `backup-${claim.jobId}`; |
| 38 | } |
| 39 | |
| 40 | /** What the sandbox is started with: its job, and nothing that reads git. */ |
| 41 | export function backupEnv(claim: BackupClaim, api: string): Record<string, string> { |
| 42 | return { |
| 43 | MODE: "backup", |
| 44 | G1T_API: api, |
| 45 | BACKUP_JOB: claim.jobId, |
| 46 | BACKUP_TOKEN: claim.token, |
| 47 | }; |
| 48 | } |