Add an admin API endpoint to get info about the shared marqs queue

This commit is contained in:
Eric Allam
2024-05-01 10:09:44 +01:00
parent 3b3b07aec2
commit 5ca9e5667c
2 changed files with 60 additions and 0 deletions
@@ -0,0 +1,31 @@
import { LoaderFunctionArgs, json } from "@remix-run/server-runtime";
import { prisma } from "~/db.server";
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
import { marqs } from "~/v3/marqs/index.server";
export async function loader({ request, params }: LoaderFunctionArgs) {
// Next authenticate the request
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
if (!authenticationResult) {
return json({ error: "Invalid or Missing API key" }, { status: 401 });
}
const user = await prisma.user.findUnique({
where: {
id: authenticationResult.userId,
},
});
if (!user) {
return json({ error: "Invalid or Missing API key" }, { status: 401 });
}
if (!user.admin) {
return json({ error: "You must be an admin to perform this action" }, { status: 403 });
}
const details = await marqs?.getSharedQueueDetails();
return json(details);
}
+29
View File
@@ -283,6 +283,35 @@ export class MarQS {
);
}
public async getSharedQueueDetails() {
const parentQueue = constants.SHARED_QUEUE;
const { range, selectionId } = await this.queuePriorityStrategy.nextCandidateSelection(
parentQueue
);
const queues = await this.#zrangeWithScores(parentQueue, range[0], range[1]);
const queuesWithScores = await this.#calculateQueueScores(queues, (queue) =>
this.#calculateMessageQueueCapacities(queue)
);
// We need to priority shuffle here to ensure all workers aren't just working on the highest priority queue
const choice = this.queuePriorityStrategy.chooseQueue(
queuesWithScores,
parentQueue,
selectionId
);
return {
selectionId,
queues,
queuesWithScores,
nextRange: range,
queueCount: queues.length,
queueChoice: choice,
};
}
/**
* Dequeue a message from the shared queue (this should be used in production environments)
*/