Save scanner issues on the db

This commit is contained in:
Zoe Roux 2024-02-17 19:31:23 +01:00
parent 9f003189e9
commit a278e3a565
2 changed files with 41 additions and 6 deletions

View File

@ -13,7 +13,7 @@ from providers.types.show import Show
from providers.types.episode import Episode, PartialShow from providers.types.episode import Episode, PartialShow
from providers.types.season import Season from providers.types.season import Season
from .parser.guess import guessit from .parser.guess import guessit
from .utils import batch, log_errors from .utils import batch, handle_errors
from .cache import cache, exec_as_cache, make_key from .cache import cache, exec_as_cache, make_key
@ -41,6 +41,7 @@ class Scanner:
async def scan(self, path: str): async def scan(self, path: str):
logging.info("Starting the scan. It can take some times...") logging.info("Starting the scan. It can take some times...")
self.registered = await self.get_registered_paths() self.registered = await self.get_registered_paths()
self.issues = await self.get_issues()
videos = [str(p) for p in Path(path).rglob("*") if p.is_file()] videos = [str(p) for p in Path(path).rglob("*") if p.is_file()]
deleted = [x for x in self.registered if x not in videos] deleted = [x for x in self.registered if x not in videos]
@ -75,7 +76,17 @@ class Scanner:
paths += list(x["path"] for x in ret["items"]) paths += list(x["path"] for x in ret["items"])
return paths return paths
@log_errors async def get_issues(self) -> List[str]:
async with self._client.get(
f"{self._url}/issues",
params={"limit": 0},
headers={"X-API-Key": self._api_key},
) as r:
r.raise_for_status()
ret = await r.json()
return [x["cause"] for x in ret if x["domain"] == "scanner"]
@handle_errors
async def identify(self, path: str): async def identify(self, path: str):
if path in self.registered or self._ignore_pattern.match(path): if path in self.registered or self._ignore_pattern.match(path):
return return

View File

@ -1,9 +1,13 @@
from __future__ import annotations
import logging import logging
from functools import wraps from functools import wraps
from itertools import islice from itertools import islice
from typing import Iterator, List, TypeVar from typing import TYPE_CHECKING, Iterator, List, TypeVar
from providers.utils import ProviderError from providers.utils import ProviderError
if TYPE_CHECKING:
from scanner.scanner import Scanner
T = TypeVar("T") T = TypeVar("T")
@ -19,14 +23,34 @@ def batch(iterable: Iterator[T], n: int) -> Iterator[List[T]]:
yield batch yield batch
def log_errors(f): def handle_errors(f):
@wraps(f) @wraps(f)
async def internal(*args, **kwargs): async def internal(self: Scanner, path: str):
try: try:
await f(*args, **kwargs) await f(self, path)
if path in self.issues:
await self._client.delete(
f'{self._url}/issues?filter=domain eq scanner and cause eq "{path}"',
headers={"X-API-Key": self._api_key},
)
except ProviderError as e: except ProviderError as e:
logging.error(str(e)) logging.error(str(e))
await self._client.post(
f"{self._url}/issues",
json={"domain": "scanner", "cause": path, "reason": str(e)},
headers={"X-API-Key": self._api_key},
)
except Exception as e: except Exception as e:
logging.exception("Unhandled error", exc_info=e) logging.exception("Unhandled error", exc_info=e)
await self._client.post(
f"{self._url}/issues",
json={
"domain": "scanner",
"cause": path,
"reason": "Unknown error",
"extra": {"type": type(e).__name__, "message": str(e)},
},
headers={"X-API-Key": self._api_key},
)
return internal return internal