Skip to content

Commit be69eee

Browse files
committed
Translate prestart and start scripts into Python
- This commit will refactor and translate the startup scripts from https://github.com/tiangolo/uvicorn-gunicorn-docker. - prestart.py will replace prestart.sh - start.py will replace start.sh and start-reload.sh
1 parent 896419a commit be69eee

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ COPY poetry.lock pyproject.toml inboard /app/
44
WORKDIR /app/
55
ENV APP_MODULE=inboard.base.main:app GUNICORN_CONF=/app/inboard/gunicorn_conf.py POETRY_VIRTUALENVS_CREATE=false
66
RUN python -m pip install poetry && poetry install --no-dev --no-interaction -E fastapi
7+
CMD python /app/inboard/start.py
78

89
FROM base as fastapi
9-
ENV APP_MODULE=inboard.fastapi.main:app POETRY_VIRTUALENVS_CREATE=false
10+
ENV APP_MODULE=inboard.fastapi.main:app
11+
CMD python /app/inboard/start.py
1012

1113
FROM base as starlette
12-
ENV APP_MODULE=inboard.starlette.main:app POETRY_VIRTUALENVS_CREATE=false
14+
ENV APP_MODULE=inboard.starlette.main:app
15+
CMD python /app/inboard/start.py

inboard/prestart.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python3
2+
3+
print("Running prestart.py. Add database migrations and other scripts here.")

inboard/start.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import subprocess
4+
from pathlib import Path, PurePath
5+
from typing import Tuple
6+
7+
import uvicorn
8+
9+
10+
def set_app_module() -> str:
11+
if Path("/app/app/main.py").is_file():
12+
default_module_name = "app.main"
13+
elif Path("/app/main.py").is_file():
14+
default_module_name = "main"
15+
module_name = os.getenv("MODULE_NAME", default_module_name)
16+
variable_name = os.getenv("VARIABLE_NAME", "app")
17+
app_module = os.getenv("APP_MODULE", f"{module_name}:{variable_name}")
18+
os.environ["APP_MODULE"] = app_module
19+
return app_module
20+
21+
22+
def set_gunicorn_conf() -> Tuple[str, str]:
23+
if Path("/app/gunicorn_conf.py").is_file():
24+
default_gunicorn_conf = "/app/gunicorn_conf.py"
25+
elif Path("/app/app/gunicorn_conf.py").is_file():
26+
default_gunicorn_conf = "/app/app/gunicorn_conf.py"
27+
elif Path("/gunicorn_conf.py").is_file():
28+
default_gunicorn_conf = "/gunicorn_conf.py"
29+
gunicorn_conf = os.getenv("GUNICORN_CONF", default_gunicorn_conf)
30+
os.environ["GUNICORN_CONF"] = gunicorn_conf
31+
worker_class = os.getenv("WORKER_CLASS", "uvicorn.workers.UvicornWorker")
32+
os.environ["WORKER_CLASS"] = worker_class
33+
return gunicorn_conf, worker_class
34+
35+
36+
def run_pre_start_script(
37+
pre_start_path: str = os.getenv("PRE_START_PATH", "/app/inboard/prestart.py")
38+
) -> None:
39+
try:
40+
print(f"Checking for pre-start script in {pre_start_path}.")
41+
if Path(pre_start_path).is_file():
42+
print(f"Running pre-start script script in {pre_start_path}.")
43+
process = "python" if PurePath(pre_start_path).suffix == ".py" else "sh"
44+
subprocess.run(process, pre_start_path)
45+
else:
46+
print("No pre-start script found.")
47+
except Exception as e:
48+
print(f"Error when running pre-start script: {e}")
49+
50+
51+
def start_server(
52+
app_module: str = str(os.getenv("APP_MODULE", "/app/inboard.base.main:app")),
53+
gunicorn_conf: str = str(
54+
os.getenv("GUNICORN_CONF", "/app/inboard/gunicorn_conf.py")
55+
),
56+
worker_class: str = str(os.getenv("WORKER_CLASS", "uvicorn.workers.UvicornWorker")),
57+
with_reload: bool = bool(os.getenv("WITH_RELOAD", False)),
58+
) -> None:
59+
if with_reload:
60+
uvicorn.run(
61+
app_module,
62+
host=os.getenv("HOST", "0.0.0.0"),
63+
port=int(os.getenv("PORT", "80")),
64+
log_level=os.getenv("LOG_LEVEL", "info"),
65+
reload=True,
66+
)
67+
else:
68+
subprocess.run("gunicorn", "-k", worker_class, "-c", gunicorn_conf, app_module)
69+
70+
71+
if __name__ == "__main__":
72+
set_app_module()
73+
set_gunicorn_conf()
74+
run_pre_start_script()
75+
start_server()

0 commit comments

Comments
 (0)