refactor: consolidate aria2 process management into state and remove global variables

This commit is contained in:
Vxrtrauter
2025-10-09 15:55:39 +02:00
parent 13d0384a64
commit 5da7989157
5 changed files with 30 additions and 41 deletions
+2 -9
View File
@@ -37,13 +37,6 @@ def state_debug(setting):
state.debug = False
def pass_aria(aria):
global aria2process
aria2process = aria
def create_tab(title, searchbar, software_list, tabs):
tab = QWidget()
layout = QVBoxLayout()
@@ -162,7 +155,7 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
thread_box = QSpinBox()
thread_box.setMinimum(1)
thread_box.setMaximum(16)
thread_box.setValue(4)
thread_box.setValue(state.aria2_threads)
# container for tight space
@@ -202,7 +195,7 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
save_btn = QPushButton("Save")
cancel_btn = QPushButton("Cancel")
save_btn.clicked.connect(lambda: save_settings(thread_box.value(), close_settings, text_edit.toPlainText(), aria2process))
save_btn.clicked.connect(lambda: save_settings(thread_box.value(), close_settings, text_edit.toPlainText()))
cancel_btn.clicked.connect(dialog.reject)
+3 -6
View File
@@ -1,6 +1,7 @@
import aria2p
import os
import subprocess
from core.utils.state import state
def run_aria2p():
global aria2
@@ -15,11 +16,7 @@ def run_aria2p():
return aria2
t = 4 # default value
def set_threads(threads):
global t
t = threads
def aria2server():
@@ -32,8 +29,8 @@ def aria2server():
"--rpc-listen-all",
"--rpc-listen-port=6800",
f"--dir={downloads_dir}",
"-x", str(t),
"-s", str(t),
"-x", str(state.aria2_threads),
"-s", str(state.aria2_threads),
]
+15 -16
View File
@@ -1,20 +1,19 @@
from core.network.aria2_integration import set_threads
from core.utils.state import state
def restart_aria2c(aria2process):
import main # had to do this because of circle import :(
import signal
import atexit
main.kill_aria2server(aria2process)
aria2process.wait()
aria2process = main.run_aria2server()
signal.signal(signal.SIGINT, main.keyboardinterrupthandler)
atexit.unregister(main.kill_aria2server)
atexit.register(main.kill_aria2server, aria2process)
def restart_aria2c():
import main # had to do this because of circle import :(
import signal
import atexit
main.kill_aria2server()
state.aria2process.wait()
state.aria2process = main.run_aria2server()
signal.signal(signal.SIGINT, main.keyboardinterrupthandler)
atexit.unregister(main.kill_aria2server)
atexit.register(main.kill_aria2server)
def save_settings(thread_count, close, apiurl, aria2process):
global api_url
set_threads(thread_count)
api_url = apiurl
restart_aria2c(aria2process)
def save_settings(thread_count, close, apiurl):
state.aria2_threads = thread_count
state.api_url = apiurl
restart_aria2c()
close()
+3
View File
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from typing import Optional, Any
@dataclass
@@ -6,6 +7,8 @@ class AppState:
debug: bool = False
tracker: str = "rutracker"
api_url: str = "https://api.michijackson.xyz"
aria2process: Optional[Any] = None
aria2_threads: int = 4
state = AppState()