Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use supervisord to supervise Postgres and Caddy in the Complement image. #12480

Merged
merged 12 commits into from
Apr 27, 2022
Merged
1 change: 1 addition & 0 deletions changelog.d/12480.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use supervisord to supervise Postgres and Caddy in the Complement image.
reivilibre marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions docker/Dockerfile-workers
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ RUN rm /etc/nginx/sites-enabled/default
# Copy Synapse worker, nginx and supervisord configuration template files
COPY ./docker/conf-workers/* /conf/

# Copy a script to prefix log lines with the supervisor program name
COPY ./docker/prefix-log /usr/local/bin/

# Expose nginx listener port
EXPOSE 8080/tcp

Expand Down
5 changes: 4 additions & 1 deletion docker/complement/SynapseWorkers.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ WORKDIR /data
# Copy the caddy config
COPY conf-workers/caddy.complement.json /root/caddy.json

COPY conf-workers/postgres.supervisord.conf /etc/supervisor/conf.d/postgres.conf
COPY conf-workers/caddy.supervisord.conf /etc/supervisor/conf.d/caddy.conf

# Copy the entrypoint
COPY conf-workers/start-complement-synapse-workers.sh /

# Expose caddy's listener ports
EXPOSE 8008 8448

ENTRYPOINT /start-complement-synapse-workers.sh
ENTRYPOINT ["/start-complement-synapse-workers.sh"]

# Update the healthcheck to have a shorter check interval
HEALTHCHECK --start-period=5s --interval=1s --timeout=1s \
Expand Down
7 changes: 7 additions & 0 deletions docker/complement/conf-workers/caddy.supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[program:caddy]
command=/usr/local/bin/prefix-log /root/caddy run --config /root/caddy.json
autorestart=unexpected
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
16 changes: 16 additions & 0 deletions docker/complement/conf-workers/postgres.supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[program:postgres]
command=/usr/local/bin/prefix-log /usr/bin/pg_ctlcluster 13 main start --foreground

# Lower priority number = starts first
priority=100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following up on #12480 (comment):

Do we actually need postgres to be a higher prority than synapse? It's not like there's any guarantee that doing so will mean that postgres is ready before synapse starts.

No, though Synapse's configuration already defined it at priority 500 so it seemed like this was the intention.

I'm not sure the priority was particularly carefully chosen, tbh. (ftr, this bit came from #9162).

(I just realised: The main Synapse process' priority is actually defined as 10; Redis' is 1. Postgres should probably be on the same level as Redis if we're going to have priority ordering?)

If we're going to have priority ordering, then yes it ought to be lower than Synapse.

I would have thought that Postgres being up first might avoid some noisy errors in Synapse's logs.

If it actually manages to start up in 1 second, then yes maybe. TBH though I would optimise for fastest runtime rather than lack of noise in the logs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's set this to be the same as Redis for now, and get this PR merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh you're way ahead of me


autorestart=unexpected
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

# Use 'Fast Shutdown' mode which aborts current transactions and closes connections quickly.
# (Default (TERM) is 'Smart Shutdown' which stops accepting new connections but
# lets existing connections close gracefully.)
stopsignal=INT
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ function log {
# Replace the server name in the caddy config
sed -i "s/{{ server_name }}/${SERVER_NAME}/g" /root/caddy.json

log "starting postgres"
pg_ctlcluster 13 main start

log "starting caddy"
/root/caddy start --config /root/caddy.json

# Set the server name of the homeserver
export SYNAPSE_SERVER_NAME=${SERVER_NAME}

Expand Down
4 changes: 0 additions & 4 deletions docker/conf/log.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ version: 1

formatters:
precise:
{% if worker_name %}
format: '%(asctime)s - worker:{{ worker_name }} - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'
{% else %}
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'
{% endif %}

handlers:
{% if LOG_FILE_PATH %}
Expand Down
2 changes: 1 addition & 1 deletion docker/configure_workers_and_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
# Templates for sections that may be inserted multiple times in config files
SUPERVISORD_PROCESS_CONFIG_BLOCK = """
[program:synapse_{name}]
command=/usr/local/bin/python -m {app} \
command=/usr/local/bin/prefix-log /usr/local/bin/python -m {app} \
--config-path="{config_path}" \
--config-path=/conf/workers/shared.yaml \
--config-path=/conf/workers/{name}.yaml
Expand Down
5 changes: 5 additions & 0 deletions docker/prefix-log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
reivilibre marked this conversation as resolved.
Show resolved Hide resolved

exec 1> >(awk '{print "'"${SUPERVISOR_PROCESS_NAME}"' | "$0}' >&1)
exec 2> >(awk '{print "'"${SUPERVISOR_PROCESS_NAME}"' | "$0}' >&2)
exec "$@"