Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
paperless-baremetal/startstop.build.sh
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
240 lines (189 sloc)
4.63 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -u | |
. build.profile | |
TO=${PROJECT}/startstop.sh | |
install -m 755 <( cat <<_EOP_ | |
#!/bin/bash | |
set -e | |
set -u | |
export XDG_RUNTIME_DIR=/run/user/\$(id -u \$USER) | |
. profile | |
function rm_pidfiles() { | |
rm -fv "\${PIDFILE}" "\${PGIDFILE}" | |
} | |
function pwait() { | |
if [[ -x /usr/bin/pwait ]]; then | |
/usr/bin/pwait "\${@}" | |
else | |
pidwait "\${@}" | |
fi | |
} | |
#### nginx #### | |
srv_nginx_start() { | |
nginx -t | |
trap rm_pidfiles EXIT | |
nginx -g 'daemon off;' | |
} | |
srv_nginx_restart() { | |
nginx -s reload | |
} | |
srv_nginx_stop() { | |
nginx -s quit | |
} | |
#### redis #### | |
srv_redis_start() { | |
# redis is special: it would start under any condition | |
local _opts=( | |
--port 0 | |
--unixsocket "${DEVSHM}/redis.sock" | |
--pidfile "${DEVSHM}/redis.pid" | |
) | |
trap rm_pidfiles EXIT | |
redis-server "\${_opts[@]}" | |
} | |
#### gunicorn #### | |
srv_gunicorn_start() { | |
cd "${PROJECT}/paperless-ngx/src" | |
_opts=( | |
--config ../gunicorn.conf.py | |
paperless.asgi:application | |
--bind "unix:${DEVSHM}/gunicorn.sock" | |
--pid "${DEVSHM}/gunicorn.pid" | |
--error-logfile "${LOGDIR}/gunicorn-error.log" | |
--access-logfile "${LOGDIR}/gunicorn-access.log" | |
--worker-tmp-dir "${DEVSHM}" | |
) | |
trap rm_pidfiles EXIT | |
gunicorn "\${_opts[@]}" | |
} | |
#### consumer #### | |
srv_consumer_start() { | |
cd "${PROJECT}/paperless-ngx/src" | |
trap rm_pidfiles EXIT | |
./manage.py document_consumer | |
} | |
#### scheduler #### | |
srv_scheduler_start() { | |
cd "${PROJECT}/paperless-ngx/src" | |
trap rm_pidfiles EXIT | |
celery --app paperless beat --loglevel INFO | |
} | |
#### worker #### | |
srv_worker_start() { | |
cd "${PROJECT}/paperless-ngx/src" | |
trap rm_pidfiles EXIT | |
celery --app paperless worker --loglevel WARNING | |
} | |
#### migrate #### | |
srv_migrate() { | |
pushd "${PROJECT}/paperless-ngx/src" | |
_opts=( | |
--no-color | |
) | |
r=0 | |
./manage.py migrate "\${_opts[@]}" --check || r=\$? | |
if [[ \$r == 1 ]]; then | |
./manage.py migrate "\${_opts[@]}" | |
fi | |
popd | |
} | |
#### generic #### | |
srv_generic_status() { | |
if [[ -s "\${PGIDFILE}" ]]; then | |
local pgid | |
read -a pgid < "\${PGIDFILE}" | |
local pids | |
pids=\$(pgrep -g \${pgid}) | |
if [[ \$? = 0 ]]; then | |
ps -f --pid \${pids} | |
else | |
echo "processs group \${pgid} has no running processes for \${PGIDFILE}" | |
fi | |
else | |
if [[ -s "\${PIDFILE}" ]]; then | |
local pid | |
read -a pid < "\${PIDFILE}" | |
ps -f --pid \${pid} --ppid \${pid} | |
else | |
echo "# no pgid or pid file found: \${PGIDFILE} \${PIDFILE}" | |
fi | |
fi | |
} | |
srv_generic_stop() { | |
if [[ -s "\${PGIDFILE}" ]]; then | |
local pgid | |
read -a pgid < "\${PGIDFILE}" | |
echo "killing process group \${pgid}" | |
kill -- -\${pgid} | |
echo "waiting for process group \${pgid} to die" | |
pwait --pgroup \${pgid} | |
else | |
if [[ -s "\${PIDFILE}" ]]; then | |
local pid | |
read -a pid < "\${PIDFILE}" | |
kill -- \${pid} | |
echo "waiting for process \${pid} to die" | |
tail -f --pid=\${pid} /dev/null | |
else | |
echo "# no pid file found: \${PGIDFILE}" | |
fi | |
fi | |
} | |
srv_generic_restart() { | |
srv_generic_stop | |
srv_generic_start | |
} | |
#### main() #### | |
cd "${PROJECT}" | |
cmd="\${1:-help}" | |
srv="\${2:-all}" | |
if [[ \${srv} = 'all' ]]; then | |
_srv=(redis nginx gunicorn consumer scheduler worker) | |
if [[ \${cmd} = 'start' ]]; then | |
srv_migrate | |
fi | |
# poor mans reverse | |
if [[ \${cmd} = 'stop' ]]; then | |
_srv=(\$(eval eval echo "'\"\\\${_srv['{\$((\${#_srv[@]}-1))..0}']}\"'")) | |
fi | |
for srv in \${_srv[@]}; do | |
echo "### ./startstop.sh \${cmd} \${srv}" | |
"${PROJECT}"/startstop.sh \${cmd} \${srv} || echo "RET: $?" | |
done | |
exit | |
fi | |
PIDFILE=${DEVSHM}/srv-\${srv}.pid | |
PGIDFILE=${DEVSHM}/srv-\${srv}.pgid | |
if [[ \$(type -t srv_\${srv}_\${cmd}) = 'function' ]]; then | |
case "\${cmd}" in | |
start) | |
mkdir -p "${DEVSHM}" "${LOGDIR}" "${TMPDIR}" | |
[[ -s "\${PIDFILE}" ]] && echo "# \${PIDFILE} found, refuse start of \${srv}" && exit 0 | |
[[ -s "\${PGIDFILE}" ]] && echo "# \${PGIDFILE} found, refuse start of \${srv}" && exit 0 | |
exec 99>"\${PIDFILE}" || exit 1 | |
flock 99 || exit 1 | |
set -m | |
coproc { | |
srv_\${srv}_\${cmd}; | |
} > ${LOGDIR}/srv-\${srv}.log 2>&1 | |
echo "\${COPROC_PID}" >> "\${PIDFILE}" | |
ps opgid= "\${COPROC_PID}" > "\${PGIDFILE}" | |
exit | |
;; | |
*) | |
echo "########## default fallback to srv_\${srv}_\${cmd} ###############" | |
srv_\${srv}_\${cmd} | |
exit | |
;; | |
esac | |
else | |
# echo "srv_\${srv}_\${cmd}() not declared" | |
if [[ \$(type -t srv_generic_\${cmd}) = 'function' ]]; then | |
srv_generic_\${cmd} | |
exit | |
fi | |
fi | |
echo "#### oh no... ####" | |
_EOP_ | |
) "${TO}" |