Skip to content

Commit

Permalink
METS Server: try to remove UDS socket at sys.exit
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Jan 26, 2024
1 parent e9c20e0 commit aba2263
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/ocrd/mets_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/ocrd/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aba2263

Please sign in to comment.