improve torrent cancelationm

This commit is contained in:
SrGooglo 2024-06-30 20:17:55 +02:00
parent 21152ac248
commit 1e74f49e4e
3 changed files with 17 additions and 6 deletions

View File

@ -41,7 +41,8 @@ export default async (pkg, step, logger, abortController) => {
use_id_only: true, use_id_only: true,
status_text: `Downloaded ${progress.transferredString} / ${progress.totalString} | ${progress.speedString}/s`, status_text: `Downloaded ${progress.transferredString} / ${progress.totalString} | ${progress.speedString}/s`,
}) })
} },
taskId: pkg.id
}) })
} }

View File

@ -20,6 +20,10 @@ export default async function reinstall(pkg_id) {
if (task) { if (task) {
BaseLog.warn(`Task not found [${pkg_id}]`) BaseLog.warn(`Task not found [${pkg_id}]`)
await task.abortController.abort() await task.abortController.abort()
global._relic_eventBus.emit(`pkg:install:cancel`, pkg_id)
global._relic_eventBus.emit(`pkg:install:cancel:${pkg_id}`, pkg_id)
global._relic_eventBus.emit(`task:cancel:${pkg_id}`, pkg_id)
} }
await UninstallHandler(pkg_id) await UninstallHandler(pkg_id)

View File

@ -1,6 +1,3 @@
import fs from "node:fs"
import path from "node:path"
import cliProgress from "cli-progress"
import humanFormat from "human-format" import humanFormat from "human-format"
import aria2 from "aria2" import aria2 from "aria2"
@ -18,6 +15,7 @@ export default async function downloadTorrent(
onProgress, onProgress,
onDone, onDone,
onError, onError,
taskId,
} = {} } = {}
) { ) {
let progressInterval = null let progressInterval = null
@ -54,9 +52,17 @@ export default async function downloadTorrent(
onStart() onStart()
} }
if (typeof taskId === "string") {
// TODO: Unregister me when task is cancelled or finished
global._relic_eventBus.once(`task:cancel:${taskId}`, () => {
client.call("remove", [downloadId])
clearInterval(progressInterval)
reject()
})
}
progressInterval = setInterval(async () => { progressInterval = setInterval(async () => {
const data = await client.call("tellStatus", downloadId) const data = await client.call("tellStatus", downloadId)
const isMetadata = data.totalLength === "0" && data.status === "active"
console.log(data) console.log(data)