Skip to content
This repository was archived by the owner on Jun 30, 2024. It is now read-only.

Commit 7da24b0

Browse files
committed
Fix: Correct logging approach in Docker.
1 parent 06d9687 commit 7da24b0

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

docker/docker_tools.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,15 @@ def _build_phase_1(
802802
# Set up nginx (partially -- more in step 3 below).
803803
"rm /etc/nginx/sites-enabled/default",
804804
"ln -sf $RUNESTONE_PATH/docker/nginx/sites-available/runestone /etc/nginx/sites-enabled/runestone",
805-
# Send nginx logs to stdout/stderr, so they'll show up in Docker logs.
806-
"ln -sf /dev/stdout /var/log/nginx/access.log",
807-
"ln -sf /dev/stderr /var/log/nginx/error.log",
805+
# Send celery, gunicorn, and nginx logs to Docker's stdout/stderr, so they'll show up in Docker logs even after restarting the servers. (Linking to ``/dev/stdout`` and ``/dev/stderr`` means that on restart, these links point not to Docker, but to the console which invoked the restart). See a `related issue on Github <https://github.com/moby/moby/issues/19616#issuecomment-174355979>`_.
806+
"ln -sf /proc/1/fd/1 /var/log/nginx/access.log",
807+
"ln -sf /proc/1/fd/2 /var/log/nginx/error.log",
808+
"mkdir -p /var/log/celery",
809+
"ln -sf /proc/1/fd/1 /var/log/celery/access.log",
810+
"ln -sf /proc/1/fd/2 /var/log/celery/error.log",
811+
"mkdir -p /var/log/gunicorn",
812+
"ln -sf /proc/1/fd/1 /var/log/gunicorn/access.log",
813+
"ln -sf /proc/1/fd/2 /var/log/gunicorn/error.log",
808814
# Set up web2py routing.
809815
"cp $RUNESTONE_PATH/docker/routes.py $WEB2PY_PATH",
810816
# ``sphinxcontrib.paverutils.run_sphinx`` lacks venv support -- it doesn't use ``sys.executable``, so it doesn't find ``sphinx-build`` in the system path when executing ``/srv/venv/bin/runestone`` directly, instead of activating the venv first (where it does work). As a huge, ugly hack, symlink it to make it available in the system path.

docker/docker_tools_misc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ def _start_servers(dev: bool) -> None:
8484
if bs_config == "development":
8585
dev = True
8686

87-
# sudo doesn't pass root's env vars; provide only the env vars Celery needs when invoking it.
87+
# ``sudo`` doesn't pass root's env vars; provide only the env vars Celery needs when invoking it.
8888
xqt(
8989
'sudo -u www-data env "PATH=$PATH" "REDIS_URI=$REDIS_URI" '
9090
"poetry run celery --app=scheduled_builder worker --pool=threads "
9191
"--concurrency=3 --loglevel=info "
92-
# This redirect ensures output ends up in the Docker log even if the servers are restarted. Sending to ``/dev/stdout`` only works at initial startup, but doesn't redirect after the servers are restarted. For more discussion, see `Github <https://github.com/moby/moby/issues/19616#issuecomment-174355979>`_.
93-
"--logfile=/proc/1/fd/1 &",
92+
# Celery runs as the ``www-data`` user, so it doesn't have access to the root-owned log files (which are symbolic links to files owned by root -- changing permission doesn't work). Therefore, redirect output (as root) to make this work.
93+
"> /var/log/celery/access.log 2> /var/log/error.log &",
9494
cwd=f"{env.RUNESTONE_PATH}/modules",
9595
)
9696

@@ -100,9 +100,9 @@ def _start_servers(dev: bool) -> None:
100100
"--error_path /tmp "
101101
"--gconfig $RUNESTONE_PATH/docker/gunicorn_config/fastapi_config.py "
102102
# This much match the address in `./nginx/sites-available/runestone.template`.
103-
f"--bind unix:/run/fastapi.sock {'--reload ' if dev else ''}"
104-
# See previous comment on redirecting output to ``/dev/stdout`` even after server restarts.
105-
+ "2>&1 > /proc/1/fd/1 &",
103+
f"--bind unix:/run/fastapi.sock {'--reload ' if dev else ''} "
104+
# If logging to a file, then Gunicorn tries to append to it (open the file with a mode of "a+"). This fails if the underlying "file" is actually ``stdout`` or ``stderr`` with the error ``io.UnsupportedOperation: File or stream is not seekable.``. So, redirect these instead.
105+
"> /var/log/celery/access.log 2> /var/log/error.log &",
106106
"service nginx start",
107107
"poetry run gunicorn -D --config $RUNESTONE_PATH/docker/gunicorn_config/web2py_config.py &",
108108
cwd=f"{env.RUNESTONE_PATH}/docker/gunicorn_config",

0 commit comments

Comments
 (0)