Skip to content

Commit 81b8c4b

Browse files
committed
add logger
1 parent 9bc9a15 commit 81b8c4b

File tree

2 files changed

+61
-41
lines changed

2 files changed

+61
-41
lines changed

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

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { logger } from "../utilities/logger.js";
21
import { TaskRunProcess } from "../executions/taskRunProcess.js";
32
import { env as stdEnv } from "std-env";
43
import { readJSONFile } from "../utilities/fileSystem.js";
@@ -16,7 +15,6 @@ import {
1615
WarmStartClient,
1716
WORKLOAD_HEADERS,
1817
type WorkloadClientToServerEvents,
19-
type WorkloadDebugLogRequestBody,
2018
WorkloadHttpClient,
2119
type WorkloadServerToClientEvents,
2220
type WorkloadRunAttemptStartResponseBody,
@@ -26,8 +24,7 @@ import { setTimeout as sleep } from "timers/promises";
2624
import { io, type Socket } from "socket.io-client";
2725
import { RunnerEnv } from "./managed/env.js";
2826
import { MetadataClient } from "./managed/overrides.js";
29-
30-
logger.loggerLevel = "debug";
27+
import { RunLogger, SendDebugLogOptions } from "./managed/logger.js";
3128

3229
const env = new RunnerEnv(stdEnv);
3330

@@ -54,6 +51,7 @@ class ManagedRunController {
5451
private readonly metadataClient?: MetadataClient;
5552

5653
private socket: Socket<WorkloadServerToClientEvents, WorkloadClientToServerEvents>;
54+
private readonly logger: RunLogger;
5755

5856
private readonly runHeartbeat: HeartbeatService;
5957
private readonly snapshotPoller: HeartbeatService;
@@ -107,6 +105,11 @@ class ManagedRunController {
107105
projectRef: env.TRIGGER_PROJECT_REF,
108106
});
109107

108+
this.logger = new RunLogger({
109+
httpClient: this.httpClient,
110+
env,
111+
});
112+
110113
const properties = {
111114
...env.raw,
112115
TRIGGER_POD_SCHEDULED_AT_MS: env.TRIGGER_POD_SCHEDULED_AT_MS.toISOString(),
@@ -1484,43 +1487,8 @@ class ManagedRunController {
14841487
assertExhaustive(attemptStatus);
14851488
}
14861489

1487-
sendDebugLog({
1488-
runId,
1489-
message,
1490-
date,
1491-
properties,
1492-
}: {
1493-
runId?: string;
1494-
message: string;
1495-
date?: Date;
1496-
properties?: WorkloadDebugLogRequestBody["properties"];
1497-
}) {
1498-
if (!runId) {
1499-
runId = this.runFriendlyId;
1500-
}
1501-
1502-
if (!runId) {
1503-
runId = env.TRIGGER_RUN_ID;
1504-
}
1505-
1506-
if (!runId) {
1507-
return;
1508-
}
1509-
1510-
const mergedProperties = {
1511-
...properties,
1512-
runId,
1513-
runnerId: this.runnerId,
1514-
workerName: this.workerInstanceName,
1515-
};
1516-
1517-
console.log(message, mergedProperties);
1518-
1519-
this.httpClient.sendDebugLog(runId, {
1520-
message,
1521-
time: date ?? new Date(),
1522-
properties: mergedProperties,
1523-
});
1490+
sendDebugLog(opts: SendDebugLogOptions) {
1491+
this.logger.sendDebugLog(opts);
15241492
}
15251493

15261494
async cancelAttempt(runId: string) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
WorkloadDebugLogRequestBody,
3+
WorkloadHttpClient,
4+
} from "@trigger.dev/core/v3/runEngineWorker";
5+
import { RunnerEnv } from "./env.js";
6+
7+
export type SendDebugLogOptions = {
8+
runId?: string;
9+
message: string;
10+
date?: Date;
11+
properties?: WorkloadDebugLogRequestBody["properties"];
12+
};
13+
14+
export type RunLoggerOptions = {
15+
httpClient: WorkloadHttpClient;
16+
env: RunnerEnv;
17+
};
18+
19+
export class RunLogger {
20+
private readonly httpClient: WorkloadHttpClient;
21+
private readonly env: RunnerEnv;
22+
23+
constructor(private readonly opts: RunLoggerOptions) {
24+
this.httpClient = opts.httpClient;
25+
this.env = opts.env;
26+
}
27+
28+
sendDebugLog({ runId, message, date, properties }: SendDebugLogOptions) {
29+
if (!runId) {
30+
runId = this.env.TRIGGER_RUN_ID;
31+
}
32+
33+
if (!runId) {
34+
return;
35+
}
36+
37+
const mergedProperties = {
38+
...properties,
39+
runId,
40+
runnerId: this.env.TRIGGER_RUNNER_ID,
41+
workerName: this.env.TRIGGER_WORKER_INSTANCE_NAME,
42+
};
43+
44+
console.log(message, mergedProperties);
45+
46+
this.httpClient.sendDebugLog(runId, {
47+
message,
48+
time: date ?? new Date(),
49+
properties: mergedProperties,
50+
});
51+
}
52+
}

0 commit comments

Comments
 (0)