|
|
import { get } from "svelte/store"; |
|
|
import { startWorker } from "$lib/task-manager/run-worker"; |
|
|
import { addWorkerToQueue, currentTasks } from "$lib/state/task-manager/current-tasks"; |
|
|
import { itemDone, itemError, itemRunning, queue } from "$lib/state/task-manager/queue"; |
|
|
|
|
|
import type { CobaltPipelineItem } from "$lib/types/workers"; |
|
|
|
|
|
const startPipeline = (pipelineItem: CobaltPipelineItem) => { |
|
|
addWorkerToQueue(pipelineItem.workerId, { |
|
|
type: pipelineItem.worker, |
|
|
parentId: pipelineItem.parentId, |
|
|
}); |
|
|
|
|
|
itemRunning(pipelineItem.parentId); |
|
|
startWorker(pipelineItem); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export const schedule = () => { |
|
|
const queueItems = get(queue); |
|
|
const ongoingTasks = get(currentTasks); |
|
|
|
|
|
for (const task of Object.values(queueItems)) { |
|
|
if (task.state === "running") { |
|
|
const finalWorker = task.pipeline[task.pipeline.length - 1]; |
|
|
|
|
|
|
|
|
|
|
|
if (Object.keys(task.pipelineResults).length === task.pipeline.length) { |
|
|
|
|
|
|
|
|
const finalFile = task.pipelineResults[finalWorker.workerId]; |
|
|
delete task.pipelineResults[finalWorker.workerId]; |
|
|
|
|
|
if (finalFile) { |
|
|
itemDone(task.id, finalFile); |
|
|
} else { |
|
|
itemError(task.id, finalWorker.workerId, "queue.no_final_file"); |
|
|
} |
|
|
|
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (const worker of task.pipeline) { |
|
|
if (task.pipelineResults[worker.workerId] || ongoingTasks[worker.workerId]) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
const needsToWait = worker.dependsOn?.some(id => !task.pipelineResults[id]); |
|
|
if (needsToWait) { |
|
|
break; |
|
|
} |
|
|
|
|
|
startPipeline(worker); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
else if (task.state === "waiting" && task.pipeline.length > 0 && Object.keys(ongoingTasks).length === 0) { |
|
|
|
|
|
|
|
|
for (const task of Object.values(queueItems)) { |
|
|
if (task.state === "running") return; |
|
|
} |
|
|
|
|
|
startPipeline(task.pipeline[0]); |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
|