70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { getPipelineQueue, getValhallaQueue } from "@/lib/queue";
|
|
import type { JobSummary } from "@transportationer/shared";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
export async function GET() {
|
|
const [pQueue, vQueue] = [getPipelineQueue(), getValhallaQueue()];
|
|
|
|
const [
|
|
pWaiting, pWaitingChildren, pActive, pCompleted, pFailed,
|
|
vWaiting, vActive, vCompleted, vFailed,
|
|
] = await Promise.all([
|
|
pQueue.getWaiting(0, 20),
|
|
pQueue.getWaitingChildren(0, 20),
|
|
pQueue.getActive(0, 20),
|
|
pQueue.getCompleted(0, 20),
|
|
pQueue.getFailed(0, 20),
|
|
vQueue.getWaiting(0, 20),
|
|
vQueue.getActive(0, 20),
|
|
vQueue.getCompleted(0, 20),
|
|
vQueue.getFailed(0, 20),
|
|
]);
|
|
|
|
const waitingChildren = [...pWaitingChildren];
|
|
const waiting = [...pWaiting, ...vWaiting];
|
|
const active = [...pActive, ...vActive];
|
|
const completed = [...pCompleted, ...vCompleted];
|
|
const failed = [...pFailed, ...vFailed];
|
|
|
|
const all = [...active, ...waitingChildren, ...waiting, ...completed, ...failed];
|
|
|
|
const jobs: JobSummary[] = all.map((job) => ({
|
|
id: job.id ?? "",
|
|
type: job.data.type,
|
|
citySlug: job.data.citySlug,
|
|
state: "waiting" as JobSummary["state"], // overridden below
|
|
progress: (job.progress as any) ?? null,
|
|
failedReason: job.failedReason ?? null,
|
|
createdAt: job.timestamp,
|
|
finishedAt: job.finishedOn ?? null,
|
|
duration:
|
|
job.finishedOn && job.processedOn
|
|
? job.finishedOn - job.processedOn
|
|
: null,
|
|
}));
|
|
|
|
// Tag states
|
|
for (const job of active) {
|
|
const found = jobs.find((j) => j.id === (job.id ?? ""));
|
|
if (found) found.state = "active";
|
|
}
|
|
for (const job of waitingChildren) {
|
|
const found = jobs.find((j) => j.id === (job.id ?? ""));
|
|
if (found) found.state = "waiting-children";
|
|
}
|
|
for (const job of completed) {
|
|
const found = jobs.find((j) => j.id === (job.id ?? ""));
|
|
if (found) found.state = "completed";
|
|
}
|
|
for (const job of failed) {
|
|
const found = jobs.find((j) => j.id === (job.id ?? ""));
|
|
if (found) found.state = "failed";
|
|
}
|
|
|
|
// Sort newest first
|
|
jobs.sort((a, b) => b.createdAt - a.createdAt);
|
|
|
|
return NextResponse.json(jobs);
|
|
}
|