From aba2263088648398d9e893461950ef5fef950277 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Fri, 26 Jan 2024 17:19:20 +0100 Subject: [PATCH] METS Server: try to remove UDS socket at sys.exit --- src/ocrd/mets_server.py | 22 ++++++++++++---------- src/ocrd/workspace.py | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/ocrd/mets_server.py b/src/ocrd/mets_server.py index 08011557b5..be2c1333e0 100644 --- a/src/ocrd/mets_server.py +++ b/src/ocrd/mets_server.py @@ -2,24 +2,24 @@ # METS server functionality """ import re -from os import environ, _exit, chmod -from io import BytesIO -from typing import Any, Dict, Optional, Union, List, Tuple +from os import _exit, chmod +from typing import Dict, Optional, Union, List, Tuple from pathlib import Path from urllib.parse import urlparse import socket +import atexit -from fastapi import FastAPI, Request, File, Form, Response +from fastapi import FastAPI, Request, Form, Response from fastapi.responses import JSONResponse -from requests import request, Session as requests_session +from requests import Session as requests_session from requests.exceptions import ConnectionError from requests_unixsocket import Session as requests_unixsocket_session from pydantic import BaseModel, Field, ValidationError import uvicorn -from ocrd_models import OcrdMets, OcrdFile, ClientSideOcrdFile, OcrdAgent, ClientSideOcrdAgent -from ocrd_utils import initLogging, getLogger, deprecated_alias +from ocrd_models import OcrdFile, ClientSideOcrdFile, OcrdAgent, ClientSideOcrdAgent +from ocrd_utils import getLogger, deprecated_alias # # Models @@ -197,9 +197,10 @@ def __init__(self, workspace, url): self.log = getLogger(f'ocrd.mets_server[{self.url}]') def shutdown(self): - self.log.info("Shutting down METS server") if self.is_uds: - Path(self.url).unlink() + if Path(self.url).exists(): + self.log.warning(f'UDS socket {self.url} still exists, removing it') + Path(self.url).unlink() # os._exit because uvicorn catches SystemExit raised by sys.exit _exit(0) @@ -296,7 +297,7 @@ async def stop(): """ Stop the server """ - getLogger('ocrd.models.ocrd_mets').info('Shutting down') + getLogger('ocrd.models.ocrd_mets').info(f'Shutting down METS Server {self.url}') workspace.save_mets() self.shutdown() @@ -308,6 +309,7 @@ async def stop(): self.log.debug(f"chmod 0o677 {self.url}") server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) server.bind(self.url) # creates the socket file + atexit.register(self.shutdown) server.close() chmod(self.url, 0o666) uvicorn_kwargs = {'uds': self.url} diff --git a/src/ocrd/workspace.py b/src/ocrd/workspace.py index 7772c54d79..633e45acf5 100644 --- a/src/ocrd/workspace.py +++ b/src/ocrd/workspace.py @@ -82,7 +82,7 @@ def __init__(self, resolver, directory, mets=None, mets_basename=DEFAULT_METS_BA mets = ClientSideOcrdMets(mets_server_url) if mets.workspace_path != self.directory: raise ValueError(f"METS server {mets_server_url} workspace directory {mets.workspace_path} differs " - "from local workspace directory {self.directory}. These are not the same workspaces.") + f"from local workspace directory {self.directory}. These are not the same workspaces.") else: mets = OcrdMets(filename=self.mets_target) self.mets = mets