forked from Support/sp-hydra-veil-gui
Profile loading is now background threaded. If all profiles fail to load, it does a database recovery (migration/wipe)
This commit is contained in:
parent
a6c31375b0
commit
72f9f0b97d
7 changed files with 72 additions and 22 deletions
|
|
@ -21,16 +21,6 @@ def post_state_evaluation(result: Result):
|
||||||
poll_state.start_polling()
|
poll_state.start_polling()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# if not result.valid and result.error_type == ResultError.ZOMBIE_SESSION and result.data:
|
|
||||||
# active_session_profiles = result.data
|
|
||||||
# logger.info("Removing Zombie Session profiles on the main thread.")
|
|
||||||
# for each_profile in active_session_profiles:
|
|
||||||
# logger.info(f"Removing this profile {active_session_profiles} from session since it's a fresh restart.")
|
|
||||||
# SessionStateController.remove(each_profile.id)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@register_action(ResultError.ZOMBIE_SYSTEM)
|
@register_action(ResultError.ZOMBIE_SYSTEM)
|
||||||
def evaluate_bad_state():
|
def evaluate_bad_state():
|
||||||
"""Start core's evaluation of state with a GUI observer."""
|
"""Start core's evaluation of state with a GUI observer."""
|
||||||
|
|
@ -64,3 +54,5 @@ def navigate_to_singbox_setup() -> Result:
|
||||||
connection_observer=connection_observer
|
connection_observer=connection_observer
|
||||||
)
|
)
|
||||||
return Result(valid=False, error_type=ResultError.NAVIGATE_TO, navigate_to="networking_setup")
|
return Result(valid=False, error_type=ResultError.NAVIGATE_TO, navigate_to="networking_setup")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
# core
|
# core
|
||||||
|
from core.models.manage.database_recovery import wipe_and_kill_entire_app
|
||||||
from core.infrastructure.state_management.global_state_controller import connected_system_profile_id, connected_session_profile_ids, is_connected
|
from core.infrastructure.state_management.global_state_controller import connected_system_profile_id, connected_session_profile_ids, is_connected
|
||||||
from core.observers.Notifications import Notifications, EventType
|
from core.observers.Notifications import Notifications, EventType
|
||||||
from core.models.enum_models import ProtocolChoice
|
from core.models.enum_models import ProtocolChoice
|
||||||
|
|
@ -37,9 +38,11 @@ from gui.v2.infrastructure.setup_observers import (
|
||||||
from gui.v2.actions.error_handlers import navigate_to_new_sub_page
|
from gui.v2.actions.error_handlers import navigate_to_new_sub_page
|
||||||
|
|
||||||
# generic
|
# generic
|
||||||
|
from typing import Optional
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import atexit
|
import atexit
|
||||||
|
import sys
|
||||||
|
|
||||||
@type_checker
|
@type_checker
|
||||||
def get_profile(profile_id: int):
|
def get_profile(profile_id: int):
|
||||||
|
|
@ -314,7 +317,7 @@ def disconnect_many_profiles(profile_ids: list) -> Result:
|
||||||
return Result(valid=False, data=failed_list)
|
return Result(valid=False, data=failed_list)
|
||||||
|
|
||||||
|
|
||||||
def disconnect_active(ui_caller: Callable) -> Result:
|
def disconnect_active(ui_caller: Optional[Callable] = None) -> Result:
|
||||||
"""Disconnects active session profiles first, then system."""
|
"""Disconnects active session profiles first, then system."""
|
||||||
|
|
||||||
# SESSION
|
# SESSION
|
||||||
|
|
@ -342,8 +345,10 @@ def disconnect_active(ui_caller: Callable) -> Result:
|
||||||
system_disconnected.message = error_msg
|
system_disconnected.message = error_msg
|
||||||
return system_disconnected
|
return system_disconnected
|
||||||
|
|
||||||
ui_caller(Result(valid=True))
|
if ui_caller:
|
||||||
|
ui_caller(Result(valid=True))
|
||||||
|
else:
|
||||||
|
return Result(valid=True)
|
||||||
|
|
||||||
def sync_now(ui_caller: Callable):
|
def sync_now(ui_caller: Callable):
|
||||||
take_action(
|
take_action(
|
||||||
|
|
@ -398,4 +403,18 @@ def edit_profile(
|
||||||
new_value=new_value
|
new_value=new_value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@register_action(ResultError.DELETE_DB)
|
||||||
|
def shut_down_and_wipe() -> Result:
|
||||||
|
"""Attempt a graceful close of profiles, then wipe the database regardless."""
|
||||||
|
logger.info("Shutting down profiles, and then wiping.")
|
||||||
|
try:
|
||||||
|
disconnect_active(ui_caller=None)
|
||||||
|
return wipe_and_kill_entire_app()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Wiping anyway despite {str(e)}")
|
||||||
|
return wipe_and_kill_entire_app()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# atexit.register(disconnect_active(ui_caller=finish))
|
# atexit.register(disconnect_active(ui_caller=finish))
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,10 @@ def load_profile(profile_id: int, on_error: StatusCallback) -> BaseProfile | Non
|
||||||
def load_profiles(on_error: StatusCallback) -> dict[int, BaseProfile]:
|
def load_profiles(on_error: StatusCallback) -> dict[int, BaseProfile]:
|
||||||
"""Load all profiles here so list rendering is not responsible for controller failures."""
|
"""Load all profiles here so list rendering is not responsible for controller failures."""
|
||||||
try:
|
try:
|
||||||
return ProfileController.get_all()
|
profile_result = ProfileController.get_all()
|
||||||
|
if not profile_result.valid:
|
||||||
|
return profile_result
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
_report_load_error(
|
_report_load_error(
|
||||||
error,
|
error,
|
||||||
|
|
@ -175,11 +178,16 @@ def _enabled_profile_kind(profile: BaseProfile, enabled: bool | None = None) ->
|
||||||
|
|
||||||
|
|
||||||
def load_menu_profiles(
|
def load_menu_profiles(
|
||||||
gui_config_file: str | None, on_error: StatusCallback,
|
profiles: dict,
|
||||||
selected_id: int | None = None, state_change: Mapping[str, Any] | None = None,
|
gui_config_file: str | None,
|
||||||
|
on_error: StatusCallback,
|
||||||
|
selected_id: int | None = None,
|
||||||
|
state_change: Mapping[str, Any] | None = None,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Provide one menu-loading action so the page does not separately fetch, order, and classify profiles."""
|
"""Provide one menu-loading action so the page does not separately fetch, order, and classify profiles."""
|
||||||
profiles = load_profiles(on_error)
|
|
||||||
|
# profiles = load_profiles(on_error)
|
||||||
|
|
||||||
state_change = state_change or {}
|
state_change = state_change or {}
|
||||||
changed_id = state_change.get("profile_id")
|
changed_id = state_change.get("profile_id")
|
||||||
enabled_kinds = {
|
enabled_kinds = {
|
||||||
|
|
|
||||||
|
|
@ -24,3 +24,9 @@ def edit_profile(profile_id: int, potential_data: dict, ui_caller: Callable):
|
||||||
profile_id=profile_id,
|
profile_id=profile_id,
|
||||||
new_choices=potential_data
|
new_choices=potential_data
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_all_profiles(ui_caller: Callable):
|
||||||
|
take_action(
|
||||||
|
action=ProfileController.get_all,
|
||||||
|
ui_caller=ui_caller
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,10 @@ def read_bwrap_enabled():
|
||||||
|
|
||||||
def prepare(gui_config_file):
|
def prepare(gui_config_file):
|
||||||
try:
|
try:
|
||||||
profiles = ProfileController.get_all()
|
profiles_result = ProfileController.get_all()
|
||||||
|
if not profiles_result.valid:
|
||||||
|
print(f"Fetch of all profiles failed: {profiles_result.message}")
|
||||||
|
profiles = profiles_result.data
|
||||||
except Exception:
|
except Exception:
|
||||||
profiles = {}
|
profiles = {}
|
||||||
profile_order = normalize_profile_order(gui_config_file, profiles.keys())
|
profile_order = normalize_profile_order(gui_config_file, profiles.keys())
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ from core.infrastructure.global_state_coordinator import evaluate_state_on_start
|
||||||
from core.services.networking.general_connection_tools.disconnect import kill_profile_from_grave
|
from core.services.networking.general_connection_tools.disconnect import kill_profile_from_grave
|
||||||
from gui.v2.infrastructure.take_plumbing_action import take_action
|
from gui.v2.infrastructure.take_plumbing_action import take_action
|
||||||
from gui.v2.actions.error_handlers import post_state_evaluation
|
from gui.v2.actions.error_handlers import post_state_evaluation
|
||||||
|
from gui.v2.actions.profile_actions import get_all_profiles
|
||||||
|
|
||||||
from core.controllers.ProfileController import ProfileController
|
from core.controllers.ProfileController import ProfileController
|
||||||
from core.models.BaseProfile import ProfileType
|
from core.models.BaseProfile import ProfileType
|
||||||
|
|
@ -175,6 +176,12 @@ class MenuPage(Page):
|
||||||
connected = is_connected(profile_id)
|
connected = is_connected(profile_id)
|
||||||
p_type = ProfileController.what_type_is_profile(profile_id)
|
p_type = ProfileController.what_type_is_profile(profile_id)
|
||||||
|
|
||||||
|
if p_type is None:
|
||||||
|
error_msg = "Can't create/modify buttons with a blank profile type"
|
||||||
|
print(error_msg)
|
||||||
|
self.update_status.update_status(error_msg)
|
||||||
|
return
|
||||||
|
|
||||||
# if it's connected, then it can't be edited.
|
# if it's connected, then it can't be edited.
|
||||||
self.boton_edit.setEnabled(not connected)
|
self.boton_edit.setEnabled(not connected)
|
||||||
|
|
||||||
|
|
@ -228,11 +235,23 @@ class MenuPage(Page):
|
||||||
@mainthread
|
@mainthread
|
||||||
def refresh_menu_buttons(self, state_change=None):
|
def refresh_menu_buttons(self, state_change=None):
|
||||||
|
|
||||||
print("refresh_menu_buttons triggered")
|
print("refresh_menu_buttons triggered. First, getting all profiles..")
|
||||||
|
get_all_profiles(ui_caller=self.actually_load_profiles)
|
||||||
|
|
||||||
|
def actually_load_profiles(self, result: Result):
|
||||||
|
if not result.valid:
|
||||||
|
print("Result is invalid, but core handled the error on it's own.")
|
||||||
|
return
|
||||||
|
|
||||||
|
profiles = result.data
|
||||||
|
print("Got the data. Loading all menu profiles.")
|
||||||
|
|
||||||
menu_data = load_menu_profiles(
|
menu_data = load_menu_profiles(
|
||||||
getattr(self.update_status, 'gui_config_file', None),
|
profiles=profiles,
|
||||||
self.update_status.update_status, self.reverse_id, state_change,
|
gui_config_file=getattr(self.update_status, 'gui_config_file', None),
|
||||||
|
on_error=self.update_status.update_status,
|
||||||
|
selected_id=self.reverse_id,
|
||||||
|
state_change=None, # legacy variable to be refactored out
|
||||||
)
|
)
|
||||||
self.profiles_data = menu_data["display_profiles"]
|
self.profiles_data = menu_data["display_profiles"]
|
||||||
self.profile_info = self.profiles_data
|
self.profile_info = self.profiles_data
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,10 @@ class ErrorPopup(QDialog):
|
||||||
ok_btn = QPushButton("✓ OK")
|
ok_btn = QPushButton("✓ OK")
|
||||||
ok_btn.setStyleSheet(CORPORATE_PUNK_DYING_ROT)
|
ok_btn.setStyleSheet(CORPORATE_PUNK_DYING_ROT)
|
||||||
|
|
||||||
ok_btn.clicked.connect(self.accept) # Closes the dialog
|
if self.end_app:
|
||||||
|
ok_btn.clicked.connect(self._terminate_app)
|
||||||
|
else:
|
||||||
|
ok_btn.clicked.connect(self.accept)
|
||||||
main_layout.addWidget(ok_btn)
|
main_layout.addWidget(ok_btn)
|
||||||
|
|
||||||
self.setLayout(main_layout)
|
self.setLayout(main_layout)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue