Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/loader/checker.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | |||||
import logging | import logging | ||||
from typing import Mapping | from typing import Any, Dict | ||||
from swh.core.config import SWHConfig | from swh.core import config | ||||
from swh.deposit.client import PrivateApiDepositClient | from swh.deposit.client import PrivateApiDepositClient | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
class DepositChecker(SWHConfig): | class DepositChecker: | ||||
"""Deposit checker implementation. | """Deposit checker implementation. | ||||
Trigger deposit's checks through the private api. | Trigger deposit's checks through the private api. | ||||
""" | """ | ||||
CONFIG_BASE_FILENAME = "deposit/checker" | def __init__(self): | ||||
config_file = os.environ["SWH_CONFIG_FILENAME"] | |||||
DEFAULT_CONFIG = { | self.config: Dict[str, Any] = config.read_raw_config( | ||||
"deposit": ("dict", {"url": "http://localhost:5006/1/private/", "auth": {},}) | config.config_basepath(config_file) | ||||
} | ) | ||||
def __init__(self, config=None): | |||||
super().__init__() | |||||
if config is None: | |||||
self.config = self.parse_config_file() | |||||
else: | |||||
self.config = config | |||||
self.client = PrivateApiDepositClient(config=self.config["deposit"]) | self.client = PrivateApiDepositClient(config=self.config["deposit"]) | ||||
def check(self, collection: str, deposit_id: str) -> Mapping[str, str]: | def check(self, collection: str, deposit_id: str) -> Dict[str, str]: | ||||
status = None | status = None | ||||
deposit_check_url = f"/{collection}/{deposit_id}/check/" | deposit_check_url = f"/{collection}/{deposit_id}/check/" | ||||
logger.debug("deposit-check-url: %s", deposit_check_url) | logger.debug("deposit-check-url: %s", deposit_check_url) | ||||
try: | try: | ||||
r = self.client.check(deposit_check_url) | r = self.client.check(deposit_check_url) | ||||
logger.debug("Check result: %s", r) | logger.debug("Check result: %s", r) | ||||
status = "eventful" if r == "verified" else "failed" | status = "eventful" if r == "verified" else "failed" | ||||
except Exception: | except Exception: | ||||
logger.exception("Failure during check on '%s'", deposit_check_url) | logger.exception("Failure during check on '%s'", deposit_check_url) | ||||
status = "failed" | status = "failed" | ||||
logger.debug("Check status: %s", status) | logger.debug("Check status: %s", status) | ||||
return {"status": status} | return {"status": status} |