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