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
class Status(Enum):
class Status(str, Enum):
UNKNOWN = "unknown"
FINISHED = "finished"
AIRING = "airing"

View File

@ -1,3 +1,4 @@
from functools import wraps
import os
import asyncio
import logging
@ -6,6 +7,15 @@ from pathlib import Path
from guessit import guessit
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:
def __init__(self, client: ClientSession, languages: list[str]) -> None:
@ -15,8 +25,9 @@ class Scanner:
async def scan(self, path: str):
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):
raw = guessit(path)
logging.info("Identied %s: %s", path, raw)
@ -36,7 +47,8 @@ class Scanner:
logging.warn("Unknown video file type: %s", raw["type"])
async def post(self, path: str, *, data: object):
url = os.environ.get('KYOO_URL', "http://back:5000")
async with self._client.post(
f"{os.environ['KYOO_URL']}/{path}", json=data
f"{url}/{path}", json=data
) as r:
r.raise_for_status()

View File

@ -1,28 +1,27 @@
{pkgs ? import <nixpkgs> {}}:
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
{pkgs ? import <nixpkgs> {}}: let
venvDir = "./.venv";
postVenvCreation = ''
unset SOURCE_DATE_EPOCH
pip install -r ./scanner/requirements.txt
'';
pythonPkgs = ./scanner/requirements.txt;
in
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.
# This is optional and can be left out to run pip manually.
postShellHook = ''
# allow pip to install wheels
unset SOURCE_DATE_EPOCH
'';
}
shellHook = ''
# Install python modules
SOURCE_DATE_EPOCH=$(date +%s)
if [ ! -d "${venvDir}" ]; then
${pkgs.python3} -m venv "${venvDir}"
fi
source "${venvDir}/bin/activate"
export PIP_DISABLE_PIP_VERSION_CHECK=1
pip install -r ${pythonPkgs}
'';
}