Skip to content

Commit

Permalink
otaproxy: not register atexit handler by us, instead use uvicorn's ex…
Browse files Browse the repository at this point in the history
…it handler
  • Loading branch information
Bodong-Yang committed Nov 26, 2024
1 parent af5cdee commit 83d8207
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
17 changes: 1 addition & 16 deletions src/ota_proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

from __future__ import annotations

import atexit
import logging
from functools import partial

from ota_proxy.external_cache import mount_external_cache, umount_external_cache

from .cache_control_header import OTAFileCacheControl
from .config import config
Expand Down Expand Up @@ -53,25 +49,14 @@ async def run_otaproxy(

from . import App, OTACache

_loaded_mnt_point = None
if external_cache_mnt_point and (
_loaded_mnt_point := mount_external_cache(external_cache_mnt_point)
):
_loaded_mnt_point = str(_loaded_mnt_point)

def _atexit(_mnt_point) -> None:
umount_external_cache(_mnt_point)

atexit.register(partial(_atexit, _loaded_mnt_point))

_ota_cache = OTACache(
base_dir=cache_dir,
db_file=cache_db_f,
cache_enabled=enable_cache,
upper_proxy=upper_proxy,
enable_https=enable_https,
init_cache=init_cache,
external_cache_mnt_point=_loaded_mnt_point,
external_cache_mnt_point=external_cache_mnt_point,
)
_config = uvicorn.Config(
App(_ota_cache),
Expand Down
12 changes: 11 additions & 1 deletion src/ota_proxy/ota_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .config import config as cfg
from .db import CacheMeta, check_db, init_db
from .errors import BaseOTACacheError
from .external_cache import mount_external_cache, umount_external_cache
from .lru_cache_helper import LRUCacheHelper
from .utils import read_file, url_based_hash

Expand Down Expand Up @@ -137,10 +138,16 @@ def __init__(
)

self._external_cache_data_dir = None
if external_cache_mnt_point and cache_enabled:
self._external_cache_mp = None
if (
cache_enabled
and external_cache_mnt_point
and mount_external_cache(external_cache_mnt_point)
):
logger.info(
f"external cache source is enabled at: {external_cache_mnt_point}"
)
self._external_cache_mp = external_cache_mnt_point
self._external_cache_data_dir = (
Path(external_cache_mnt_point) / cfg.EXTERNAL_CACHE_DATA_DNAME
)
Expand Down Expand Up @@ -224,6 +231,9 @@ async def close(self):
if self._cache_enabled:
self._lru_helper.close()

if self._external_cache_mp:
umount_external_cache(self._external_cache_mp)

logger.info("shutdown ota-cache completed")

def _background_check_free_space(self):
Expand Down

0 comments on commit 83d8207

Please sign in to comment.