From 78d6541c1bd3dd719a664d062a52d521f5f7ce89 Mon Sep 17 00:00:00 2001 From: Inrixia Date: Thu, 4 Apr 2024 15:33:22 +1300 Subject: [PATCH] Fix downloadProgress cpu & timing & error logging --- package.json | 2 +- src/Downloader.ts | 19 ++++++++++++++----- src/lib/helpers/index.ts | 2 +- src/lib/helpers/rateLimit.ts | 9 --------- src/lib/logging/ProgressBars.ts | 14 ++++++++++---- src/lib/logging/ProgressLogger.ts | 7 +++---- 6 files changed, 29 insertions(+), 24 deletions(-) delete mode 100644 src/lib/helpers/rateLimit.ts diff --git a/package.json b/package.json index cf9fd47..379435c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "floatplane-plex-downloader", - "version": "5.11.4", + "version": "5.12.0", "private": true, "type": "module", "scripts": { diff --git a/src/Downloader.ts b/src/Downloader.ts index 0d5d353..1d9bf40 100644 --- a/src/Downloader.ts +++ b/src/Downloader.ts @@ -1,5 +1,6 @@ import { Counter, Gauge } from "prom-client"; import { Video } from "./lib/Video.js"; +import type { Progress } from "got"; import { settings, args } from "./lib/helpers/index.js"; import { MyPlexAccount } from "@ctrl/plex"; @@ -8,7 +9,6 @@ import { ProgressHeadless } from "./lib/logging/ProgressConsole.js"; import { ProgressBars } from "./lib/logging/ProgressBars.js"; import { promisify } from "util"; -import { rateLimit } from "./lib/helpers/rateLimit.js"; const sleep = promisify(setTimeout); const promQueued = new Gauge({ @@ -62,7 +62,7 @@ export class VideoDownloader { private static async processVideo(video: Video) { const logger = new this.ProgressLogger(video.title); - for (let retries = 0; retries < VideoDownloader.MaxRetries; retries++) { + for (let retries = 1; retries < VideoDownloader.MaxRetries + 1; retries++) { try { if (settings.extras.saveNfo) { logger.log("Saving .nfo"); @@ -78,10 +78,19 @@ export class VideoDownloader { logger.log("Waiting on delivery cdn..."); const downloadRequest = await video.download(settings.floatplane.videoResolution); - downloadRequest.on("downloadProgress", rateLimit(50, logger.onDownloadProgress.bind(logger))); + + let downloadInterval: NodeJS.Timeout; + downloadRequest.once("downloadProgress", (downloadProgress: Progress) => { + downloadInterval = setInterval(() => logger.onDownloadProgress(downloadRequest.downloadProgress), 125); + logger.onDownloadProgress(downloadProgress); + }); + await new Promise((res, rej) => { downloadRequest.once("end", res); downloadRequest.once("error", rej); + }).finally(() => { + clearInterval(downloadInterval); + logger.onDownloadProgress(downloadRequest.downloadProgress); }); } // eslint-disable-next-line no-fallthrough @@ -117,9 +126,9 @@ export class VideoDownloader { if (retries < VideoDownloader.MaxRetries) { logger.error(`${message} - Retrying in ${retries}s [${retries}/${this.MaxRetries}]`); // Wait between retries - await sleep(1000 * (retries + 1)); + await sleep(1000 * retries); } else { - logger.error(`${message} - Max Retries! [${retries}/${this.MaxRetries}]`); + logger.error(`${message} - Max Retries! [${retries}/${this.MaxRetries}]`, true); } } } diff --git a/src/lib/helpers/index.ts b/src/lib/helpers/index.ts index 689ab1e..262a9af 100644 --- a/src/lib/helpers/index.ts +++ b/src/lib/helpers/index.ts @@ -13,7 +13,7 @@ import "dotenv/config"; import json5 from "json5"; const { parse } = json5; -export const DownloaderVersion = "5.11.4"; +export const DownloaderVersion = "5.12.0"; import type { PartialArgs, Settings } from "../types.js"; diff --git a/src/lib/helpers/rateLimit.ts b/src/lib/helpers/rateLimit.ts deleted file mode 100644 index 0b776fa..0000000 --- a/src/lib/helpers/rateLimit.ts +++ /dev/null @@ -1,9 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/ban-types -export const rateLimit = (delay: number, func: Function) => { - let lastCall = 0; - return (...args: unknown[]) => { - const _now = Date.now(); - if (_now - lastCall >= delay) func(...args); - lastCall = _now; - }; -}; diff --git a/src/lib/logging/ProgressBars.ts b/src/lib/logging/ProgressBars.ts index 9fea6dd..ceb69f3 100644 --- a/src/lib/logging/ProgressBars.ts +++ b/src/lib/logging/ProgressBars.ts @@ -1,5 +1,6 @@ import { MultiProgressBars } from "multi-progress-bars"; import { ProgressLogger, type IProgressLogger } from "./ProgressLogger.js"; +import type { Progress } from "got"; export class ProgressBars extends ProgressLogger implements IProgressLogger { private static readonly _reset = "\u001b[0m"; @@ -39,14 +40,19 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger { public done() { ProgressBars._Bars.done(this.title); this.reset(); - setTimeout(() => ProgressBars._Bars.removeTask(this.title), 10000 + Math.floor(Math.random() * 6000)); + this.removeBar(); } - public error(message: string) { + public error(message: string, final?: true) { this.log(`${ProgressLogger.ERR}: ${message}`); this.reset(); + if (final) this.removeBar(); + } + private removeBar() { + setTimeout(() => ProgressBars._Bars.removeTask(this.title), 10000 + Math.floor(Math.random() * 6000)); } - public onDownloadProgress(progress: { total: number; transferred: number; percent: number }): void { + public onDownloadProgress(progress: Progress): void { + if (progress.total === undefined) return; ProgressBars.DownloadedBytes += progress.transferred - this.downloadedBytes; super.onDownloadProgress(progress); @@ -65,7 +71,7 @@ export class ProgressBars extends ProgressLogger implements IProgressLogger { const downloadETA = progress.total / this._downloadSpeed - elapsed; - const downloaded = `${ProgressBars.cy((this.downloadedBytes / 1000000).toFixed(2))}/${ProgressBars.cy(`${(progress.total / 1000000).toFixed(2)}MB`)}`; + const downloaded = `${ProgressBars.cy((progress.transferred / 1000000).toFixed(2))}/${ProgressBars.cy(`${(progress.total / 1000000).toFixed(2)}MB`)}`; const speed = `${ProgressBars.gr((this._downloadSpeed / 125000).toFixed(2) + "mb/s")}`; const eta = `ETA: ${ProgressBars.bl(`${Math.floor(downloadETA / 60)}m ${Math.floor(downloadETA) % 60}s`)}`; diff --git a/src/lib/logging/ProgressLogger.ts b/src/lib/logging/ProgressLogger.ts index 4cba005..2339d1f 100644 --- a/src/lib/logging/ProgressLogger.ts +++ b/src/lib/logging/ProgressLogger.ts @@ -1,12 +1,11 @@ import { Counter } from "prom-client"; - -type DownloadProgress = { total: number; transferred: number; percent: number }; +import type { Progress } from "got"; export interface IProgressLogger { readonly title: string; log(message: string): void; error(message: string): void; - onDownloadProgress(progress: DownloadProgress): void; + onDownloadProgress(progress: Progress): void; done(message: string): void; } @@ -23,7 +22,7 @@ export class ProgressLogger { help: "Video downloaded bytes", }); - public onDownloadProgress(progress: DownloadProgress) { + public onDownloadProgress(progress: Progress) { ProgressLogger._downloadedBytesTotalCounter.inc(progress.transferred - this.downloadedBytes); this.downloadedBytes = progress.transferred; }