Skip to content
Permalink
main
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 211 lines (168 sloc) 4.16 KB
#!/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
cd "${PROJECT}"
cmd="\${1:-help}"
srv="\${2:-all}"
if [[ \${srv} = 'all' ]]; then
for srv in redis nginx gunicorn consumer scheduler worker; 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
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
}
#### 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() ####
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}"