From f853a2a39e37c3b24e7e1c4893b9c8320015597b Mon Sep 17 00:00:00 2001 From: Hachi-R Date: Mon, 23 Dec 2024 14:07:03 -0300 Subject: [PATCH 1/3] feat: move initial seeding for DownloadManager --- python_rpc/main.py | 7 +++++++ src/main/main.ts | 13 ++++++++++--- src/main/services/download/download-manager.ts | 8 ++++++-- src/main/services/python-rpc.ts | 5 +++-- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/python_rpc/main.py b/python_rpc/main.py index 1c08e896..10633423 100644 --- a/python_rpc/main.py +++ b/python_rpc/main.py @@ -12,6 +12,7 @@ torrent_port = sys.argv[1] http_port = sys.argv[2] rpc_password = sys.argv[3] start_download_payload = sys.argv[4] +start_seeding_payload = sys.argv[5] downloads = {} # This can be streamed down from Node @@ -32,6 +33,12 @@ if start_download_payload: downloads[initial_download['game_id']] = http_downloader http_downloader.start_download(initial_download['url'], initial_download['save_path'], initial_download.get('header')) +if start_seeding_payload: + initial_seeding = json.loads(urllib.parse.unquote(start_seeding_payload)) + for seed in initial_seeding: + torrent_downloader = TorrentDownloader(torrent_session) + downloads[seed['game_id']] = torrent_downloader + torrent_downloader.start_download(seed['url'], seed['save_path'], "") def validate_rpc_password(): """Middleware to validate RPC password.""" diff --git a/src/main/main.ts b/src/main/main.ts index c39a5196..19110a0d 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -1,6 +1,7 @@ import { DownloadManager, Ludusavi, startMainLoop } from "./services"; import { downloadQueueRepository, + gameRepository, userPreferencesRepository, } from "./repository"; import { UserPreferences } from "./entity"; @@ -35,14 +36,20 @@ const loadState = async (userPreferences: UserPreferences | null) => { }, }); + const seedList = await gameRepository.find({ + where: { + shouldSeed: true, + downloader: 1, + progress: 1, + }, + }); + if (nextQueueItem?.game.status === "active") { - DownloadManager.startRPC(nextQueueItem.game); + DownloadManager.startRPC(nextQueueItem.game, seedList); } else { PythonRPC.spawn(); } - await startSeedProcess(); - startMainLoop(); }; diff --git a/src/main/services/download/download-manager.ts b/src/main/services/download/download-manager.ts index 9e32a742..4aae5808 100644 --- a/src/main/services/download/download-manager.ts +++ b/src/main/services/download/download-manager.ts @@ -24,13 +24,17 @@ import { logger } from "../logger"; export class DownloadManager { private static downloadingGameId: number | null = null; - public static startRPC(game: Game) { + public static startRPC(game: Game, initialSeeding?: Game[]) { if (game && game.status === "active") { PythonRPC.spawn({ game_id: game.id, url: game.uri!, save_path: game.downloadPath!, - }); + }, initialSeeding?.map((game) => ({ + game_id: game.id, + url: game.uri!, + save_path: game.downloadPath!, + }))); this.downloadingGameId = game.id; } diff --git a/src/main/services/python-rpc.ts b/src/main/services/python-rpc.ts index 84898c65..dc03f0de 100644 --- a/src/main/services/python-rpc.ts +++ b/src/main/services/python-rpc.ts @@ -9,7 +9,7 @@ import { logger } from "./logger"; import { Readable } from "node:stream"; import { app, dialog } from "electron"; -interface StartDownloadPayload { +interface GamePayload { game_id: number; url: string; save_path: string; @@ -42,12 +42,13 @@ export class PythonRPC { readable.on("data", logger.log); } - public static spawn(initialDownload?: StartDownloadPayload) { + public static spawn(initialDownload?: GamePayload, initialSeeding?: GamePayload[]) { const commonArgs = [ this.BITTORRENT_PORT, this.RPC_PORT, this.RPC_PASSWORD, initialDownload ? JSON.stringify(initialDownload) : "", + initialSeeding ? JSON.stringify(initialSeeding) : "", ]; if (app.isPackaged) { From 230c24cca5ac4926b8e8125cbe26efeba99d5ddc Mon Sep 17 00:00:00 2001 From: Hachi-R Date: Mon, 23 Dec 2024 14:07:15 -0300 Subject: [PATCH 2/3] lint --- .../services/download/download-manager.ts | 21 +++++++++++-------- src/main/services/python-rpc.ts | 5 ++++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/services/download/download-manager.ts b/src/main/services/download/download-manager.ts index 4aae5808..3a8da5b4 100644 --- a/src/main/services/download/download-manager.ts +++ b/src/main/services/download/download-manager.ts @@ -26,15 +26,18 @@ export class DownloadManager { public static startRPC(game: Game, initialSeeding?: Game[]) { if (game && game.status === "active") { - PythonRPC.spawn({ - game_id: game.id, - url: game.uri!, - save_path: game.downloadPath!, - }, initialSeeding?.map((game) => ({ - game_id: game.id, - url: game.uri!, - save_path: game.downloadPath!, - }))); + PythonRPC.spawn( + { + game_id: game.id, + url: game.uri!, + save_path: game.downloadPath!, + }, + initialSeeding?.map((game) => ({ + game_id: game.id, + url: game.uri!, + save_path: game.downloadPath!, + })) + ); this.downloadingGameId = game.id; } diff --git a/src/main/services/python-rpc.ts b/src/main/services/python-rpc.ts index dc03f0de..19622a99 100644 --- a/src/main/services/python-rpc.ts +++ b/src/main/services/python-rpc.ts @@ -42,7 +42,10 @@ export class PythonRPC { readable.on("data", logger.log); } - public static spawn(initialDownload?: GamePayload, initialSeeding?: GamePayload[]) { + public static spawn( + initialDownload?: GamePayload, + initialSeeding?: GamePayload[] + ) { const commonArgs = [ this.BITTORRENT_PORT, this.RPC_PORT, From 17879f9c70f1ee75289116cc1b3c8565a40a5ee0 Mon Sep 17 00:00:00 2001 From: Hachi-R Date: Mon, 23 Dec 2024 14:28:24 -0300 Subject: [PATCH 3/3] refactor: remove unused seed process functionality from main application --- src/main/main.ts | 1 - src/main/services/seed.ts | 21 --------------------- 2 files changed, 22 deletions(-) delete mode 100644 src/main/services/seed.ts diff --git a/src/main/main.ts b/src/main/main.ts index 19110a0d..8c566123 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -9,7 +9,6 @@ import { RealDebridClient } from "./services/download/real-debrid"; import { HydraApi } from "./services/hydra-api"; import { uploadGamesBatch } from "./services/library-sync"; import { Aria2 } from "./services/aria2"; -import { startSeedProcess } from "./services/seed"; import { PythonRPC } from "./services/python-rpc"; const loadState = async (userPreferences: UserPreferences | null) => { diff --git a/src/main/services/seed.ts b/src/main/services/seed.ts deleted file mode 100644 index 89172f50..00000000 --- a/src/main/services/seed.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { gameRepository } from "@main/repository"; -import { DownloadManager } from "./download/download-manager"; -import { sleep } from "@main/helpers"; - -export const startSeedProcess = async () => { - const seedList = await gameRepository.find({ - where: { - shouldSeed: true, - downloader: 1, - progress: 1, - }, - }); - - if (seedList.length === 0) return; - await sleep(1000); - - seedList.map(async (game) => { - await DownloadManager.startDownload(game); - await sleep(300); - }); -};