Skip to content

Commit b12b617

Browse files
committed
add metadata changes introduced in #1563
1 parent b8cb8dc commit b12b617

File tree

8 files changed

+145
-6
lines changed

8 files changed

+145
-6
lines changed

apps/webapp/app/models/runtimeEnvironment.server.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import type { Prisma, RuntimeEnvironment } from "@trigger.dev/database";
1+
import { AuthenticatedEnvironment } from "@internal/testcontainers";
2+
import type { Prisma, PrismaClientOrTransaction, RuntimeEnvironment } from "@trigger.dev/database";
23
import { prisma } from "~/db.server";
34
import { getUsername } from "~/utils/username";
45

56
export type { RuntimeEnvironment };
67

7-
export async function findEnvironmentByApiKey(apiKey: string) {
8+
export async function findEnvironmentByApiKey(
9+
apiKey: string
10+
): Promise<AuthenticatedEnvironment | null> {
811
const environment = await prisma.runtimeEnvironment.findUnique({
912
where: {
1013
apiKey,
@@ -24,7 +27,9 @@ export async function findEnvironmentByApiKey(apiKey: string) {
2427
return environment;
2528
}
2629

27-
export async function findEnvironmentByPublicApiKey(apiKey: string) {
30+
export async function findEnvironmentByPublicApiKey(
31+
apiKey: string
32+
): Promise<AuthenticatedEnvironment | null> {
2833
const environment = await prisma.runtimeEnvironment.findUnique({
2934
where: {
3035
pkApiKey: apiKey,
@@ -44,7 +49,7 @@ export async function findEnvironmentByPublicApiKey(apiKey: string) {
4449
return environment;
4550
}
4651

47-
export async function findEnvironmentById(id: string) {
52+
export async function findEnvironmentById(id: string): Promise<AuthenticatedEnvironment | null> {
4853
const environment = await prisma.runtimeEnvironment.findUnique({
4954
where: {
5055
id,
@@ -64,6 +69,32 @@ export async function findEnvironmentById(id: string) {
6469
return environment;
6570
}
6671

72+
export async function findEnvironmentFromRun(
73+
runId: string,
74+
tx?: PrismaClientOrTransaction
75+
): Promise<AuthenticatedEnvironment | null> {
76+
const taskRun = await (tx ?? prisma).taskRun.findUnique({
77+
where: {
78+
id: runId,
79+
},
80+
include: {
81+
runtimeEnvironment: {
82+
include: {
83+
project: true,
84+
organization: true,
85+
orgMember: true,
86+
},
87+
},
88+
},
89+
});
90+
91+
if (!taskRun) {
92+
return null;
93+
}
94+
95+
return taskRun?.runtimeEnvironment;
96+
}
97+
6798
export async function createNewSession(environment: RuntimeEnvironment, ipAddress: string) {
6899
const session = await prisma.runtimeEnvironmentSession.create({
69100
data: {

apps/webapp/app/services/routeBuilders/apiBuilder.server.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ export function createLoaderApiRoute<
230230
if (error instanceof Response) {
231231
return await wrapResponse(request, error, corsStrategy !== "none");
232232
}
233+
234+
logger.error("Error in loader", {
235+
error:
236+
error instanceof Error
237+
? {
238+
name: error.name,
239+
message: error.message,
240+
stack: error.stack,
241+
}
242+
: String(error),
243+
url: request.url,
244+
});
245+
233246
return await wrapResponse(
234247
request,
235248
json({ error: "Internal Server Error" }, { status: 500 }),
@@ -770,6 +783,19 @@ export function createLoaderWorkerApiRoute<
770783
if (error instanceof Response) {
771784
return error;
772785
}
786+
787+
logger.error("Error in loader", {
788+
error:
789+
error instanceof Error
790+
? {
791+
name: error.name,
792+
message: error.message,
793+
stack: error.stack,
794+
}
795+
: String(error),
796+
url: request.url,
797+
});
798+
773799
return json({ error: "Internal Server Error" }, { status: 500 });
774800
}
775801
};
@@ -785,6 +811,7 @@ type WorkerActionRouteBuilderOptions<
785811
searchParams?: TSearchParamsSchema;
786812
headers?: THeadersSchema;
787813
body?: TBodySchema;
814+
method?: "POST" | "PUT" | "DELETE" | "PATCH";
788815
};
789816

790817
type WorkerActionHandlerFunction<
@@ -823,6 +850,15 @@ export function createActionWorkerApiRoute<
823850
>
824851
) {
825852
return async function action({ request, params }: ActionFunctionArgs) {
853+
if (options.method) {
854+
if (request.method.toUpperCase() !== options.method) {
855+
return json(
856+
{ error: "Method not allowed" },
857+
{ status: 405, headers: { Allow: options.method } }
858+
);
859+
}
860+
}
861+
826862
const {
827863
params: paramsSchema,
828864
searchParams: searchParamsSchema,
@@ -903,6 +939,19 @@ export function createActionWorkerApiRoute<
903939
if (error instanceof Response) {
904940
return error;
905941
}
942+
943+
logger.error("Error in action", {
944+
error:
945+
error instanceof Error
946+
? {
947+
name: error.name,
948+
message: error.message,
949+
stack: error.stack,
950+
}
951+
: String(error),
952+
url: request.url,
953+
});
954+
906955
return json({ error: "Internal Server Error" }, { status: 500 });
907956
}
908957
};

apps/webapp/app/v3/runEngineHandlers.server.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { roomFromFriendlyRunId, socketIo } from "./handleSocketIo.server";
99
import { engine } from "./runEngine.server";
1010
import { PerformTaskRunAlertsService } from "./services/alerts/performTaskRunAlerts.server";
1111
import { RunId } from "@trigger.dev/core/v3/apps";
12+
import { updateMetadataService } from "~/services/metadata/updateMetadata.server";
13+
import { findEnvironmentFromRun } from "~/models/runtimeEnvironment.server";
1214

1315
export function registerRunEngineEventBusHandlers() {
1416
engine.eventBus.on("runSucceeded", async ({ time, run }) => {
@@ -235,6 +237,31 @@ export function registerRunEngineEventBusHandlers() {
235237
}
236238
});
237239

240+
engine.eventBus.on("runMetadataUpdated", async ({ time, run }) => {
241+
const env = await findEnvironmentFromRun(run.id);
242+
243+
if (!env) {
244+
logger.error("[runMetadataUpdated] Failed to find environment", { runId: run.id });
245+
return;
246+
}
247+
248+
try {
249+
await updateMetadataService.call(env, run.id, run.metadata);
250+
} catch (e) {
251+
logger.error("[runMetadataUpdated] Failed to update metadata", {
252+
taskRun: run.id,
253+
error:
254+
e instanceof Error
255+
? {
256+
name: e.name,
257+
message: e.message,
258+
stack: e.stack,
259+
}
260+
: e,
261+
});
262+
}
263+
});
264+
238265
engine.eventBus.on("executionSnapshotCreated", async ({ time, run, snapshot }) => {
239266
try {
240267
const foundRun = await prisma.taskRun.findUnique({

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { CancelAttemptService } from "./cancelAttempt.server";
2828
import { CreateCheckpointService } from "./createCheckpoint.server";
2929
import { FinalizeTaskRunService } from "./finalizeTaskRun.server";
3030
import { RetryAttemptService } from "./retryAttempt.server";
31-
import { updateMetadataService } from "~/services/metadata/updateMetadata.server";
3231

3332
type FoundAttempt = Awaited<ReturnType<typeof findAttempt>>;
3433

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TaskRunExecutionStatus, TaskRunStatus } from "@trigger.dev/database";
22
import { AuthenticatedEnvironment } from "../shared";
3-
import { TaskRunError } from "@trigger.dev/core/v3";
3+
import { FlushedRunMetadata, TaskRunError } from "@trigger.dev/core/v3";
44

55
export type EventBusEvents = {
66
runAttemptStarted: [
@@ -79,6 +79,15 @@ export type EventBusEvents = {
7979
};
8080
},
8181
];
82+
runMetadataUpdated: [
83+
{
84+
time: Date;
85+
run: {
86+
id: string;
87+
metadata: FlushedRunMetadata;
88+
};
89+
},
90+
];
8291
workerNotification: [
8392
{
8493
time: Date;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,16 @@ export class RunEngine {
11851185
snapshotId: string;
11861186
completion: TaskRunExecutionResult;
11871187
}): Promise<CompleteRunAttemptResult> {
1188+
if (completion.metadata) {
1189+
this.eventBus.emit("runMetadataUpdated", {
1190+
time: new Date(),
1191+
run: {
1192+
id: runId,
1193+
metadata: completion.metadata,
1194+
},
1195+
});
1196+
}
1197+
11881198
switch (completion.ok) {
11891199
case true: {
11901200
return this.#attemptSucceeded({ runId, snapshotId, completion, tx: this.prisma });

packages/cli-v3/src/entryPoints/managed-run-worker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ const zodIpc = new ZodIpcConnection({
217217
usage: {
218218
durationMs: 0,
219219
},
220+
metadata: runMetadataManager.stopAndReturnLastFlush(),
220221
},
221222
});
222223

@@ -247,6 +248,7 @@ const zodIpc = new ZodIpcConnection({
247248
usage: {
248249
durationMs: 0,
249250
},
251+
metadata: runMetadataManager.stopAndReturnLastFlush(),
250252
},
251253
});
252254

@@ -278,6 +280,7 @@ const zodIpc = new ZodIpcConnection({
278280
usage: {
279281
durationMs: 0,
280282
},
283+
metadata: runMetadataManager.stopAndReturnLastFlush(),
281284
},
282285
});
283286

@@ -304,6 +307,7 @@ const zodIpc = new ZodIpcConnection({
304307
usage: {
305308
durationMs: 0,
306309
},
310+
metadata: runMetadataManager.stopAndReturnLastFlush(),
307311
},
308312
});
309313

@@ -356,6 +360,7 @@ const zodIpc = new ZodIpcConnection({
356360
usage: {
357361
durationMs: usageSample.cpuTime,
358362
},
363+
metadata: runMetadataManager.stopAndReturnLastFlush(),
359364
},
360365
});
361366
}
@@ -379,6 +384,7 @@ const zodIpc = new ZodIpcConnection({
379384
usage: {
380385
durationMs: usageSample.cpuTime,
381386
},
387+
metadata: runMetadataManager.stopAndReturnLastFlush(),
382388
},
383389
});
384390
}
@@ -401,6 +407,7 @@ const zodIpc = new ZodIpcConnection({
401407
usage: {
402408
durationMs: 0,
403409
},
410+
metadata: runMetadataManager.stopAndReturnLastFlush(),
404411
},
405412
});
406413
}

packages/cli-v3/src/entryPoints/unmanaged-run-worker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ const zodIpc = new ZodIpcConnection({
217217
usage: {
218218
durationMs: 0,
219219
},
220+
metadata: runMetadataManager.stopAndReturnLastFlush(),
220221
},
221222
});
222223

@@ -247,6 +248,7 @@ const zodIpc = new ZodIpcConnection({
247248
usage: {
248249
durationMs: 0,
249250
},
251+
metadata: runMetadataManager.stopAndReturnLastFlush(),
250252
},
251253
});
252254

@@ -278,6 +280,7 @@ const zodIpc = new ZodIpcConnection({
278280
usage: {
279281
durationMs: 0,
280282
},
283+
metadata: runMetadataManager.stopAndReturnLastFlush(),
281284
},
282285
});
283286

@@ -304,6 +307,7 @@ const zodIpc = new ZodIpcConnection({
304307
usage: {
305308
durationMs: 0,
306309
},
310+
metadata: runMetadataManager.stopAndReturnLastFlush(),
307311
},
308312
});
309313

@@ -356,6 +360,7 @@ const zodIpc = new ZodIpcConnection({
356360
usage: {
357361
durationMs: usageSample.cpuTime,
358362
},
363+
metadata: runMetadataManager.stopAndReturnLastFlush(),
359364
},
360365
});
361366
}
@@ -379,6 +384,7 @@ const zodIpc = new ZodIpcConnection({
379384
usage: {
380385
durationMs: usageSample.cpuTime,
381386
},
387+
metadata: runMetadataManager.stopAndReturnLastFlush(),
382388
},
383389
});
384390
}
@@ -401,6 +407,7 @@ const zodIpc = new ZodIpcConnection({
401407
usage: {
402408
durationMs: 0,
403409
},
410+
metadata: runMetadataManager.stopAndReturnLastFlush(),
404411
},
405412
});
406413
}

0 commit comments

Comments
 (0)