ufal / factgenie

Data visualization and span-based annotation of generated texts
MIT License
15 stars 1 forks source link

Document and stress test production deployment #50

Open oplatek opened 2 months ago

oplatek commented 2 months ago

I just tested on localhost without (without host_prefix)

gunicorn -w 1 --threads 10 'factgenie.cli:create_app()' --bind 127.0.0.1:11111 on commit 76ab57c114cdc398e385dcb3791f3c8318df36d4

But we should test it thoroughly and then add it to README. We should include a version of gunicorn, which we use.

dmhowcroft commented 2 months ago

I'm using Docker Compose to launch the server behind an nginx reverse proxy.

One level up from factgenie repo root, I have the following docker-compose.yml

services:
  factgenie:
    container_name: factgenie
    image: factgenie
    restart: on-failure
    ports:
      - 8080:80
    build: ./factgenie

In the factgenie repo root I added this Dockerfile:

FROM python:3.10

RUN mkdir -p /usr/src/factgenie
WORKDIR /usr/src/factgenie

COPY . /usr/src/factgenie

RUN pip install -e .

EXPOSE 80
ENTRYPOINT ["gunicorn", "-b", ":80", "-w", "1", "--threads", "2", "factgenie.cli:create_app"]

The docker compose file ensures that the gunicorn server running on port 80 inside docker is exposed at localhost:8080, which is where nginx expects to find it when forwarding traffic.

When running wget localhost:8080 or loading DOMAIN.TLD/MY_HOST_PREFIX in a browser, I get the following error:

factgenie  | [2024-07-16 12:06:48 +0000] [6] [ERROR] Error handling request /MY_HOST_PREFIX/
factgenie  | Traceback (most recent call last):
factgenie  |   File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/gthread.py", line 282, in handle
factgenie  |     keepalive = self.handle_request(req, conn)
factgenie  |   File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/gthread.py", line 334, in handle_request
factgenie  |     respiter = self.wsgi(environ, resp.start_response)
factgenie  | TypeError: create_app() takes 0 positional arguments but 2 were given
oplatek commented 2 months ago

@dmhowcroft I test it without prefix on a local setup on latest commit just to be sure.

##### First terminal - in the factgenie root repo
$ gunicorn -w 1 --threads 2 'factgenie.cli:create_app()' --bind 127.0.0.1:8080
2024-07-16 14:29:04 +0200] [55374] [INFO] Starting gunicorn 22.0.0
[2024-07-16 14:29:04 +0200] [55374] [INFO] Listening at: http://127.0.0.1:8080 (55374)
[2024-07-16 14:29:04 +0200] [55374] [INFO] Using worker: gthread
[2024-07-16 14:29:04 +0200] [55375] [INFO] Booting worker with pid: 55375
2024-07-16 14:29:12 INFO Application ready
2024-07-16 14:31:15 INFO Main page loaded
##### Second terminal
$ wget localhost:8080
--2024-07-16 14:31:15--  http://localhost:8080/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3447 (3.4K) [text/html]
Saving to: ‘index.html’

index.html                                     100%[=================================================================================================>]   3.37K  --.-KB/s    in 0s      

2024-07-16 14:31:15 (365 MB/s) - ‘index.html’ saved [3447/3447]

My version used:

I have tested it with a prefix and reproduced your bug. See https://github.com/kasnerz/factgenie/issues/51

oplatek commented 2 weeks ago

FYI @kasnerz: Find below how I document my setup for deployment. Should we add it to the wiki?

ATM I am starting at ufal factgenie with the following config using gunicorn using this script. The factgenie is installed via pip install -e .[dev,deploy] into virtualenv.

factgenie/config.yml

---
debug: false
host_prefix: "/demo/factgenie"
logging_level: INFO
login:
  active: true
  username: MASKED_OUT
  password: MASKED_OUT

I run this gunicorn.sh script from the factgenie root dir

#!/bin/bash
# Section in the `/etc/nginx/sites-available/default`
#
# ```
#     # factgenie
#     rewrite ^/factgenie/(.*)/$ /demo/factgenie/$1 permanent;
#     location /factgenie {
#         proxy_pass http://localhost:5000/;
#     }
#     location /factgenie/ {
#         proxy_pass http://localhost:5000/;
#     }
#     # end of factgenie
# ```
gunicorn -w 1 --threads 2 'factgenie.cli:create_app()' --bind 127.0.0.1:5000
kasnerz commented 6 days ago

@oplatek You should definitely document how to use dev and deploy arguments for pip - I don't think that's documented anywhere.

It will also be useful to show the gunicorn command for deployment.

Regarding nginx configuration, I think that's quite specific to our setup, so writing specific commands might confuse users that copy-paste it and expect it to work.

oplatek commented 6 days ago

I will document how I deploy it in case where no host_prefix is needed and then I will compare it with the setup where host_prefix is needed. I will document it using nginx and gunicorn tools.