Skip to content

Commit b45376a

Browse files
committed
added cancel controller
1 parent 324aca6 commit b45376a

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

kraken/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,33 @@ wss.on('connection', async (ws, req) => {
9696
return;
9797
}
9898

99-
const dataStream = jobService.streamStatus(<JobLogRequest>{jobId: jobId})
100-
101-
for await (const chunk of dataStream) {
102-
if (!chunk.jobInfo) {
103-
console.warn("Empty job state")
104-
continue
105-
}
99+
const controller = new AbortController();
100+
const dataStream = jobService.streamStatus(<JobLogRequest>{jobId: jobId}, {signal: controller.signal})
101+
102+
ws.on("close", () => {
103+
console.log("disconnected")
104+
controller.abort()
105+
})
106+
try {
107+
for await (const chunk of dataStream) {
108+
if (!chunk.jobInfo) {
109+
console.warn("Empty job state")
110+
continue
111+
}
106112

107-
const {jobTimeout, $unknown, $typeName, ...rest} = chunk.jobInfo!
113+
const {jobTimeout, $unknown, $typeName, ...rest} = chunk.jobInfo!
108114

109-
console.log("Job", rest);
110-
console.log(chunk.logs)
115+
console.log("Job", rest);
116+
console.log(chunk.logs)
111117

112-
ws.send(JSON.stringify({
113-
logs: chunk.logs,
114-
jobStatus: rest,
115-
}));
118+
ws.send(JSON.stringify({
119+
logs: chunk.logs,
120+
jobStatus: rest,
121+
}));
122+
}
123+
} catch (e) {
124+
console.error(e)
116125
}
117126

118127
console.log("Job ID:", jobId, "done streaming");
119128
});
120-
121-
wss.on('close', () => {
122-
123-
})

0 commit comments

Comments
 (0)