fixed task system

This commit is contained in:
DecDuck
2024-10-21 21:50:21 +11:00
parent c355f6fdbb
commit e1c1d7ea39
2 changed files with 19 additions and 13 deletions
+14 -8
View File
@@ -3,12 +3,16 @@ import session from "~/server/internal/session";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import taskHandler, { TaskMessage } from "~/server/internal/tasks"; import taskHandler, { TaskMessage } from "~/server/internal/tasks";
// TODO add web socket sessions for horizontal scaling
// ID to admin
const socketSessions: { [key: string]: boolean } = {};
export default defineWebSocketHandler({ export default defineWebSocketHandler({
open(peer) { open(peer) {
const dummyEvent = { const dummyEvent = {
node: { node: {
req: { req: {
headers: peer.headers, headers: peer.request?.headers,
}, },
}, },
} as unknown as H3Event; } as unknown as H3Event;
@@ -18,29 +22,31 @@ export default defineWebSocketHandler({
return; return;
} }
const admin = session.getAdminUser(dummyEvent); const admin = session.getAdminUser(dummyEvent);
const peerId = uuidv4(); socketSessions[peer.id] = admin !== undefined;
peer.ctx.id = peerId;
peer.ctx.admin = admin !== undefined;
const rtMsg: TaskMessage = { const rtMsg: TaskMessage = {
id: "connect", id: "connect",
name: "Connect",
success: true, success: true,
progress: 0, progress: 0,
error: undefined, error: undefined,
log: [], log: [],
}; };
peer.send(rtMsg); peer.send(JSON.stringify(rtMsg));
}, },
message(peer, message) { message(peer, message) {
if (!peer.ctx.id) return; if (!peer.id) return;
if (socketSessions[peer.id] === undefined) return;
const text = message.text(); const text = message.text();
if (text.startsWith("connect/")) { if (text.startsWith("connect/")) {
const id = text.substring("connect/".length); const id = text.substring("connect/".length);
taskHandler.connect(peer.ctx.id, id, peer, peer.ctx.admin); taskHandler.connect(peer.id, id, peer, socketSessions[peer.id]);
return; return;
} }
}, },
close(peer, details) { close(peer, details) {
if (!peer.ctx.id) return; if (!peer.id) return;
if (socketSessions[peer.id] === undefined) return;
delete socketSessions[peer.id];
}, },
}); });
+5 -5
View File
@@ -41,7 +41,7 @@ class TaskHandler {
}; };
for (const client of Object.keys(taskEntry.clients)) { for (const client of Object.keys(taskEntry.clients)) {
if (!this.clientRegistry[client]) continue; if (!this.clientRegistry[client]) continue;
this.clientRegistry[client].send(taskMessage); this.clientRegistry[client].send(JSON.stringify(taskMessage));
} }
updateCollectTimeout = undefined; updateCollectTimeout = undefined;
}, 100); }, 100);
@@ -91,9 +91,9 @@ class TaskHandler {
connect(id: string, taskId: string, peer: PeerImpl, isAdmin = false) { connect(id: string, taskId: string, peer: PeerImpl, isAdmin = false) {
const task = this.taskRegistry[taskId]; const task = this.taskRegistry[taskId];
if (!task) return false; if (!task) return "Invalid task";
if (task.requireAdmin && !isAdmin) return false; if (task.requireAdmin && !isAdmin) return "Requires admin";
this.clientRegistry[id] = peer; this.clientRegistry[id] = peer;
this.taskRegistry[taskId].clients[id] = true; // Uniquely insert client to avoid sending duplicate traffic this.taskRegistry[taskId].clients[id] = true; // Uniquely insert client to avoid sending duplicate traffic
@@ -106,7 +106,7 @@ class TaskHandler {
log: task.log, log: task.log,
progress: task.progress, progress: task.progress,
}; };
peer.send(catchupMessage); peer.send(JSON.stringify(catchupMessage));
return true; return true;
} }
@@ -150,7 +150,7 @@ export type TaskMessage = {
}; };
export type PeerImpl = { export type PeerImpl = {
send: (message: TaskMessage) => void; send: (message: string) => void;
}; };
export const taskHandler = new TaskHandler(); export const taskHandler = new TaskHandler();