Skip to content

Commit

Permalink
Fix downloadProgress cpu & timing & error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Apr 4, 2024
1 parent aba04c5 commit 78d6541
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "floatplane-plex-downloader",
"version": "5.11.4",
"version": "5.12.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
19 changes: 14 additions & 5 deletions src/Downloader.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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({
Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
9 changes: 0 additions & 9 deletions src/lib/helpers/rateLimit.ts

This file was deleted.

14 changes: 10 additions & 4 deletions src/lib/logging/ProgressBars.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);

Expand All @@ -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`)}`;

Expand Down
7 changes: 3 additions & 4 deletions src/lib/logging/ProgressLogger.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 78d6541

Please sign in to comment.