Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with conversion runtime timeout, and make timeout configurable #1

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ curl -F "file=@tests/hello-world.pdf;" http://localhost:8888/?mode=pdf2txt

Note that files not ending in `.pdf` will be rejected.

### Timeout of conversion

Be default, if a conversion takes longer than 3min (180sec), the conversion
process will be killed (and, depending on the mode, the next conversion method
be tried, see avove).

The timeout is configurable by passing `convert_timeout=NN` as API parameter.

### Converting PDFs located in GCS buckets

It is possible to convert PDF files in GCS buckets via the endpoint
Expand Down
21 changes: 14 additions & 7 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
import tempfile
from collections import namedtuple
from subprocess import PIPE, Popen
from subprocess import PIPE, Popen, TimeoutExpired

from dotenv import load_dotenv
from fastapi import FastAPI, File, HTTPException, UploadFile
Expand Down Expand Up @@ -67,7 +67,7 @@ def check_input_file(uri: str) -> None:
raise HTTPException(status_code=400, detail="Input bucket not found in ACCEPTED_BUCKETS")


def convert_file(temp_dir: str, file_path_in: str, mode: str, params: str) -> FileResponse:
def convert_file(temp_dir: str, file_path_in: str, mode: str, convert_timeout: int, params: str) -> FileResponse:
"""Convert the given PDF files to text."""
file_path_out: str = file_path_in + ".txt"
logging.debug(f"Entering convert_file: PARAM2PROGRAM_FOUND.keys: {PARAM2PROGRAM_FOUND.keys()}")
Expand Down Expand Up @@ -100,7 +100,12 @@ def convert_file(temp_dir: str, file_path_in: str, mode: str, params: str) -> Fi
logging.debug(f"Running {cmd}")
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
logging.debug("Starting conversion process")
out, err = p.communicate()
try:
out, err = p.communicate(timeout=convert_timeout)
except TimeoutExpired:
logging.warning(f"Conversion with mode {mode} timed out after {convert_timeout}secs")
p.kill()

logging.debug(f"stdout={out.decode('utf-8')}, stderr={err.decode('utf-8')}")
logging.debug("Conversion process finished")

Expand All @@ -123,7 +128,7 @@ def healthcheck() -> str:


@app.post("/from_bucket")
def handle_file_from_bucket(uri: str, mode: str = "auto", params: str = "") -> FileResponse:
def handle_file_from_bucket(uri: str, mode: str = "auto", convert_timeout: int = 180, params: str = "") -> FileResponse:
"""Entry point for API call to convert pdf via bucket url to text."""
check_input_file(uri)
temp_dir = tempfile.mkdtemp()
Expand All @@ -134,11 +139,13 @@ def handle_file_from_bucket(uri: str, mode: str = "auto", params: str = "") -> F
blob.download_to_filename(file_path_in)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to obtain file from bucket: {e}")
return convert_file(temp_dir, file_path_in, mode, params)
return convert_file(temp_dir, file_path_in, mode, convert_timeout, params)


@app.post("/")
def handle_file(file: UploadFile = File(...), mode: str = "auto", params: str = "") -> FileResponse:
def handle_file(
file: UploadFile = File(...), mode: str = "auto", convert_timeout: int = 180, params: str = ""
) -> FileResponse:
"""Entry point for API call to convert pdf to text."""
if file.filename is None:
raise HTTPException(status_code=400, detail="No filename provided.")
Expand All @@ -153,7 +160,7 @@ def handle_file(file: UploadFile = File(...), mode: str = "auto", params: str =
finally:
file.file.close()

return convert_file(temp_dir, file_path_in, mode, params)
return convert_file(temp_dir, file_path_in, mode, convert_timeout, params)


if __name__ == "__main__":
Expand Down
Loading