feat: apply suggestions

This commit is contained in:
Zamitto 2024-07-03 16:10:23 -03:00
parent ae3daa4c79
commit 153291f89f
10 changed files with 32 additions and 30 deletions

View File

@ -1,6 +1,6 @@
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import * as Sentry from "@sentry/electron/main"; import * as Sentry from "@sentry/electron/main";
import { HydraApi, RPCManager, gamesPlaytime } from "@main/services"; import { HydraApi, PythonInstance, gamesPlaytime } from "@main/services";
import { dataSource } from "@main/data-source"; import { dataSource } from "@main/data-source";
import { DownloadQueue, Game, UserAuth } from "@main/entity"; import { DownloadQueue, Game, UserAuth } from "@main/entity";
@ -24,7 +24,7 @@ const signOut = async (_event: Electron.IpcMainInvokeEvent) => {
Sentry.setUser(null); Sentry.setUser(null);
/* Disconnects libtorrent */ /* Disconnects libtorrent */
RPCManager.killTorrent(); PythonInstance.killTorrent();
await Promise.all([ await Promise.all([
databaseOperations, databaseOperations,

View File

@ -1,6 +1,6 @@
import { gameRepository } from "@main/repository"; import { gameRepository } from "@main/repository";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import { RPCManager, logger } from "@main/services"; import { PythonInstance, logger } from "@main/services";
import sudo from "sudo-prompt"; import sudo from "sudo-prompt";
import { app } from "electron"; import { app } from "electron";
@ -16,7 +16,7 @@ const closeGame = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
gameId: number gameId: number
) => { ) => {
const processes = await RPCManager.getProcessList(); const processes = await PythonInstance.getProcessList();
const game = await gameRepository.findOne({ const game = await gameRepository.findOne({
where: { id: gameId, isDeleted: false }, where: { id: gameId, isDeleted: false },
}); });

View File

@ -5,7 +5,7 @@ import i18n from "i18next";
import path from "node:path"; import path from "node:path";
import url from "node:url"; import url from "node:url";
import { electronApp, optimizer } from "@electron-toolkit/utils"; import { electronApp, optimizer } from "@electron-toolkit/utils";
import { logger, RPCManager, WindowManager } from "@main/services"; import { logger, PythonInstance, WindowManager } from "@main/services";
import { dataSource } from "@main/data-source"; import { dataSource } from "@main/data-source";
import * as resources from "@locales"; import * as resources from "@locales";
import { userPreferencesRepository } from "@main/repository"; import { userPreferencesRepository } from "@main/repository";
@ -116,7 +116,7 @@ app.on("window-all-closed", () => {
app.on("before-quit", () => { app.on("before-quit", () => {
/* Disconnects libtorrent */ /* Disconnects libtorrent */
RPCManager.kill(); PythonInstance.kill();
}); });
app.on("activate", () => { app.on("activate", () => {

View File

@ -1,7 +1,7 @@
import { import {
DownloadManager, DownloadManager,
RepacksManager, RepacksManager,
RPCManager, PythonInstance,
startMainLoop, startMainLoop,
} from "./services"; } from "./services";
import { import {
@ -41,7 +41,7 @@ const loadState = async (userPreferences: UserPreferences | null) => {
if (nextQueueItem?.game.status === "active") { if (nextQueueItem?.game.status === "active") {
DownloadManager.startDownload(nextQueueItem.game); DownloadManager.startDownload(nextQueueItem.game);
} else { } else {
RPCManager.spawn(); PythonInstance.spawn();
} }
startMainLoop(); startMainLoop();

View File

@ -1,6 +1,6 @@
import { Game } from "@main/entity"; import { Game } from "@main/entity";
import { Downloader } from "@shared"; import { Downloader } from "@shared";
import { RPCManager } from "./rpc-manager"; import { PythonInstance } from "./python-instance";
import { WindowManager } from "../window-manager"; import { WindowManager } from "../window-manager";
import { downloadQueueRepository, gameRepository } from "@main/repository"; import { downloadQueueRepository, gameRepository } from "@main/repository";
import { publishDownloadCompleteNotification } from "../notifications"; import { publishDownloadCompleteNotification } from "../notifications";
@ -16,7 +16,7 @@ export class DownloadManager {
if (this.currentDownloader === Downloader.RealDebrid) { if (this.currentDownloader === Downloader.RealDebrid) {
status = await RealDebridDownloader.getStatus(); status = await RealDebridDownloader.getStatus();
} else { } else {
status = await RPCManager.getStatus(); status = await PythonInstance.getStatus();
} }
if (status) { if (status) {
@ -65,7 +65,7 @@ export class DownloadManager {
if (this.currentDownloader === Downloader.RealDebrid) { if (this.currentDownloader === Downloader.RealDebrid) {
RealDebridDownloader.pauseDownload(); RealDebridDownloader.pauseDownload();
} else { } else {
await RPCManager.pauseDownload(); await PythonInstance.pauseDownload();
} }
WindowManager.mainWindow?.setProgressBar(-1); WindowManager.mainWindow?.setProgressBar(-1);
@ -77,7 +77,7 @@ export class DownloadManager {
RealDebridDownloader.startDownload(game); RealDebridDownloader.startDownload(game);
this.currentDownloader = Downloader.RealDebrid; this.currentDownloader = Downloader.RealDebrid;
} else { } else {
RPCManager.startDownload(game); PythonInstance.startDownload(game);
this.currentDownloader = Downloader.Torrent; this.currentDownloader = Downloader.Torrent;
} }
} }
@ -86,7 +86,7 @@ export class DownloadManager {
if (this.currentDownloader === Downloader.RealDebrid) { if (this.currentDownloader === Downloader.RealDebrid) {
RealDebridDownloader.cancelDownload(); RealDebridDownloader.cancelDownload();
} else { } else {
RPCManager.cancelDownload(gameId); PythonInstance.cancelDownload(gameId);
} }
WindowManager.mainWindow?.setProgressBar(-1); WindowManager.mainWindow?.setProgressBar(-1);
@ -98,7 +98,7 @@ export class DownloadManager {
RealDebridDownloader.startDownload(game); RealDebridDownloader.startDownload(game);
this.currentDownloader = Downloader.RealDebrid; this.currentDownloader = Downloader.RealDebrid;
} else { } else {
RPCManager.startDownload(game); PythonInstance.startDownload(game);
this.currentDownloader = Downloader.Torrent; this.currentDownloader = Downloader.Torrent;
} }
} }

View File

@ -1,2 +1,2 @@
export * from "./download-manager"; export * from "./download-manager";
export * from "./rpc-manager"; export * from "./python-instance";

View File

@ -20,8 +20,8 @@ import {
ProcessPayload, ProcessPayload,
} from "./types"; } from "./types";
export class RPCManager { export class PythonInstance {
private static rpcProcess: cp.ChildProcess | null = null; private static pythonProcess: cp.ChildProcess | null = null;
private static downloadingGameId = -1; private static downloadingGameId = -1;
private static rpc = axios.create({ private static rpc = axios.create({
@ -32,19 +32,19 @@ export class RPCManager {
}); });
public static spawn(args?: StartDownloadPayload) { public static spawn(args?: StartDownloadPayload) {
this.rpcProcess = startRPCClient(args); this.pythonProcess = startRPCClient(args);
} }
public static kill() { public static kill() {
if (this.rpcProcess) { if (this.pythonProcess) {
this.rpcProcess.kill(); this.pythonProcess.kill();
this.rpcProcess = null; this.pythonProcess = null;
this.downloadingGameId = -1; this.downloadingGameId = -1;
} }
} }
public static killTorrent() { public static killTorrent() {
if (this.rpcProcess) { if (this.pythonProcess) {
this.rpc.post("/action", { action: "kill-torrent" }); this.rpc.post("/action", { action: "kill-torrent" });
this.downloadingGameId = -1; this.downloadingGameId = -1;
} }
@ -131,7 +131,7 @@ export class RPCManager {
} }
static async startDownload(game: Game) { static async startDownload(game: Game) {
if (!this.rpcProcess) { if (!this.pythonProcess) {
this.spawn({ this.spawn({
game_id: game.id, game_id: game.id,
magnet: game.uri!, magnet: game.uri!,

View File

@ -3,7 +3,7 @@ import { gameRepository } from "@main/repository";
import { WindowManager } from "./window-manager"; import { WindowManager } from "./window-manager";
import { createGame, updateGamePlaytime } from "./library-sync"; import { createGame, updateGamePlaytime } from "./library-sync";
import { GameRunning } from "@types"; import { GameRunning } from "@types";
import { RPCManager } from "./download"; import { PythonInstance } from "./download";
export const gamesPlaytime = new Map< export const gamesPlaytime = new Map<
number, number,
@ -19,7 +19,7 @@ export const watchProcesses = async () => {
}); });
if (games.length === 0) return; if (games.length === 0) return;
const processes = await RPCManager.getProcessList(); const processes = await PythonInstance.getProcessList();
for (const game of games) { for (const game of games) {
const executablePath = game.executablePath!; const executablePath = game.executablePath!;

View File

@ -30,12 +30,13 @@ class Downloader:
self.torrent_handles[game_id] = None self.torrent_handles[game_id] = None
self.downloading_game_id = -1 self.downloading_game_id = -1
def cancel_all_downloads(self): def abort_session(self):
for game_id in self.torrent_handles: for game_id in self.torrent_handles:
torrent_handle = self.torrent_handles[game_id] torrent_handle = self.torrent_handles[game_id]
torrent_handle.pause() torrent_handle.pause()
self.session.remove_torrent(torrent_handle) self.session.remove_torrent(torrent_handle)
self.session.abort()
self.torrent_handles = {} self.torrent_handles = {}
self.downloading_game_id = -1 self.downloading_game_id = -1

View File

@ -8,11 +8,12 @@ from downloader import Downloader
torrent_port = sys.argv[1] torrent_port = sys.argv[1]
http_port = sys.argv[2] http_port = sys.argv[2]
rpc_password = sys.argv[3] rpc_password = sys.argv[3]
start_download_payload = sys.argv[4]
downloader = None downloader = None
if sys.argv[4]: if start_download_payload:
initial_download = json.loads(urllib.parse.unquote(sys.argv[4])) initial_download = json.loads(urllib.parse.unquote(start_download_payload))
downloader = Downloader(torrent_port) downloader = Downloader(torrent_port)
downloader.start_download(initial_download['game_id'], initial_download['magnet'], initial_download['save_path']) downloader.start_download(initial_download['game_id'], initial_download['magnet'], initial_download['save_path'])
@ -75,7 +76,7 @@ class Handler(BaseHTTPRequestHandler):
elif data['action'] == 'cancel': elif data['action'] == 'cancel':
downloader.cancel_download(data['game_id']) downloader.cancel_download(data['game_id'])
elif data['action'] == 'kill-torrent': elif data['action'] == 'kill-torrent':
downloader.cancel_all_downloads() downloader.abort_session()
downloader = None downloader = None
self.send_response(200) self.send_response(200)