83 lines
3 KiB
TypeScript
83 lines
3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { getPipelineQueue, getDownloadQueue, getValhallaQueue, getValhallaTransitQueue } from "@/lib/queue";
|
|
import type { JobSummary } from "@transportationer/shared";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
export async function GET() {
|
|
const [pQueue, dQueue, vQueue, vtQueue] = [getPipelineQueue(), getDownloadQueue(), getValhallaQueue(), getValhallaTransitQueue()];
|
|
|
|
const [
|
|
pWaiting, pWaitingChildren, pActive, pCompleted, pFailed,
|
|
dWaiting, dWaitingChildren, dActive, dCompleted, dFailed,
|
|
vWaiting, vWaitingChildren, vActive, vCompleted, vFailed,
|
|
vtWaiting, vtWaitingChildren, vtActive, vtCompleted, vtFailed,
|
|
] = await Promise.all([
|
|
pQueue.getWaiting(0, 20),
|
|
pQueue.getWaitingChildren(0, 20),
|
|
pQueue.getActive(0, 20),
|
|
pQueue.getCompleted(0, 20),
|
|
pQueue.getFailed(0, 20),
|
|
dQueue.getWaiting(0, 20),
|
|
dQueue.getWaitingChildren(0, 20),
|
|
dQueue.getActive(0, 20),
|
|
dQueue.getCompleted(0, 20),
|
|
dQueue.getFailed(0, 20),
|
|
vQueue.getWaiting(0, 20),
|
|
vQueue.getWaitingChildren(0, 20),
|
|
vQueue.getActive(0, 20),
|
|
vQueue.getCompleted(0, 20),
|
|
vQueue.getFailed(0, 20),
|
|
vtQueue.getWaiting(0, 20),
|
|
vtQueue.getWaitingChildren(0, 20),
|
|
vtQueue.getActive(0, 20),
|
|
vtQueue.getCompleted(0, 20),
|
|
vtQueue.getFailed(0, 20),
|
|
]);
|
|
|
|
const waitingChildren = [...pWaitingChildren, ...dWaitingChildren, ...vWaitingChildren, ...vtWaitingChildren];
|
|
const waiting = [...pWaiting, ...dWaiting, ...vWaiting, ...vtWaiting];
|
|
const active = [...pActive, ...dActive, ...vActive, ...vtActive];
|
|
const completed = [...pCompleted, ...dCompleted, ...vCompleted, ...vtCompleted];
|
|
const failed = [...pFailed, ...dFailed, ...vFailed, ...vtFailed];
|
|
|
|
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);
|
|
}
|