Non-blocking checksums computation when server starts. Fixes #2228

This commit is contained in:
grossmj 2023-11-06 12:32:23 +10:00
parent fe246cd413
commit 27d5ac537f
No known key found for this signature in database
GPG Key ID: 0A2D76AC45EA25CD

View File

@ -29,6 +29,7 @@ import functools
import time import time
import atexit import atexit
import weakref import weakref
import concurrent.futures
# Import encoding now, to avoid implicit import later. # Import encoding now, to avoid implicit import later.
# Implicit import within threads may cause LookupError when standard library is in a ZIP # Implicit import within threads may cause LookupError when standard library is in a ZIP
@ -38,7 +39,7 @@ from .route import Route
from ..config import Config from ..config import Config
from ..compute import MODULES from ..compute import MODULES
from ..compute.port_manager import PortManager from ..compute.port_manager import PortManager
from ..compute.qemu import Qemu from ..utils.images import list_images
from ..controller import Controller from ..controller import Controller
# do not delete this import # do not delete this import
@ -87,6 +88,8 @@ class WebServer:
except (RuntimeError, OSError, asyncio.CancelledError) as e: except (RuntimeError, OSError, asyncio.CancelledError) as e:
log.critical("Could not start the server: {}".format(e)) log.critical("Could not start the server: {}".format(e))
return False return False
except KeyboardInterrupt:
return False
return True return True
async def reload_server(self): async def reload_server(self):
@ -230,16 +233,27 @@ class WebServer:
atexit.register(close_asyncio_loop) atexit.register(close_asyncio_loop)
async def _compute_image_checksums(self):
"""
Compute image checksums.
"""
loop = asyncio.get_event_loop()
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as pool:
log.info("Computing image checksums...")
await loop.run_in_executor(pool, list_images, "qemu")
log.info("Finished computing image checksums")
async def _on_startup(self, *args): async def _on_startup(self, *args):
""" """
Called when the HTTP server start Called when the HTTP server start
""" """
await Controller.instance().start() await Controller.instance().start()
# Because with a large image collection
# without md5sum already computed we start the # Start computing checksums now because it can take a long time
# computing with server start # for a large image collection
asyncio.ensure_future(Qemu.instance().list_images()) await self._compute_image_checksums()
def run(self): def run(self):
""" """