2api / entrypoint.sh
ohmyapi's picture
refactor: disable cleanup by default, set ENABLE_CLEANUP=true to activate
492f303 verified
#!/bin/sh
#
# entrypoint.sh - Launch CLIProxyAPI, optionally with scheduled token cleanup
#
# Set ENABLE_CLEANUP=true to activate automatic invalid token cleanup.
# When disabled (default), behaves identically to a plain CMD ["./cli-proxy-api"].
#
if [ "${ENABLE_CLEANUP}" = "true" ]; then
echo "[entrypoint] Cleanup enabled, starting up at $(date)"
CLEANUP_CRON_SCHEDULE="${CLEANUP_CRON_SCHEDULE:-0 */6 * * *}"
CLEANUP_STARTUP_DELAY="${CLEANUP_STARTUP_DELAY:-10}"
CRON_OK=0
mkdir -p /tmp/logs /tmp/crontabs
# Wrapper script that injects env vars (busybox crond doesn't inherit them)
cat > /tmp/run_cleanup.sh << WRAPPER
#!/bin/sh
export MANAGEMENT_PASSWORD='$(printf '%s' "$MANAGEMENT_PASSWORD" | sed "s/'/'\\\\''/g")'
export FEISHU_WEBHOOK_URL='$(printf '%s' "$FEISHU_WEBHOOK_URL" | sed "s/'/'\\\\''/g")'
export CLEANUP_CONCURRENCY='${CLEANUP_CONCURRENCY:-20}'
exec /app/cleanup_tokens.sh
WRAPPER
chmod +x /tmp/run_cleanup.sh
cat > /tmp/crontabs/root << EOF
${CLEANUP_CRON_SCHEDULE} /tmp/run_cleanup.sh >> /tmp/logs/cleanup.log 2>&1
EOF
echo "[entrypoint] Cleanup schedule: ${CLEANUP_CRON_SCHEDULE}"
if crond -c /tmp/crontabs -l 8 -L /tmp/logs/crond.log 2>/dev/null; then
echo "[entrypoint] crond started"
CRON_OK=1
else
echo "[entrypoint] WARNING: crond failed, will use sleep-loop fallback"
fi
(
MAX_WAIT=120; WAITED=0
while ! wget -q -O /dev/null http://localhost:7860/ 2>/dev/null; do
sleep 2; WAITED=$((WAITED + 2))
[ "$WAITED" -ge "$MAX_WAIT" ] && break
done
sleep "${CLEANUP_STARTUP_DELAY}"
/app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
if [ "$CRON_OK" = "0" ]; then
while true; do
sleep 21600
/app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
done
fi
) &
fi
exec /app/cli-proxy-api --config /app/config.yaml "$@"