-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-command-queue.ts
More file actions
45 lines (39 loc) · 1.48 KB
/
Copy pathagent-command-queue.ts
File metadata and controls
45 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Queue } from "bullmq";
import { getQueueConnection } from "@/lib/queue/connection";
/**
* Routes a command from an API route (running in the Next.js process) to
* a specific agent's live WebSocket connection, which is held by the
* separate agent-gateway process (`npm run agent-gateway`) - the same
* cross-process problem `worker:ai` solves with a queue, reused here
* rather than inventing a second mechanism. The agent-gateway process
* runs a BullMQ Worker on this same queue (see
* lib/agent-gateway/server.ts) that looks up its own in-memory connection
* map and sends the message, or reports the command failed if that agent
* isn't currently connected.
*/
export const AGENT_COMMAND_QUEUE_NAME = "botfleet-agent-commands";
export interface AgentCommandJobData {
agentId: string;
/** Already-constructed, fully-validated ControlPlaneToAgent envelope -
* the queue is just a transport between processes, not another place
* to build or validate messages. */
message: unknown;
}
function createQueue() {
return new Queue<AgentCommandJobData>(AGENT_COMMAND_QUEUE_NAME, {
connection: getQueueConnection(),
});
}
let queue: ReturnType<typeof createQueue> | undefined;
function getQueue() {
if (!queue) {
queue = createQueue();
}
return queue;
}
export async function enqueueAgentCommand(data: AgentCommandJobData): Promise<void> {
await getQueue().add("send-command", data, {
removeOnComplete: { age: 3600 },
removeOnFail: { age: 3600 },
});
}