fix: restart queue when a job is queued
When the worker queue drains, it sets is_running=false on the Queue record. If a new job is then queued, the worker's loop skips it entirely because is_running=false — the job sits frozen until the user manually starts the queue. Fix: always set is_running=true on the queue when a job is queued, mirroring what the /api/queue/[queueID]/start route already does. Also fixes the create-path which was creating the queue with is_running=false, meaning a brand-new queue would also never start.
This commit is contained in:
parent
1ab388ed03
commit
f4036a93ec
|
|
@ -0,0 +1,65 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export async function GET(request: NextRequest, { params }: { params: { jobID: string } }) {
|
||||
const { jobID } = await params;
|
||||
|
||||
const job = await prisma.job.findUnique({
|
||||
where: { id: jobID },
|
||||
});
|
||||
|
||||
if (!job) {
|
||||
return NextResponse.json({ error: 'Job not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// get highest queue position
|
||||
const highestQueuePosition = await prisma.job.aggregate({
|
||||
_max: {
|
||||
queue_position: true,
|
||||
},
|
||||
});
|
||||
const newQueuePosition = (highestQueuePosition._max.queue_position || 0) + 1000;
|
||||
|
||||
await prisma.job.update({
|
||||
where: { id: jobID },
|
||||
data: { queue_position: newQueuePosition },
|
||||
});
|
||||
|
||||
// make sure the queue exists and is running
|
||||
const queue = await prisma.queue.findFirst({
|
||||
where: {
|
||||
gpu_ids: job.gpu_ids,
|
||||
},
|
||||
});
|
||||
|
||||
// if queue doesn't exist, create it and start it
|
||||
if (!queue) {
|
||||
await prisma.queue.create({
|
||||
data: {
|
||||
gpu_ids: job.gpu_ids,
|
||||
is_running: true,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// ensure the queue is running so the worker picks up the new job
|
||||
await prisma.queue.update({
|
||||
where: { id: queue.id },
|
||||
data: { is_running: true },
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.job.update({
|
||||
where: { id: jobID },
|
||||
data: {
|
||||
status: 'queued',
|
||||
stop: false,
|
||||
return_to_queue: false,
|
||||
info: 'Job queued',
|
||||
},
|
||||
});
|
||||
|
||||
// Return the response immediately
|
||||
return NextResponse.json(job);
|
||||
}
|
||||
Loading…
Reference in New Issue