diff --git a/.gitignore b/.gitignore index 7bd76930..65bbab97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .vscode node_modules -aria2/ +hydra-download-manager/ fastlist.exe __pycache__ dist diff --git a/src/main/events/library/delete-game-folder.ts b/src/main/events/library/delete-game-folder.ts index 533e0063..bdae9b3e 100644 --- a/src/main/events/library/delete-game-folder.ts +++ b/src/main/events/library/delete-game-folder.ts @@ -45,10 +45,6 @@ const deleteGameFolder = async ( reject(); } - const aria2ControlFilePath = `${folderPath}.aria2`; - if (fs.existsSync(aria2ControlFilePath)) - fs.rmSync(aria2ControlFilePath); - resolve(); } ); diff --git a/src/main/services/download/torrent-downloader.ts b/src/main/services/download/torrent-downloader.ts index 3e509bf5..0ea4f01d 100644 --- a/src/main/services/download/torrent-downloader.ts +++ b/src/main/services/download/torrent-downloader.ts @@ -7,6 +7,8 @@ import { DownloadProgress } from "@types"; import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"; import { calculateETA } from "./helpers"; import axios from "axios"; +import { sleep } from "@main/helpers"; +import { logger } from "../logger"; enum LibtorrentStatus { CheckingFiles = 1, @@ -35,8 +37,29 @@ export class TorrentDownloader { baseURL: `http://localhost:${RPC_PORT}`, }); - private static spawn() { - this.torrentClient = startTorrentClient(); + private static async healthCheck(retries = 15) { + try { + await this.rpc.get("/healthcheck"); + } catch (err) { + if (retries === 0) { + throw new Error("Failed to connect to libtorrent client"); + } + + await sleep(200); + + return this.healthCheck(retries - 1); + } + } + + private static async spawn() { + try { + this.torrentClient = startTorrentClient(); + await this.healthCheck(); + + logger.log("libtorrent client started"); + } catch (err) { + logger.error(err); + } } public static kill() { @@ -75,7 +98,7 @@ export class TorrentDownloader { const isCheckingFiles = status === LibtorrentStatus.CheckingFiles; - if (!isDownloadingMetadata) { + if (!isDownloadingMetadata && !isCheckingFiles) { const update: QueryDeepPartialEntity = { bytesDownloaded, fileSize, @@ -111,20 +134,18 @@ export class TorrentDownloader { } static async pauseDownload() { - if (!this.torrentClient) this.spawn(); + if (!this.torrentClient) await this.spawn(); - await this.rpc - .post("/action", { - action: "pause", - game_id: this.downloadingGameId, - }) - .catch(() => {}); + await this.rpc.post("/action", { + action: "pause", + game_id: this.downloadingGameId, + }); this.downloadingGameId = -1; } static async startDownload(game: Game) { - if (!this.torrentClient) this.spawn(); + if (!this.torrentClient) await this.spawn(); await this.rpc.post("/action", { action: "start", @@ -137,7 +158,7 @@ export class TorrentDownloader { } static async cancelDownload(gameId: number) { - if (!this.torrentClient) this.spawn(); + if (!this.torrentClient) await this.spawn(); await this.rpc.post("/action", { action: "cancel", diff --git a/torrent-client/main.py b/torrent-client/main.py index 1c7ccfa0..19f38f6a 100644 --- a/torrent-client/main.py +++ b/torrent-client/main.py @@ -72,6 +72,9 @@ class Handler(BaseHTTPRequestHandler): status = downloader.get_download_status() self.wfile.write(json.dumps(status).encode('utf-8')) + if self.path == "/healthcheck": + self.send_response(200) + self.end_headers() def do_POST(self): if self.path == "/action":