feat: adding healthcheck

This commit is contained in:
Chubby Granny Chaser 2024-06-27 18:46:59 +01:00
parent ccaea88a88
commit 7cd121cb80
No known key found for this signature in database
4 changed files with 37 additions and 17 deletions

2
.gitignore vendored
View file

@ -1,6 +1,6 @@
.vscode .vscode
node_modules node_modules
aria2/ hydra-download-manager/
fastlist.exe fastlist.exe
__pycache__ __pycache__
dist dist

View file

@ -45,10 +45,6 @@ const deleteGameFolder = async (
reject(); reject();
} }
const aria2ControlFilePath = `${folderPath}.aria2`;
if (fs.existsSync(aria2ControlFilePath))
fs.rmSync(aria2ControlFilePath);
resolve(); resolve();
} }
); );

View file

@ -7,6 +7,8 @@ import { DownloadProgress } from "@types";
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity"; import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
import { calculateETA } from "./helpers"; import { calculateETA } from "./helpers";
import axios from "axios"; import axios from "axios";
import { sleep } from "@main/helpers";
import { logger } from "../logger";
enum LibtorrentStatus { enum LibtorrentStatus {
CheckingFiles = 1, CheckingFiles = 1,
@ -35,8 +37,29 @@ export class TorrentDownloader {
baseURL: `http://localhost:${RPC_PORT}`, baseURL: `http://localhost:${RPC_PORT}`,
}); });
private static spawn() { 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(); this.torrentClient = startTorrentClient();
await this.healthCheck();
logger.log("libtorrent client started");
} catch (err) {
logger.error(err);
}
} }
public static kill() { public static kill() {
@ -75,7 +98,7 @@ export class TorrentDownloader {
const isCheckingFiles = status === LibtorrentStatus.CheckingFiles; const isCheckingFiles = status === LibtorrentStatus.CheckingFiles;
if (!isDownloadingMetadata) { if (!isDownloadingMetadata && !isCheckingFiles) {
const update: QueryDeepPartialEntity<Game> = { const update: QueryDeepPartialEntity<Game> = {
bytesDownloaded, bytesDownloaded,
fileSize, fileSize,
@ -111,20 +134,18 @@ export class TorrentDownloader {
} }
static async pauseDownload() { static async pauseDownload() {
if (!this.torrentClient) this.spawn(); if (!this.torrentClient) await this.spawn();
await this.rpc await this.rpc.post("/action", {
.post("/action", {
action: "pause", action: "pause",
game_id: this.downloadingGameId, game_id: this.downloadingGameId,
}) });
.catch(() => {});
this.downloadingGameId = -1; this.downloadingGameId = -1;
} }
static async startDownload(game: Game) { static async startDownload(game: Game) {
if (!this.torrentClient) this.spawn(); if (!this.torrentClient) await this.spawn();
await this.rpc.post("/action", { await this.rpc.post("/action", {
action: "start", action: "start",
@ -137,7 +158,7 @@ export class TorrentDownloader {
} }
static async cancelDownload(gameId: number) { static async cancelDownload(gameId: number) {
if (!this.torrentClient) this.spawn(); if (!this.torrentClient) await this.spawn();
await this.rpc.post("/action", { await this.rpc.post("/action", {
action: "cancel", action: "cancel",

View file

@ -72,6 +72,9 @@ class Handler(BaseHTTPRequestHandler):
status = downloader.get_download_status() status = downloader.get_download_status()
self.wfile.write(json.dumps(status).encode('utf-8')) self.wfile.write(json.dumps(status).encode('utf-8'))
if self.path == "/healthcheck":
self.send_response(200)
self.end_headers()
def do_POST(self): def do_POST(self):
if self.path == "/action": if self.path == "/action":