web: core system for queue & queen bee, move remux to new system
it's 3 am and i think i had a divine intervention
This commit is contained in:
36
web/src/lib/queen-bee/queue.ts
Normal file
36
web/src/lib/queen-bee/queue.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { addItem } from "$lib/state/queen-bee/queue";
|
||||
import type { CobaltPipelineItem } from "$lib/types/workers";
|
||||
|
||||
export const getMediaType = (type: string) => {
|
||||
const kind = type.split('/')[0];
|
||||
|
||||
// can't use .includes() here for some reason
|
||||
if (kind === "video" || kind === "audio" || kind === "image") {
|
||||
return kind;
|
||||
}
|
||||
}
|
||||
|
||||
export const createRemuxPipeline = (file: File) => {
|
||||
// chopped khia
|
||||
const parentId = crypto.randomUUID();
|
||||
const mediaType = getMediaType(file.type);
|
||||
|
||||
const pipeline: CobaltPipelineItem[] = [{
|
||||
worker: "remux",
|
||||
workerId: crypto.randomUUID(),
|
||||
parentId,
|
||||
workerArgs: {
|
||||
files: [file],
|
||||
},
|
||||
}];
|
||||
|
||||
if (mediaType) {
|
||||
addItem({
|
||||
id: parentId,
|
||||
state: "waiting",
|
||||
pipeline,
|
||||
filename: file.name,
|
||||
mediaType,
|
||||
})
|
||||
}
|
||||
}
|
||||
60
web/src/lib/queen-bee/run-worker.ts
Normal file
60
web/src/lib/queen-bee/run-worker.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import RemuxWorker from "$lib/workers/remux?worker";
|
||||
//import RemoveBgWorker from "$lib/workers/removebg?worker";
|
||||
|
||||
import type { CobaltPipelineItem } from "$lib/types/workers";
|
||||
import { itemDone, itemError } from "$lib/state/queen-bee/queue";
|
||||
|
||||
const workerError = (parentId: string, workerId: string, worker: Worker, error: string) => {
|
||||
itemError(parentId, workerId, error);
|
||||
worker.terminate();
|
||||
}
|
||||
|
||||
const workerSuccess = (parentId: string, workerId: string, worker: Worker, file: File) => {
|
||||
itemDone(parentId, workerId, file);
|
||||
worker.terminate();
|
||||
}
|
||||
|
||||
export const runRemuxWorker = async (workerId: string, parentId: string, file: File) => {
|
||||
const worker = new RemuxWorker();
|
||||
|
||||
worker.postMessage({ file });
|
||||
|
||||
worker.onerror = (e) => {
|
||||
console.error("remux worker exploded:", e);
|
||||
|
||||
// TODO: proper error code
|
||||
workerError(parentId, workerId, worker, "internal error");
|
||||
};
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
const eventData = event.data.cobaltRemuxWorker;
|
||||
if (!eventData) return;
|
||||
|
||||
console.log(eventData);
|
||||
|
||||
// TODO: calculate & use progress again
|
||||
|
||||
if (eventData.render) {
|
||||
return workerSuccess(
|
||||
parentId,
|
||||
workerId,
|
||||
worker,
|
||||
new File([eventData.render], eventData.filename, {
|
||||
type: eventData.render.type,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (eventData.error) {
|
||||
return workerError(parentId, workerId, worker, eventData.error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const startWorker = async ({ worker, workerId, parentId, workerArgs }: CobaltPipelineItem) => {
|
||||
switch (worker) {
|
||||
case "remux":
|
||||
await runRemuxWorker(workerId, parentId, workerArgs.files[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
41
web/src/lib/queen-bee/scheduler.ts
Normal file
41
web/src/lib/queen-bee/scheduler.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { get } from "svelte/store";
|
||||
import { itemRunning, queue } from "$lib/state/queen-bee/queue";
|
||||
import { startWorker } from "$lib/queen-bee/run-worker";
|
||||
import { addWorkerToQueue, currentTasks } from "$lib/state/queen-bee/current-tasks";
|
||||
|
||||
export const checkTasks = () => {
|
||||
const queueItems = get(queue);
|
||||
const ongoingTasks = get(currentTasks)
|
||||
|
||||
if (Object.keys(ongoingTasks).length > 0) return;
|
||||
|
||||
for (const item of Object.keys(queueItems)) {
|
||||
const task = queueItems[item];
|
||||
|
||||
if (task.state === "running") {
|
||||
break;
|
||||
}
|
||||
|
||||
if (task.state === "waiting") {
|
||||
for (let i = 0; i < task.pipeline.length; i++) {
|
||||
// TODO: loop here and pass the file between pipelines
|
||||
// or schedule several tasks one after another but within
|
||||
// one parent & pipeline
|
||||
const pipelineItem = task.pipeline[i];
|
||||
|
||||
startWorker(pipelineItem);
|
||||
|
||||
addWorkerToQueue({
|
||||
id: pipelineItem.workerId,
|
||||
parentId: task.id,
|
||||
step: i + 1,
|
||||
totalSteps: task.pipeline.length,
|
||||
});
|
||||
|
||||
itemRunning(task.id, i);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user