Skip to content

Commit 56ec707

Browse files
committed
feat(run-store): wire RunStore into run-engine SystemResources and webapp BaseService
Add RunStore field to SystemResources, instantiate PostgresRunStore in RunEngine constructor (after prisma/readOnlyPrisma are set), and expose it on the resources object passed to all systems. Create a webapp singleton (runStore.server.ts) and thread it as a default parameter into BaseService so subclasses can access it without changes.
1 parent f66bbad commit 56ec707

4 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { PostgresRunStore } from "@internal/run-store";
2+
import { $replica, prisma } from "~/db.server";
3+
import { singleton } from "~/utils/singleton";
4+
5+
export const runStore = singleton(
6+
"PostgresRunStore",
7+
() => new PostgresRunStore({ prisma, readOnlyPrisma: $replica })
8+
);

apps/webapp/app/v3/services/baseService.server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { Span, SpanKind } from "@opentelemetry/api";
2+
import type { RunStore } from "@internal/run-store";
23
import { $replica, PrismaClientOrTransaction, prisma } from "~/db.server";
34
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
45
import { attributesFromAuthenticatedEnv, tracer } from "../tracer.server";
56
import { engine, RunEngine } from "../runEngine.server";
7+
import { runStore as defaultRunStore } from "../runStore.server";
68
import { ServiceValidationError } from "./common.server";
79

810
export { ServiceValidationError };
911

1012
export abstract class BaseService {
1113
constructor(
1214
protected readonly _prisma: PrismaClientOrTransaction = prisma,
13-
protected readonly _replica: PrismaClientOrTransaction = $replica
15+
protected readonly _replica: PrismaClientOrTransaction = $replica,
16+
protected readonly runStore: RunStore = defaultRunStore
1417
) {}
1518

1619
protected async traceWithEnv<T>(

internal-packages/run-engine/src/engine/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import { RaceSimulationSystem } from "./systems/raceSimulationSystem.js";
7373
import { RunAttemptSystem } from "./systems/runAttemptSystem.js";
7474
import { NoopPendingVersionRunIdLookup } from "./services/pendingVersionLookup.js";
7575
import { SystemResources } from "./systems/systems.js";
76+
import { PostgresRunStore, RunStore } from "@internal/run-store";
7677
import { TtlSystem } from "./systems/ttlSystem.js";
7778
import { WaitpointSystem } from "./systems/waitpointSystem.js";
7879
import {
@@ -102,6 +103,7 @@ export class RunEngine {
102103

103104
prisma: PrismaClient;
104105
readOnlyPrisma: PrismaReplicaClient;
106+
runStore: RunStore;
105107
runQueue: RunQueue;
106108
eventBus: EventBus = new EventEmitter<EventBusEvents>();
107109
executionSnapshotSystem: ExecutionSnapshotSystem;
@@ -123,6 +125,7 @@ export class RunEngine {
123125
this.logger = options.logger ?? new Logger("RunEngine", this.options.logLevel ?? "info");
124126
this.prisma = options.prisma;
125127
this.readOnlyPrisma = options.readOnlyPrisma ?? this.prisma;
128+
this.runStore = new PostgresRunStore({ prisma: this.prisma, readOnlyPrisma: this.readOnlyPrisma });
126129
this.runLockRedis = createRedisClient(
127130
{
128131
...options.runLock.redis,
@@ -313,6 +316,7 @@ export class RunEngine {
313316
const resources: SystemResources = {
314317
prisma: this.prisma,
315318
readOnlyPrisma: this.readOnlyPrisma,
319+
runStore: this.runStore,
316320
worker: this.worker,
317321
eventBus: this.eventBus,
318322
logger: this.logger,

internal-packages/run-engine/src/engine/systems/systems.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Meter, Tracer } from "@internal/tracing";
2+
import { RunStore } from "@internal/run-store";
23
import { Logger } from "@trigger.dev/core/logger";
34
import { PrismaClient, PrismaReplicaClient } from "@trigger.dev/database";
45
import { RunQueue } from "../../run-queue/index.js";
@@ -11,6 +12,7 @@ import { RaceSimulationSystem } from "./raceSimulationSystem.js";
1112
export type SystemResources = {
1213
prisma: PrismaClient;
1314
readOnlyPrisma: PrismaReplicaClient;
15+
runStore: RunStore;
1416
worker: EngineWorker;
1517
eventBus: EventBus;
1618
logger: Logger;

0 commit comments

Comments
 (0)