Files
Auto_Bangumi/entrypoint.sh
Estrella Pan 6f72cfe1df perf(startup): defer LLM SDK imports and gate the volume chown
Two things dominated container start:

- The LLM SDKs (anthropic + openai + google-genai) imported at module load
  via parser.analyser/__init__ → llm.py, costing ~0.7s on every boot even
  though the LLM parser is off by default. Move those three heavy imports
  into the provider branch of __init__ and each _parse_* method, so they
  load only when an LLMParser is actually built. Measured: importing
  parser.analyser drops 782ms → 203ms; none of the SDKs are in sys.modules
  after the startup import chain. httpx/httpx_socks stay module-level (cheap,
  and the proxy tests patch them by module path).

- entrypoint.sh ran `chown -R /app/data /app/config` unconditionally every
  start — O(files), which a large poster cache on a NAS turns into the
  dominant startup cost. Walk the tree only when ownership needs fixing
  (first run, changed PUID/PGID, or a wrong-owned volume root), tracked by a
  marker; the legacy bangumi.json migration chowns its one file explicitly so
  the gate can't miss it. /home/ab (image-internal, tiny) is still chowned
  every time.

Also regenerates the auto-imports dts for onResponseError (exported for the
401 test in the previous commit).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FAxVyRwrY7z5NotTtgnwVs
2026-07-04 19:16:49 +02:00

63 lines
2.0 KiB
Bash
Raw Permalink 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.
#!/bin/bash
# shellcheck shell=bash
umask ${UMASK}
if [ -f /config/bangumi.json ]; then
mv /config/bangumi.json /app/data/bangumi.json
# Created as root by the mv; the gated chown below may skip the tree walk,
# so fix this one legacy file explicitly.
chown ab:ab /app/data/bangumi.json
fi
groupmod -o -g "${PGID}" ab
usermod -o -u "${PUID}" ab
# 重启循环让 shell 站在 tini 和应用之间,因此必须自己转发信号并等待
# 应用退出,否则 docker stop 时 shell 先死、tini 跟着退出,python 会被
# SIGKILL 打断(SQLite WAL 来不及落盘)。
child=""
forward_signal() {
if [ -n "${child}" ]; then
kill -TERM "${child}" 2>/dev/null
wait "${child}"
exit $?
fi
exit 0
}
trap forward_signal TERM INT
while true; do
# 应用持久卷中的在线更新覆盖层(在启动应用前、以 root 运行)。脚本内部已全程
# 兜底 try/except,这里再加 `|| true`:覆盖层失败绝不能阻断容器启动。
python /app/boot_overlay.py || true
# Recursive chown of the mounted volumes is O(files): a large poster cache
# on a NAS makes it the dominant cost of every container start. Only walk
# the tree when ownership actually needs fixing — first run or a changed
# PUID/PGID — recorded by a marker. /home/ab is image-internal and tiny, so
# chown it every time.
chown ab:ab -R /home/ab
own_marker="/app/config/.ab_owner"
want="${PUID}:${PGID}"
if [ "$(cat "${own_marker}" 2>/dev/null)" != "${want}" ] ||
[ "$(stat -c '%u:%g' /app/data 2>/dev/null)" != "${want}" ] ||
[ "$(stat -c '%u:%g' /app/config 2>/dev/null)" != "${want}" ]; then
echo "${want}" >"${own_marker}"
chown ab:ab -R /app/data /app/config
fi
su-exec "${PUID}:${PGID}" python main.py &
child=$!
wait "${child}"
status=$?
child=""
if [ -f /app/config/updates/.restart ]; then
rm -f /app/config/updates/.restart
continue
fi
exit "${status}"
done