Skip to content

Commit bbd7ea6

Browse files
committed
create socket in constructor
1 parent aebe34e commit bbd7ea6

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type Snapshot = {
4242
friendlyId: string;
4343
};
4444

45+
type SupervisorSocket = Socket<WorkloadServerToClientEvents, WorkloadClientToServerEvents>;
46+
4547
class ManagedRunController {
4648
private taskRunProcess?: TaskRunProcess;
4749

@@ -51,7 +53,7 @@ class ManagedRunController {
5153
private readonly warmStartClient: WarmStartClient | undefined;
5254
private readonly metadataClient?: MetadataClient;
5355

54-
private socket: Socket<WorkloadServerToClientEvents, WorkloadClientToServerEvents>;
56+
private socket: SupervisorSocket;
5557
private readonly logger: RunLogger;
5658

5759
private readonly runHeartbeat: RunExecutionHeartbeat;
@@ -121,6 +123,9 @@ class ManagedRunController {
121123
heartbeatIntervalSeconds: this.heartbeatIntervalSeconds,
122124
});
123125

126+
// Websocket notifications are only an optimisation so we don't need to wait for a successful connection
127+
this.socket = this.createSupervisorSocket();
128+
124129
process.on("SIGTERM", async () => {
125130
this.sendDebugLog({
126131
runId: this.runFriendlyId,
@@ -130,6 +135,8 @@ class ManagedRunController {
130135
});
131136
}
132137

138+
// These settings depend on env vars that may be overridden, e.g. after runs and restores
139+
133140
get heartbeatIntervalSeconds() {
134141
return env.TRIGGER_HEARTBEAT_INTERVAL_SECONDS;
135142
}
@@ -1103,17 +1110,18 @@ class ManagedRunController {
11031110
process.exit(code);
11041111
}
11051112

1106-
createSocket() {
1113+
createSupervisorSocket(): SupervisorSocket {
11071114
const wsUrl = new URL("/workload", this.workerApiUrl);
11081115

1109-
this.socket = io(wsUrl.href, {
1116+
const socket = io(wsUrl.href, {
11101117
transports: ["websocket"],
11111118
extraHeaders: {
11121119
[WORKLOAD_HEADERS.DEPLOYMENT_ID]: env.TRIGGER_DEPLOYMENT_ID,
11131120
[WORKLOAD_HEADERS.RUNNER_ID]: env.TRIGGER_RUNNER_ID,
11141121
},
1115-
});
1116-
this.socket.on("run:notify", async ({ version, run }) => {
1122+
}) satisfies SupervisorSocket;
1123+
1124+
socket.on("run:notify", async ({ version, run }) => {
11171125
this.sendDebugLog({
11181126
runId: run.friendlyId,
11191127
message: "run:notify received by runner",
@@ -1165,7 +1173,8 @@ class ManagedRunController {
11651173

11661174
await this.handleSnapshotChange(latestSnapshot.data.execution);
11671175
});
1168-
this.socket.on("connect", () => {
1176+
1177+
socket.on("connect", () => {
11691178
this.sendDebugLog({
11701179
runId: this.runFriendlyId,
11711180
message: "Connected to supervisor",
@@ -1177,20 +1186,24 @@ class ManagedRunController {
11771186
this.subscribeToRunNotifications({ run, snapshot });
11781187
}
11791188
});
1180-
this.socket.on("connect_error", (error) => {
1189+
1190+
socket.on("connect_error", (error) => {
11811191
this.sendDebugLog({
11821192
runId: this.runFriendlyId,
11831193
message: "Connection error",
11841194
properties: { error: error instanceof Error ? error.message : String(error) },
11851195
});
11861196
});
1187-
this.socket.on("disconnect", (reason, description) => {
1197+
1198+
socket.on("disconnect", (reason, description) => {
11881199
this.sendDebugLog({
11891200
runId: this.runFriendlyId,
11901201
message: "Disconnected from supervisor",
11911202
properties: { reason, description: description?.toString() },
11921203
});
11931204
});
1205+
1206+
return socket;
11941207
}
11951208

11961209
private async executeRun({
@@ -1432,9 +1445,6 @@ class ManagedRunController {
14321445
message: "Starting up",
14331446
});
14341447

1435-
// Websocket notifications are only an optimisation so we don't need to wait for a successful connection
1436-
this.createSocket();
1437-
14381448
// If we have run and snapshot IDs, we can start an attempt immediately
14391449
if (env.TRIGGER_RUN_ID && env.TRIGGER_SNAPSHOT_ID) {
14401450
this.startAndExecuteRunAttempt({

0 commit comments

Comments
 (0)