feat: using retry system to connect to aria2

This commit is contained in:
Chubby Granny Chaser 2024-05-29 21:50:35 +01:00
parent 85516c1744
commit ffb3d79954
No known key found for this signature in database
34 changed files with 243 additions and 317 deletions

View file

@ -1,27 +0,0 @@
import path from "node:path";
import { spawn } from "node:child_process";
import type { ChildProcessWithoutNullStreams } from "node:child_process";
import { app } from "electron";
export const startAria2 = (): Promise<ChildProcessWithoutNullStreams> => {
return new Promise((resolve) => {
const binaryPath = app.isPackaged
? path.join(process.resourcesPath, "aria2", "aria2c")
: path.join(__dirname, "..", "..", "aria2", "aria2c");
const cp = spawn(binaryPath, [
"--enable-rpc",
"--rpc-listen-all",
"--file-allocation=none",
"--allow-overwrite=true",
]);
cp.stdout.on("data", async (data) => {
const msg = Buffer.from(data).toString("utf-8");
if (msg.includes("IPv6 RPC: listening on TCP")) {
resolve(cp);
}
});
});
};

View file

@ -0,0 +1,20 @@
import path from "node:path";
import { spawn } from "node:child_process";
import { app } from "electron";
export const startAria2 = () => {
const binaryPath = app.isPackaged
? path.join(process.resourcesPath, "aria2", "aria2c")
: path.join(__dirname, "..", "..", "aria2", "aria2c");
return spawn(
binaryPath,
[
"--enable-rpc",
"--rpc-listen-all",
"--file-allocation=none",
"--allow-overwrite=true",
],
{ stdio: "inherit" }
);
};

View file

@ -10,7 +10,9 @@ import { Downloader } from "@shared";
import { DownloadProgress } from "@types";
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
import { Game } from "@main/entity";
import { startAria2 } from "./aria2";
import { startAria2 } from "./aria2c";
import { sleep } from "@main/helpers";
import { logger } from "./logger";
export class DownloadManager {
private static downloads = new Map<number, string>();
@ -23,9 +25,22 @@ export class DownloadManager {
private static aria2 = new Aria2({});
private static async connect() {
await startAria2();
await this.aria2.open();
this.connected = true;
startAria2();
let retries = 0;
while (retries < 4 && !this.connected) {
try {
await this.aria2.open();
logger.log("Connected to aria2");
this.connected = true;
} catch (err) {
await sleep(100);
logger.log("Failed to connect to aria2, retrying...");
retries++;
}
}
}
private static getETA(

View file

@ -1,8 +1,7 @@
import { sleep } from "@main/helpers";
import { DownloadManager } from "./download-manager";
import { watchProcesses } from "./process-watcher";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export const startMainLoop = async () => {
// eslint-disable-next-line no-constant-condition
while (true) {