peppa/infra/act-runner/daemons.sh
peppa-bot 4ae203e037
All checks were successful
peppa/lint lint: success за 2с
peppa/typecheck typecheck: success за 49с
peppa/build build: success за 35с
peppa/bundle bundle: success за 0с
peppa/test test: success за 1652с
peppa-ci / ci (pull_request) Successful in 29m39s
PEP-320: один демон исполнителя проверок на регистрацию
supervise.sh пишет в .pid номер самого демона, журнал без буфера sed,
лишних демонов каталога останавливает SIGTERM; register.sh сначала
останавливает прежнего демона. Тест с mock-act-runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 13:42:33 +00:00

62 lines
2.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# shellcheck shell=sh
# Общие функции supervise.sh и register.sh (PEP-320): какие `act_runner daemon` работают в каталоге
# регистрации и как их мягко остановить. busybox sh (образ gitea/act_runner, alpine), есть /proc.
# Пути — через переменные окружения (тест act-runner-supervise.test.ts), по умолчанию прежние.
RUNNERS_DIR=${PEPPA_RUNNERS_DIR:-/data/runners}
ACT_CONFIG=${PEPPA_ACT_RUNNER_CONFIG:-/peppa/act-runner/config.yaml}
ACT_BIN=${PEPPA_ACT_RUNNER_BIN:-act_runner}
PROC=${PEPPA_PROC_DIR:-/proc}
# act_runner по SIGTERM доделывает идущие задания (runner.timeout 1h) — ждём столько и ещё запас.
STOP_WAIT=${PEPPA_RUNNER_STOP_WAIT:-3900}
# is_daemon <pid> — процесс жив (не зомби) и это `act_runner daemon`
is_daemon() {
_cmd=$(tr '\0' '\n' < "$PROC/$1/cmdline" 2>/dev/null) || return 1
case "$_cmd" in *act_runner*) ;; *) return 1 ;; esac
printf '%s\n' "$_cmd" | grep -qx daemon
}
# real_dir <каталог> — абсолютный путь без ссылок (так его показывает /proc/<n>/cwd)
real_dir() { (cd "$1" 2>/dev/null && pwd -P); }
# daemons_in <каталог> — номера демонов, чей рабочий каталог — этот (по одному в строке)
daemons_in() {
_want=$(real_dir "$1") || return 0
[ -n "$_want" ] || return 0
for _p in "$PROC"/[0-9]*; do
[ "$(readlink "$_p/cwd" 2>/dev/null)" = "$_want" ] || continue
is_daemon "${_p##*/}" && echo "${_p##*/}"
done
return 0
}
# owns <каталог> <pid> — <pid> — демон этого каталога
owns() {
[ -n "$2" ] || return 1
is_daemon "$2" || return 1
[ "$(readlink "$PROC/$2/cwd" 2>/dev/null)" = "$(real_dir "$1")" ]
}
# runner_sum <каталог> — отпечаток нынешней регистрации (.runner)
runner_sum() { cksum < "$1/.runner" 2>/dev/null; }
# stop_all <каталог> — SIGTERM всем демонам каталога и ждать выхода; дольше STOP_WAIT — SIGKILL
stop_all() {
_pids=$(daemons_in "$1")
[ -n "$_pids" ] || return 0
echo "[register] stop daemon(s) of $1: $(echo $_pids)"
kill -TERM $_pids 2>/dev/null || true
_waited=0
while [ -n "$(daemons_in "$1")" ]; do
if [ "$_waited" -ge "$STOP_WAIT" ]; then
echo "[register] daemon(s) ignored SIGTERM for ${STOP_WAIT}s — SIGKILL"
kill -KILL $(daemons_in "$1") 2>/dev/null || true
sleep 1
break
fi
sleep 1
_waited=$((_waited + 1))
done
return 0
}