Skip to content

Commit 23ba82e

Browse files
committed
more efficient by using friendly IDs instead of doing joins
1 parent 95ad817 commit 23ba82e

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

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

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
} from "./executionSnapshotSystem.js";
5353
import { SystemResources } from "./systems.js";
5454
import { WaitpointSystem } from "./waitpointSystem.js";
55+
import { BatchId, RunId } from "@trigger.dev/core/v3/isomorphic";
5556

5657
export type RunAttemptSystemOptions = {
5758
resources: SystemResources;
@@ -82,6 +83,19 @@ type BackwardsCompatibleTaskRunExecution = Omit<TaskRunExecution, "task" | "atte
8283
};
8384
};
8485

86+
const ORG_FRESH_TTL = 60000 * 60 * 24; // 1 day
87+
const ORG_STALE_TTL = 60000 * 60 * 24 * 2; // 2 days
88+
const PROJECT_FRESH_TTL = 60000 * 60 * 24; // 1 day
89+
const PROJECT_STALE_TTL = 60000 * 60 * 24 * 2; // 2 days
90+
const TASK_FRESH_TTL = 60000 * 60 * 24; // 1 day
91+
const TASK_STALE_TTL = 60000 * 60 * 24 * 2; // 2 days
92+
const MACHINE_PRESET_FRESH_TTL = 60000 * 60 * 24; // 1 day
93+
const MACHINE_PRESET_STALE_TTL = 60000 * 60 * 24 * 2; // 2 days
94+
const DEPLOYMENT_FRESH_TTL = 60000 * 60 * 24; // 1 day
95+
const DEPLOYMENT_STALE_TTL = 60000 * 60 * 24 * 2; // 2 days
96+
const QUEUE_FRESH_TTL = 60000 * 60; // 1 hour
97+
const QUEUE_STALE_TTL = 60000 * 60 * 2; // 2 hours
98+
8599
export class RunAttemptSystem {
86100
private readonly $: SystemResources;
87101
private readonly executionSnapshotSystem: ExecutionSnapshotSystem;
@@ -119,33 +133,33 @@ export class RunAttemptSystem {
119133
this.cache = createCache({
120134
orgs: new Namespace<TaskRunExecutionOrganization>(ctx, {
121135
stores: [memory, redisCacheStore],
122-
fresh: 60000,
123-
stale: 180000,
136+
fresh: ORG_FRESH_TTL,
137+
stale: ORG_STALE_TTL,
124138
}),
125139
projects: new Namespace<TaskRunExecutionProject>(ctx, {
126140
stores: [memory, redisCacheStore],
127-
fresh: 60000,
128-
stale: 180000,
141+
fresh: PROJECT_FRESH_TTL,
142+
stale: PROJECT_STALE_TTL,
129143
}),
130144
tasks: new Namespace<BackwardsCompatibleTaskRunExecution["task"]>(ctx, {
131145
stores: [memory, redisCacheStore],
132-
fresh: 60000,
133-
stale: 180000,
146+
fresh: TASK_FRESH_TTL,
147+
stale: TASK_STALE_TTL,
134148
}),
135149
machinePresets: new Namespace<MachinePreset>(ctx, {
136150
stores: [memory, redisCacheStore],
137-
fresh: 60000,
138-
stale: 180000,
151+
fresh: MACHINE_PRESET_FRESH_TTL,
152+
stale: MACHINE_PRESET_STALE_TTL,
139153
}),
140154
deployments: new Namespace<TaskRunExecutionDeployment>(ctx, {
141155
stores: [memory, redisCacheStore],
142-
fresh: 60000,
143-
stale: 180000,
156+
fresh: DEPLOYMENT_FRESH_TTL,
157+
stale: DEPLOYMENT_STALE_TTL,
144158
}),
145159
queues: new Namespace<TaskRunExecutionQueue>(ctx, {
146160
stores: [memory, redisCacheStore],
147-
fresh: 60000,
148-
stale: 180000,
161+
fresh: QUEUE_FRESH_TTL,
162+
stale: QUEUE_STALE_TTL,
149163
}),
150164
});
151165
}
@@ -193,16 +207,9 @@ export class RunAttemptSystem {
193207
organizationId: true,
194208
},
195209
},
196-
parentTaskRun: {
197-
select: {
198-
friendlyId: true,
199-
},
200-
},
201-
rootTaskRun: {
202-
select: {
203-
friendlyId: true,
204-
},
205-
},
210+
parentTaskRunId: true,
211+
rootTaskRunId: true,
212+
batchId: true,
206213
},
207214
});
208215

@@ -252,8 +259,8 @@ export class RunAttemptSystem {
252259
version: run.taskVersion ?? "unknown",
253260
maxDuration: run.maxDurationInSeconds ?? undefined,
254261
priority: run.priorityMs === 0 ? undefined : run.priorityMs / 1_000,
255-
parentTaskRunId: run.parentTaskRun?.friendlyId ?? undefined,
256-
rootTaskRunId: run.rootTaskRun?.friendlyId,
262+
parentTaskRunId: run.parentTaskRunId ? RunId.toFriendlyId(run.parentTaskRunId) : undefined,
263+
rootTaskRunId: run.rootTaskRunId ? RunId.toFriendlyId(run.rootTaskRunId) : undefined,
257264
},
258265
attempt: {
259266
number: run.attemptNumber ?? 1,
@@ -272,6 +279,7 @@ export class RunAttemptSystem {
272279
branchName: run.runtimeEnvironment.branchName ?? undefined,
273280
git: safeParseGitMeta(run.runtimeEnvironment.git),
274281
},
282+
batch: run.batchId ? { id: BatchId.toFriendlyId(run.batchId) } : undefined,
275283
};
276284
}
277285

@@ -407,6 +415,7 @@ export class RunAttemptSystem {
407415
costInCents: true,
408416
traceContext: true,
409417
priorityMs: true,
418+
batchId: true,
410419
runtimeEnvironment: {
411420
select: {
412421
id: true,
@@ -417,16 +426,8 @@ export class RunAttemptSystem {
417426
organizationId: true,
418427
},
419428
},
420-
parentTaskRun: {
421-
select: {
422-
friendlyId: true,
423-
},
424-
},
425-
rootTaskRun: {
426-
select: {
427-
friendlyId: true,
428-
},
429-
},
429+
parentTaskRunId: true,
430+
rootTaskRunId: true,
430431
},
431432
});
432433

@@ -567,8 +568,12 @@ export class RunAttemptSystem {
567568
baseCostInCents: updatedRun.baseCostInCents,
568569
traceContext: updatedRun.traceContext as Record<string, string | undefined>,
569570
priority: updatedRun.priorityMs === 0 ? undefined : updatedRun.priorityMs / 1_000,
570-
parentTaskRunId: updatedRun.parentTaskRun?.friendlyId ?? undefined,
571-
rootTaskRunId: updatedRun.rootTaskRun?.friendlyId ?? undefined,
571+
parentTaskRunId: updatedRun.parentTaskRunId
572+
? RunId.toFriendlyId(updatedRun.parentTaskRunId)
573+
: undefined,
574+
rootTaskRunId: updatedRun.rootTaskRunId
575+
? RunId.toFriendlyId(updatedRun.rootTaskRunId)
576+
: undefined,
572577
},
573578
task,
574579
queue,
@@ -583,7 +588,11 @@ export class RunAttemptSystem {
583588
project,
584589
machine: machinePreset,
585590
deployment,
586-
batch: { id: "TODO" },
591+
batch: updatedRun.batchId
592+
? {
593+
id: BatchId.toFriendlyId(updatedRun.batchId),
594+
}
595+
: undefined,
587596
};
588597

589598
return { run: updatedRun, snapshot, execution };

references/hello-world/src/trigger/example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const parentTask = task({
5757
id: "parent",
5858
machine: "medium-1x",
5959
run: async (payload: any, { ctx }) => {
60-
logger.log("Hello, world from the parent", { payload });
60+
logger.log("Hello, world from the parent", { payload, ctx });
6161
await childTask.triggerAndWait({ message: "Hello, world!", aReallyBigInt: BigInt(10000) });
6262
},
6363
});
@@ -107,7 +107,7 @@ export const childTask = task({
107107
}: { message?: string; failureChance?: number; duration?: number; aReallyBigInt?: bigint },
108108
{ ctx }
109109
) => {
110-
logger.info("Hello, world from the child", { message, failureChance, aReallyBigInt });
110+
logger.info("Hello, world from the child", { ctx, failureChance, aReallyBigInt });
111111

112112
if (Math.random() < failureChance) {
113113
throw new Error("Random error at start");

0 commit comments

Comments
 (0)