Skip to content

Commit

Permalink
32 recommender restart when exception (#41)
Browse files Browse the repository at this point in the history
* catch exception during `process_text` in AHDClassifier when the AHD is not accessible "requests.exceptions.RequestException: 404 Server Error: 'Not Found' for url: ..." and terminate with "sys.exit" to be able to restart docker container

* increased VERSION

* implemented usage of env variable "DOCKER_MODE" that can be used in the classifiers for varying effects/needs
  • Loading branch information
fmatthies authored Dec 11, 2024
1 parent 0855563 commit 72f873f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions inception_ahd_recommender/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ Nutzt den Standard-Ordner, wenn nicht gesetzt.
Diese beiden Werte werden an den WSGI Server (`gunicorn`) übergeben und bestimmen mit `WORKERS` wieviele Benutzer*innen gleichzeitig den Recommender verwenden können
und mit `ADDRESS` unter welcher Adresse der Recommender von [INCEpTION](https://inception-project.github.io) erreicht werden kann.

###### DOCKER_MODE
_(default: True)_ Ob das Programm als Docker-Container läuft.
Wird z.B. (im `AHDClassifier`) dazu genutzt, um zu bestimmen, ob eine `RequestException` bzgl. der Erreichbarkeit der `AHD` abgefangen
und das Programm geschlossen werden soll. Das ermöglicht dann das automatische Neustarten des Docker-Containers.

### Andere Einstellungen in der docker-compose.yml
##### ports
```
Expand Down
2 changes: 1 addition & 1 deletion inception_ahd_recommender/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
1.2.2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import requests
from cassis import Cas
from averbis import Client as AHDClient
from requests import RequestException

from ariadne.classifier import Classifier as AriadneClassifier
from ariadne.contrib.external_server_consumer import (
Expand All @@ -34,6 +35,7 @@
"response_consumer",
"classifier",
"processor",
"docker_mode"
],
)

Expand Down Expand Up @@ -109,6 +111,7 @@ def _initialize_configuration(self, config):
response_consumer=None,
classifier=None,
processor=None,
docker_mode=None
)
if isinstance(config, Path):
try:
Expand Down Expand Up @@ -326,9 +329,16 @@ def _get_model_path(self, user_id: str) -> Path:
return super()._get_model_path(user_id)

def process_text(self, text: str, language: str = None):
return self.get_response_consumer().process(
self.get_pipeline().analyse_text_to_cas(source=text, language=language)
)
try:
return self.get_response_consumer().process(
self.get_pipeline().analyse_text_to_cas(source=text, language=language)
)
except RequestException as e:
log_str = f"AHD not accessible: '{e}'"
if self.get_configuration().docker_mode:
sys.exit(log_str)
else:
logging.error(log_str)

def fit(
self,
Expand Down
1 change: 1 addition & 0 deletions inception_ahd_recommender/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
- MODEL_DIR=/models
- RECOMMENDER_WORKERS=4
- RECOMMENDER_ADDRESS=:5000
- DOCKER_MODE=True
ports:
- "5000:5000"
networks:
Expand Down
13 changes: 12 additions & 1 deletion inception_ahd_recommender/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
import pathlib
import sys
from pydoc import locate

from typing import Union

from ariadne.contrib.external_server_consumer import ProcessorType
from ariadne.contrib.external_uima_classifier import AHDClassifier
from ariadne.server import Server


def eval_bool(bool_str: Union[str, bool]) -> bool:
if isinstance(bool_str, bool):
return bool_str
elif isinstance(bool_str, str):
return {"true": True, "false": False, "yes": True, "no": False,
"y": True, "n": False, "j": True, "ja": True, "nein": False}.get(bool_str.lower())
else:
return True

_config = {
"address": os.getenv("EXTERNAL_SERVER_ADDRESS", "http://localhost:8080"),
"security_token": os.getenv("EXTERNAL_SERVER_TOKEN", ""),
Expand All @@ -20,6 +29,7 @@
),
"classifier": os.getenv("CLASSIFIER", False),
"processor": os.getenv("PROCESSOR", ProcessorType.CAS),
"docker_mode": eval_bool(os.getenv("DOCKER_MODE", True)),
}

_server_handle = os.getenv("SERVER_HANDLE", "deid_recommender")
Expand Down Expand Up @@ -58,6 +68,7 @@
"./prefab-mapping-files/deid_mapping_singlelayer.json",
"classifier": os.getenv("CLASSIFIER", False),
"processor": os.getenv("PROCESSOR", ProcessorType.CAS),
"docker_mode": False
}
_classifier = (
AHDClassifier if not _config["classifier"] else locate(_config["classifier"])
Expand Down

0 comments on commit 72f873f

Please sign in to comment.