|
| 1 | +// A redis-primary birth records itself in two places, written by the same pod in one unit: the |
| 2 | +// TaskRun row's annotations (in the INSERT that creates the run) and the run's MemoryDB `seq` hash |
| 3 | +// (`bf`). A mirrored birth records neither. Real Postgres + real Redis, no mocks. |
| 4 | +import { describe, expect } from "vitest"; |
| 5 | +import { containerTest } from "@internal/testcontainers"; |
| 6 | +import { createRedisClient } from "@internal/redis"; |
| 7 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 8 | +import { generateInternalId } from "@trigger.dev/core/v3/isomorphic"; |
| 9 | +import { PostgresRunStore } from "./PostgresRunStore.js"; |
| 10 | +import { RedisSnapshotStore } from "./redisSnapshotStore.js"; |
| 11 | +import { TaskRunExecutionSnapshotStore } from "./taskRunExecutionSnapshotStore.js"; |
| 12 | +import { hasRedisPrimaryBirth } from "./snapshotResidency.js"; |
| 13 | +import { snapshotKeys } from "./snapshotKeys.js"; |
| 14 | +import { |
| 15 | + buildCreateRunData, |
| 16 | + seedSnapshotEnvironment, |
| 17 | + type SnapshotFixtureEnv, |
| 18 | +} from "./testFixtures/snapshotIdFixture.js"; |
| 19 | + |
| 20 | +const ROUTE = "logical:1"; |
| 21 | +const TRIGGER_ANNOTATIONS = { |
| 22 | + triggerSource: "api", |
| 23 | + triggerAction: "trigger", |
| 24 | + rootTriggerSource: "api", |
| 25 | +}; |
| 26 | + |
| 27 | +function birthSnapshot(env: SnapshotFixtureEnv, id: string) { |
| 28 | + return { |
| 29 | + id, |
| 30 | + createdAt: new Date(), |
| 31 | + engine: "V2" as const, |
| 32 | + executionStatus: "RUN_CREATED" as const, |
| 33 | + description: "Run was created", |
| 34 | + runStatus: "PENDING" as const, |
| 35 | + environmentId: env.id, |
| 36 | + environmentType: env.type, |
| 37 | + projectId: env.projectId, |
| 38 | + organizationId: env.organizationId, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +function writer( |
| 43 | + prisma: PrismaClient, |
| 44 | + store: RedisSnapshotStore, |
| 45 | + redisPrimaryBirthReady: boolean |
| 46 | +): TaskRunExecutionSnapshotStore { |
| 47 | + return new TaskRunExecutionSnapshotStore( |
| 48 | + new PostgresRunStore({ prisma, readOnlyPrisma: prisma }), |
| 49 | + { |
| 50 | + store, |
| 51 | + mode: "redis-only", |
| 52 | + resolveDial: () => "redis-only", |
| 53 | + logicalRunStoreRoute: ROUTE, |
| 54 | + redisPrimaryBirthReady: () => redisPrimaryBirthReady, |
| 55 | + } |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +async function annotationsOf(prisma: PrismaClient, runId: string): Promise<unknown> { |
| 60 | + return (await prisma.taskRun.findUniqueOrThrow({ where: { id: runId } })).annotations; |
| 61 | +} |
| 62 | + |
| 63 | +describe("TaskRunExecutionSnapshotStore redis-primary birth flag", () => { |
| 64 | + containerTest( |
| 65 | + "a redis-primary createRun flags the TaskRun annotations and the seq hash", |
| 66 | + async ({ prisma, redisOptions }) => { |
| 67 | + const store = new RedisSnapshotStore({ redisOptions, completedTtlMs: 60_000 }); |
| 68 | + const raw = createRedisClient(redisOptions); |
| 69 | + try { |
| 70 | + const env = await seedSnapshotEnvironment(prisma); |
| 71 | + const runId = generateInternalId(); |
| 72 | + await writer(prisma, store, true).createRun({ |
| 73 | + data: { ...buildCreateRunData(runId, env), annotations: TRIGGER_ANNOTATIONS }, |
| 74 | + snapshot: birthSnapshot(env, generateInternalId()), |
| 75 | + }); |
| 76 | + |
| 77 | + expect(await store.readBirthResidency(runId)).toBe("redis-primary"); |
| 78 | + // The trigger's own annotations survive next to the flag. |
| 79 | + expect(await annotationsOf(prisma, runId)).toEqual({ |
| 80 | + ...TRIGGER_ANNOTATIONS, |
| 81 | + redisPrimaryBirth: true, |
| 82 | + }); |
| 83 | + expect(await raw.hget(snapshotKeys(runId).seq, "bf")).toBe("1"); |
| 84 | + } finally { |
| 85 | + await raw.quit(); |
| 86 | + await store.quit(); |
| 87 | + } |
| 88 | + } |
| 89 | + ); |
| 90 | + |
| 91 | + containerTest( |
| 92 | + "a mirrored createRun flags neither the TaskRun nor the seq hash", |
| 93 | + async ({ prisma, redisOptions }) => { |
| 94 | + const store = new RedisSnapshotStore({ redisOptions, completedTtlMs: 60_000 }); |
| 95 | + const raw = createRedisClient(redisOptions); |
| 96 | + try { |
| 97 | + const env = await seedSnapshotEnvironment(prisma); |
| 98 | + const runId = generateInternalId(); |
| 99 | + // Not ready: the redis-only dial degrades the birth to mirrored. |
| 100 | + await writer(prisma, store, false).createRun({ |
| 101 | + data: { ...buildCreateRunData(runId, env), annotations: TRIGGER_ANNOTATIONS }, |
| 102 | + snapshot: birthSnapshot(env, generateInternalId()), |
| 103 | + }); |
| 104 | + |
| 105 | + expect(await store.readBirthResidency(runId)).toBe("mirrored"); |
| 106 | + expect(await annotationsOf(prisma, runId)).toEqual(TRIGGER_ANNOTATIONS); |
| 107 | + expect(await raw.hget(snapshotKeys(runId).seq, "bf")).toBeNull(); |
| 108 | + } finally { |
| 109 | + await raw.quit(); |
| 110 | + await store.quit(); |
| 111 | + } |
| 112 | + } |
| 113 | + ); |
| 114 | + |
| 115 | + containerTest( |
| 116 | + "a redis-primary createCancelledRun flags the TaskRun annotations and the seq hash", |
| 117 | + async ({ prisma, redisOptions }) => { |
| 118 | + const store = new RedisSnapshotStore({ redisOptions, completedTtlMs: 60_000 }); |
| 119 | + const raw = createRedisClient(redisOptions); |
| 120 | + try { |
| 121 | + const env = await seedSnapshotEnvironment(prisma); |
| 122 | + const runId = generateInternalId(); |
| 123 | + const cancelledAt = new Date(); |
| 124 | + await writer(prisma, store, true).createCancelledRun({ |
| 125 | + data: { |
| 126 | + ...buildCreateRunData(runId, env), |
| 127 | + status: "CANCELED", |
| 128 | + error: { type: "STRING_ERROR", raw: "cancelled before materialisation" }, |
| 129 | + completedAt: cancelledAt, |
| 130 | + updatedAt: cancelledAt, |
| 131 | + attemptNumber: 0, |
| 132 | + }, |
| 133 | + snapshot: { |
| 134 | + ...birthSnapshot(env, generateInternalId()), |
| 135 | + executionStatus: "FINISHED", |
| 136 | + description: "Run cancelled before materialisation", |
| 137 | + runStatus: "CANCELED", |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(await store.readBirthResidency(runId)).toBe("redis-primary"); |
| 142 | + expect(hasRedisPrimaryBirth(await annotationsOf(prisma, runId))).toBe(true); |
| 143 | + expect(await raw.hget(snapshotKeys(runId).seq, "bf")).toBe("1"); |
| 144 | + } finally { |
| 145 | + await raw.quit(); |
| 146 | + await store.quit(); |
| 147 | + } |
| 148 | + } |
| 149 | + ); |
| 150 | +}); |
0 commit comments