Cleanup error handling

This commit is contained in:
Zoe Roux 2023-03-19 18:06:38 +09:00
parent 6b9842c9d3
commit d4d223dbf0
3 changed files with 40 additions and 29 deletions

View File

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
class Status(Enum): class Status(str, Enum):
UNKNOWN = "unknown" UNKNOWN = "unknown"
FINISHED = "finished" FINISHED = "finished"
AIRING = "airing" AIRING = "airing"

View File

@ -1,3 +1,4 @@
from functools import wraps
import os import os
import asyncio import asyncio
import logging import logging
@ -6,6 +7,15 @@ from pathlib import Path
from guessit import guessit from guessit import guessit
from providers.provider import Provider from providers.provider import Provider
def log_errors(f):
@wraps(f)
async def internal(*args, **kwargs):
try:
await f(*args, **kwargs)
except Exception as e:
logging.exception("Unhandled error", exc_info=e)
return internal
class Scanner: class Scanner:
def __init__(self, client: ClientSession, languages: list[str]) -> None: def __init__(self, client: ClientSession, languages: list[str]) -> None:
@ -15,8 +25,9 @@ class Scanner:
async def scan(self, path: str): async def scan(self, path: str):
videos = filter(lambda p: p.is_file(), Path(path).rglob("*")) videos = filter(lambda p: p.is_file(), Path(path).rglob("*"))
await asyncio.gather(*map(self.identify, videos), return_exceptions=True) await asyncio.gather(*map(self.identify, videos))
@log_errors
async def identify(self, path: Path): async def identify(self, path: Path):
raw = guessit(path) raw = guessit(path)
logging.info("Identied %s: %s", path, raw) logging.info("Identied %s: %s", path, raw)
@ -36,7 +47,8 @@ class Scanner:
logging.warn("Unknown video file type: %s", raw["type"]) logging.warn("Unknown video file type: %s", raw["type"])
async def post(self, path: str, *, data: object): async def post(self, path: str, *, data: object):
url = os.environ.get('KYOO_URL', "http://back:5000")
async with self._client.post( async with self._client.post(
f"{os.environ['KYOO_URL']}/{path}", json=data f"{url}/{path}", json=data
) as r: ) as r:
r.raise_for_status() r.raise_for_status()

View File

@ -1,28 +1,27 @@
{pkgs ? import <nixpkgs> {}}: {pkgs ? import <nixpkgs> {}}: let
pkgs.mkShell {
packages = with pkgs; [
nodejs-16_x
nodePackages.yarn
(with dotnetCorePackages;
combinePackages [
sdk_6_0
aspnetcore_6_0
])
python3
python3Packages.venvShellHook
];
# Run this command, only after creating the virtual environment
venvDir = "./.venv"; venvDir = "./.venv";
postVenvCreation = '' pythonPkgs = ./scanner/requirements.txt;
unset SOURCE_DATE_EPOCH in
pip install -r ./scanner/requirements.txt pkgs.mkShell {
''; packages = with pkgs; [
nodejs-16_x
nodePackages.yarn
(with dotnetCorePackages;
combinePackages [
sdk_6_0
aspnetcore_6_0
])
python3
];
# Now we can execute any commands within the virtual environment. shellHook = ''
# This is optional and can be left out to run pip manually. # Install python modules
postShellHook = '' SOURCE_DATE_EPOCH=$(date +%s)
# allow pip to install wheels if [ ! -d "${venvDir}" ]; then
unset SOURCE_DATE_EPOCH ${pkgs.python3} -m venv "${venvDir}"
''; fi
} source "${venvDir}/bin/activate"
export PIP_DISABLE_PIP_VERSION_CHECK=1
pip install -r ${pythonPkgs}
'';
}