|
| 1 | +import { SnapshotOrphanSweeper } from "@internal/run-store"; |
| 2 | +import { env } from "~/env.server"; |
| 3 | +import { logger } from "~/services/logger.server"; |
| 4 | +import { engine } from "./runEngine.server"; |
| 5 | +import { runStoreWithoutSnapshotDecorator } from "./runStore.server"; |
| 6 | +import { buildSnapshotSweepRunner } from "./snapshotSweepRunner.server"; |
| 7 | +import { setSnapshotRepairEnqueuer, setSnapshotSweepRunner } from "./snapshotStoreBindings.server"; |
| 8 | +import { getSnapshotSweepClient } from "./snapshotStoreInstance.server"; |
| 9 | + |
| 10 | +/** |
| 11 | + * The third module: it imports both sides, so neither the run store nor the engine has to import |
| 12 | + * the other. Invoked from entry.server.tsx, beside the other boot registrations. |
| 13 | + */ |
| 14 | +export function registerSnapshotStoreWiring(): boolean { |
| 15 | + const sweepClient = getSnapshotSweepClient(); |
| 16 | + |
| 17 | + if (!sweepClient) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + |
| 21 | + setSnapshotRepairEnqueuer(async (args) => { |
| 22 | + await engine.enqueueSnapshotRepair(args); |
| 23 | + }); |
| 24 | + |
| 25 | + const sweeper = new SnapshotOrphanSweeper({ |
| 26 | + redisOptions: { |
| 27 | + keyPrefix: "engine:", |
| 28 | + host: env.RUN_ENGINE_SNAPSHOT_STORE_REDIS_HOST ?? undefined, |
| 29 | + port: env.RUN_ENGINE_SNAPSHOT_STORE_REDIS_PORT ?? undefined, |
| 30 | + username: env.RUN_ENGINE_SNAPSHOT_STORE_REDIS_USERNAME ?? undefined, |
| 31 | + password: env.RUN_ENGINE_SNAPSHOT_STORE_REDIS_PASSWORD ?? undefined, |
| 32 | + ...(env.RUN_ENGINE_SNAPSHOT_STORE_REDIS_TLS_DISABLED === "true" ? {} : { tls: {} }), |
| 33 | + }, |
| 34 | + // The undecorated router: rule 2 asks Postgres whether a run row exists, and must never be |
| 35 | + // able to ask Redis whether Redis is an orphan. |
| 36 | + runStore: runStoreWithoutSnapshotDecorator, |
| 37 | + completedTtlMs: env.RUN_ENGINE_SNAPSHOT_STORE_COMPLETED_TTL_MS, |
| 38 | + orphanAgeMs: env.RUN_ENGINE_SNAPSHOT_STORE_ORPHAN_AGE_MS, |
| 39 | + }); |
| 40 | + |
| 41 | + setSnapshotSweepRunner( |
| 42 | + buildSnapshotSweepRunner({ |
| 43 | + client: sweepClient, |
| 44 | + // The sweep does not yet accept a deadline, so the budget is not enforced inside a pass. The |
| 45 | + // fenced lock is what keeps two passes apart; its TTL covers the expected pass duration. |
| 46 | + sweep: async () => ({ ...(await sweeper.sweep()) }), |
| 47 | + lockTtlMs: env.RUN_ENGINE_SNAPSHOT_STORE_GC_SWEEP_BUDGET_MS + 3_600_000, |
| 48 | + }) |
| 49 | + ); |
| 50 | + |
| 51 | + logger.info("snapshot store wiring registered"); |
| 52 | + return true; |
| 53 | +} |
0 commit comments