mirror of https://github.com/VERT-sh/VERT.git
feat: job cancellation for vertd
This commit is contained in:
parent
1bcf21d94c
commit
056baee469
|
|
@ -190,16 +190,16 @@ export class FFmpegConverter extends Converter {
|
||||||
public async cancel(input: VertFile): Promise<void> {
|
public async cancel(input: VertFile): Promise<void> {
|
||||||
const ffmpeg = this.activeConversions.get(input.id);
|
const ffmpeg = this.activeConversions.get(input.id);
|
||||||
if (!ffmpeg) {
|
if (!ffmpeg) {
|
||||||
log(
|
error(
|
||||||
["converters", this.name],
|
["converters", this.name],
|
||||||
`No active conversion found for file ${input.name}`,
|
`no active conversion found for file ${input.name}`,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log(
|
log(
|
||||||
["converters", this.name],
|
["converters", this.name],
|
||||||
`Cancelling conversion for file ${input.name}`,
|
`cancelling conversion for file ${input.name}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
ffmpeg.terminate();
|
ffmpeg.terminate();
|
||||||
|
|
|
||||||
|
|
@ -231,16 +231,16 @@ export class MagickConverter extends Converter {
|
||||||
public async cancel(input: VertFile): Promise<void> {
|
public async cancel(input: VertFile): Promise<void> {
|
||||||
const worker = this.activeConversions.get(input.id);
|
const worker = this.activeConversions.get(input.id);
|
||||||
if (!worker) {
|
if (!worker) {
|
||||||
log(
|
error(
|
||||||
["converters", this.name],
|
["converters", this.name],
|
||||||
`No active conversion found for file ${input.name}`,
|
`no active conversion found for file ${input.name}`,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log(
|
log(
|
||||||
["converters", this.name],
|
["converters", this.name],
|
||||||
`Cancelling conversion for file ${input.name}`,
|
`cancelling conversion for file ${input.name}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { Converter, FormatInfo } from "./converter.svelte";
|
||||||
import { browser } from "$app/environment";
|
import { browser } from "$app/environment";
|
||||||
import PandocWorker from "$lib/workers/pandoc?worker&url";
|
import PandocWorker from "$lib/workers/pandoc?worker&url";
|
||||||
import { addToast } from "$lib/store/ToastProvider";
|
import { addToast } from "$lib/store/ToastProvider";
|
||||||
import { log } from "$lib/logger";
|
import { error, log } from "$lib/logger";
|
||||||
|
|
||||||
export class PandocConverter extends Converter {
|
export class PandocConverter extends Converter {
|
||||||
public name = "pandoc";
|
public name = "pandoc";
|
||||||
|
|
@ -105,16 +105,16 @@ export class PandocConverter extends Converter {
|
||||||
public async cancel(input: VertFile): Promise<void> {
|
public async cancel(input: VertFile): Promise<void> {
|
||||||
const worker = this.activeConversions.get(input.id);
|
const worker = this.activeConversions.get(input.id);
|
||||||
if (!worker) {
|
if (!worker) {
|
||||||
log(
|
error(
|
||||||
["converters", this.name],
|
["converters", this.name],
|
||||||
`No active conversion found for file ${input.name}`,
|
`no active conversion found for file ${input.name}`,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log(
|
log(
|
||||||
["converters", this.name],
|
["converters", this.name],
|
||||||
`Cancelling conversion for file ${input.name}`,
|
`cancelling conversion for file ${input.name}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { log } from "$lib/logger";
|
import { error, log } from "$lib/logger";
|
||||||
import { Settings } from "$lib/sections/settings/index.svelte";
|
import { Settings } from "$lib/sections/settings/index.svelte";
|
||||||
import { VertFile } from "$lib/types";
|
import { VertFile } from "$lib/types";
|
||||||
import { Converter, FormatInfo } from "./converter.svelte";
|
import { Converter, FormatInfo } from "./converter.svelte";
|
||||||
|
|
@ -90,6 +90,21 @@ interface CompletedMessage {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CancelJobMessage {
|
||||||
|
type: "cancelJob";
|
||||||
|
data: {
|
||||||
|
jobId: string;
|
||||||
|
token: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface JobCancelledMessage {
|
||||||
|
type: "jobCancelled";
|
||||||
|
data: {
|
||||||
|
jobId: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
interface FpsProgress {
|
interface FpsProgress {
|
||||||
type: "fps";
|
type: "fps";
|
||||||
data: number;
|
data: number;
|
||||||
|
|
@ -106,6 +121,8 @@ type VertdMessage =
|
||||||
| StartJobMessage
|
| StartJobMessage
|
||||||
| ErrorMessage
|
| ErrorMessage
|
||||||
| ProgressMessage
|
| ProgressMessage
|
||||||
|
| CancelJobMessage
|
||||||
|
| JobCancelledMessage
|
||||||
| CompletedMessage;
|
| CompletedMessage;
|
||||||
|
|
||||||
const progressEstimates = {
|
const progressEstimates = {
|
||||||
|
|
@ -202,6 +219,15 @@ export class VertdConverter extends Converter {
|
||||||
public ready = $state(false);
|
public ready = $state(false);
|
||||||
public reportsProgress = true;
|
public reportsProgress = true;
|
||||||
|
|
||||||
|
private activeConversions = new Map<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
ws: WebSocket;
|
||||||
|
jobId: string;
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
>();
|
||||||
|
|
||||||
public supportedFormats = [
|
public supportedFormats = [
|
||||||
new FormatInfo("mkv", true, true),
|
new FormatInfo("mkv", true, true),
|
||||||
new FormatInfo("mp4", true, true),
|
new FormatInfo("mp4", true, true),
|
||||||
|
|
@ -253,6 +279,13 @@ export class VertdConverter extends Converter {
|
||||||
const ws = new WebSocket(
|
const ws = new WebSocket(
|
||||||
`${protocol}//${apiUrl.replace("http://", "").replace("https://", "")}/api/ws`,
|
`${protocol}//${apiUrl.replace("http://", "").replace("https://", "")}/api/ws`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.activeConversions.set(input.id, {
|
||||||
|
ws,
|
||||||
|
jobId: uploadRes.id,
|
||||||
|
token: uploadRes.auth,
|
||||||
|
});
|
||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
const speed = Settings.instance.settings.vertdSpeed;
|
const speed = Settings.instance.settings.vertdSpeed;
|
||||||
const keepMetadata = Settings.instance.settings.metadata;
|
const keepMetadata = Settings.instance.settings.metadata;
|
||||||
|
|
@ -289,6 +322,7 @@ export class VertdConverter extends Converter {
|
||||||
case "jobFinished": {
|
case "jobFinished": {
|
||||||
this.log("job finished");
|
this.log("job finished");
|
||||||
ws.close();
|
ws.close();
|
||||||
|
this.activeConversions.delete(input.id);
|
||||||
const url = `${apiUrl}/api/download/${msg.data.jobId}/${uploadRes.auth}`;
|
const url = `${apiUrl}/api/download/${msg.data.jobId}/${uploadRes.auth}`;
|
||||||
this.log(`downloading from ${url}`);
|
this.log(`downloading from ${url}`);
|
||||||
// const res = await fetch(url).then((res) => res.blob());
|
// const res = await fetch(url).then((res) => res.blob());
|
||||||
|
|
@ -297,8 +331,17 @@ export class VertdConverter extends Converter {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "jobCancelled": {
|
||||||
|
this.log("job cancelled");
|
||||||
|
ws.close();
|
||||||
|
this.activeConversions.delete(input.id);
|
||||||
|
reject("Conversion cancelled");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case "error": {
|
case "error": {
|
||||||
this.log(`error: ${msg.data.message}`);
|
this.log(`error: ${msg.data.message}`);
|
||||||
|
this.activeConversions.delete(input.id);
|
||||||
reject(msg.data.message);
|
reject(msg.data.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -306,6 +349,39 @@ export class VertdConverter extends Converter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async cancel(input: VertFile): Promise<void> {
|
||||||
|
const activeConversion = this.activeConversions.get(input.id);
|
||||||
|
if (!activeConversion) {
|
||||||
|
error(
|
||||||
|
["converters", this.name],
|
||||||
|
`no active conversion found for file ${input.name}`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log(
|
||||||
|
["converters", this.name],
|
||||||
|
`cancelling conversion for file ${input.name}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const { ws, jobId, token } = activeConversion;
|
||||||
|
|
||||||
|
if (ws.readyState === WebSocket.OPEN) {
|
||||||
|
const cancelMsg: CancelJobMessage = {
|
||||||
|
type: "cancelJob",
|
||||||
|
data: {
|
||||||
|
jobId,
|
||||||
|
token,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
ws.send(JSON.stringify(cancelMsg));
|
||||||
|
this.log("sent cancelJob message");
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.close();
|
||||||
|
this.activeConversions.delete(input.id);
|
||||||
|
}
|
||||||
|
|
||||||
public async valid(): Promise<boolean> {
|
public async valid(): Promise<boolean> {
|
||||||
if (!Settings.instance.settings.vertdURL) {
|
if (!Settings.instance.settings.vertdURL) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue