setup: Minor refactoring of force upgrader class instantiation

- It is okay to instantiate before shutdown to mark shutdown initiation.

- Still keep the instantiating lazy.

Tests:

- Tested as part of patch series.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2024-02-14 15:23:38 -08:00 committed by James Valleroy
parent 29d48e86b7
commit 56b58174b3
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -26,8 +26,6 @@ _is_first_setup = False
is_first_setup_running = False is_first_setup_running = False
_is_shutting_down = False _is_shutting_down = False
_force_upgrader = None
def run_setup_on_app(app_id, allow_install=True, rerun=False): def run_setup_on_app(app_id, allow_install=True, rerun=False):
"""Execute the setup process in a thread.""" """Execute the setup process in a thread."""
@ -54,18 +52,16 @@ def run_setup_on_app(app_id, allow_install=True, rerun=False):
def _run_setup_on_app(app, current_version): def _run_setup_on_app(app, current_version):
"""Execute the setup process.""" """Execute the setup process."""
global _force_upgrader
logger.info('Setup run: %s', app.app_id) logger.info('Setup run: %s', app.app_id)
exception_to_update = None exception_to_update = None
message = None message = None
try: try:
if not _force_upgrader: force_upgrader = ForceUpgrader.get_instance()
_force_upgrader = ForceUpgrader()
# Check if this app needs force_upgrade. If it is needed, but not yet # Check if this app needs force_upgrade. If it is needed, but not yet
# supported for the new version of the package, then an exception will # supported for the new version of the package, then an exception will
# be raised, so that we do not run setup. # be raised, so that we do not run setup.
_force_upgrader.attempt_upgrade_for_app(app.app_id) force_upgrader.attempt_upgrade_for_app(app.app_id)
current_version = app.get_setup_version() current_version = app.get_setup_version()
app.setup(old_version=current_version) app.setup(old_version=current_version)
@ -152,8 +148,8 @@ def stop():
global _is_shutting_down global _is_shutting_down
_is_shutting_down = True _is_shutting_down = True
if _force_upgrader: force_upgrader = ForceUpgrader.get_instance()
_force_upgrader.shutdown() force_upgrader.shutdown()
def setup_apps(app_ids=None, essential=False, allow_install=True): def setup_apps(app_ids=None, essential=False, allow_install=True):
@ -238,8 +234,8 @@ def _get_apps_for_regular_setup():
1. essential apps that are not up-to-date 1. essential apps that are not up-to-date
2. non-essential app that are installed and need updates 2. non-essential app that are installed and need updates
""" """
if (app.info.is_essential and if (app.info.is_essential and app.get_setup_state()
app.get_setup_state() != app_module.App.SetupState.UP_TO_DATE): != app_module.App.SetupState.UP_TO_DATE):
return True return True
if app.get_setup_state() == app_module.App.SetupState.NEEDS_UPDATE: if app.get_setup_state() == app_module.App.SetupState.NEEDS_UPDATE:
@ -344,6 +340,7 @@ class ForceUpgrader():
""" """
_instance = None
_run_lock = threading.Lock() _run_lock = threading.Lock()
_wait_event = threading.Event() _wait_event = threading.Event()
@ -358,6 +355,14 @@ class ForceUpgrader():
"""Raised when upgrade fails and there is nothing more we wish to do. """Raised when upgrade fails and there is nothing more we wish to do.
""" """
@classmethod
def get_instance(cls):
"""Return a single instance of a the class."""
if not cls._instance:
cls._instance = ForceUpgrader()
return cls._instance
def __init__(self): def __init__(self):
"""Initialize the force upgrader.""" """Initialize the force upgrader."""
if plinth.cfg.develop: if plinth.cfg.develop:
@ -410,7 +415,7 @@ class ForceUpgrader():
def shutdown(self): def shutdown(self):
"""If we are sleeping for next attempt, cancel it. """If we are sleeping for next attempt, cancel it.
If we are actually upgrading packages, don nothing. If we are actually upgrading packages, do nothing.
""" """
self._wait_event.set() self._wait_event.set()
@ -576,8 +581,5 @@ class ForceUpgrader():
def on_package_cache_updated(): def on_package_cache_updated():
"""Called by D-Bus service when apt package cache is updated.""" """Called by D-Bus service when apt package cache is updated."""
global _force_upgrader force_upgrader = ForceUpgrader.get_instance()
if not _force_upgrader: force_upgrader.on_package_cache_updated()
_force_upgrader = ForceUpgrader()
_force_upgrader.on_package_cache_updated()