Skip to content

Commit

Permalink
Ensure index usage is in sync and try to improve inventory updates
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Jeandet <[email protected]>
  • Loading branch information
jeandet committed Dec 5, 2023
1 parent a2f9d05 commit 97c7233
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
5 changes: 2 additions & 3 deletions speasy_proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
from fastapi.staticfiles import StaticFiles
import os
from datetime import datetime, UTC
from .index import index
from .index import up_since
from .api.v1 import api_router as v1_api_router
from .frontend import frontend_router
from apscheduler.schedulers.background import BackgroundScheduler
import speasy as spz
import logging
from .backend.inventory_updater import ensure_update_inventory

Expand All @@ -37,7 +36,7 @@ def get_application() -> FastAPI:
_app.include_router(v1_api_router)
_app.mount("/static", StaticFiles(directory=f"{os.path.dirname(os.path.abspath(__file__))}/static"), name="static")

index["up_since"] = datetime.now(UTC)
up_since.set(datetime.now(UTC))

_app.add_middleware(
CORSMiddleware,
Expand Down
10 changes: 5 additions & 5 deletions speasy_proxy/backend/inventory_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
from speasy.core.cache import CacheCall
import logging
import threading

_last_update = datetime.now(UTC)
from ..index import IndexEntry

log = logging.getLogger(__name__)
lock = threading.Lock()

last_update = IndexEntry("last_update", datetime.now(UTC) - timedelta(days=1))


@CacheCall(cache_retention=timedelta(minutes=30))
def ensure_update_inventory():
global _last_update
if datetime.now(UTC) > (_last_update + timedelta(minutes=30)):
if datetime.now(UTC) > (last_update.value() + timedelta(minutes=30)):
with lock:
log.debug("Updating runtime inventory")
spz.update_inventories()
_last_update = datetime.now(UTC)
last_update.set(datetime.now(UTC))


class EnsureUpdatedInventory(object):
Expand Down
24 changes: 14 additions & 10 deletions speasy_proxy/frontend/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from .routes import router
from speasy.core import cache
from speasy.core.cache import _cache
from speasy import inventories
from humanize import filesize, time
from datetime import datetime, UTC
import logging
from speasy_proxy.index import index
from speasy_proxy.backend.inventory_updater import _last_update, ensure_update_inventory
from speasy_proxy.index import up_since
from speasy_proxy.backend.inventory_updater import ensure_update_inventory, last_update
import os
from threading import Thread
from urllib.parse import urljoin
Expand All @@ -32,20 +32,24 @@ def _refresh_inventory():
@router.get('/', response_class=HTMLResponse)
def home(request: Request, user_agent: Annotated[str | None, Header()] = None):
log.debug(f'Client asking for home page from {user_agent}')
up_since = index["up_since"]
up_time = datetime.now(UTC) - up_since
cache_stats = cache.stats()
_up_since = up_since.value()
up_time = datetime.now(UTC) - _up_since

with _cache.transact():
cache_stats = _cache.stats()
cache_len = len(_cache)
cache_disk = _cache.disk_size()
_refresh_inventory()
return templates.TemplateResponse("welcome.html",
{"request": request,
'entries': cache.cache_len(),
'entries': cache_len,
'cache_disk_size': filesize.naturalsize(
cache.cache_disk_size()),
'up_date': time.naturaldate(up_since),
cache_disk),
'up_date': time.naturaldate(_up_since),
'up_duration': time.naturaldelta(up_time),
'cache_hits': str(cache_stats['hit']),
'cache_misses': str(cache_stats['misses']),
'inventory_update': str(_last_update.isoformat()),
'inventory_update': str(last_update.value().isoformat()),
'inventory_size': str(
sum(map(lambda p: len(p.parameters),
set(inventories.flat_inventories.__dict__.values())))),
Expand Down
20 changes: 19 additions & 1 deletion speasy_proxy/index/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
from ..config import index as index_cfg
from diskcache import Index

index = Index(index_cfg.path())
_index = Index(index_cfg.path())


class IndexEntry:
def __init__(self, key: str, default=None):
self._key = key
with _index.transact():
_index[self._key] = default

def value(self):
with _index.transact():
return _index[self._key]

def set(self, value):
with _index.transact():
_index[self._key] = value


up_since = IndexEntry("up_since", None)

0 comments on commit 97c7233

Please sign in to comment.