matrix-docker-ansible-deploy/roles/matrix-synapse/files/usr-local-bin/matrix-synapse-worker-write-pid

31 lines
1.1 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# Find a synapse worker's PID and write it to a file so systemd can manage it as a service
# example invocation:
# matrix-synapse-worker-write-pid user_dir:18700 /run/matrix-synapse-worker.user_dir:18700.pid
docker_api_call() { curl --silent --unix-socket /var/run/docker.sock ${@}; }
TARGETCONTAINER=matrix-synapse
TARGETWORKER=${1}
PIDFILE=${2}
# get ID list of subprocesses executed in $TARGETCONTAINER, and for each..
for EXECID in $(docker_api_call http://localhost/containers/${TARGETCONTAINER}/json | jq --raw-output '.ExecIDs[]')
do
# fetch detailed process info
EXECINFO=$(docker_api_call http://localhost/exec/${EXECID}/json)
# extract config file path from last command argument
WORKERCONFIGFILE=$(echo ${EXECINFO} | jq --raw-output .ProcessConfig.arguments[-1])
# reconstruct worker name
WORKERNAME=${WORKERCONFIGFILE#*/worker.}
WORKERNAME=${WORKERNAME%.yaml}
# if name matches the target worker: write out most recent PID & quit
[ "${WORKERNAME}" = "${TARGETWORKER}" ] \
&& echo ${EXECINFO} | jq --raw-output .Pid > ${PIDFILE} \
&& exit 0
done