mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: using retry system to connect to aria2
This commit is contained in:
parent
85516c1744
commit
ffb3d79954
34 changed files with 243 additions and 317 deletions
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
20
src/main/services/aria2c.ts
Normal file
20
src/main/services/aria2c.ts
Normal 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" }
|
||||
);
|
||||
};
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue