2api / entrypoint.sh
shenhao-stu
fix: wait for API ready before running initial token cleanup
5abf60c
raw
history blame
2.02 kB
#!/bin/sh
#
# entrypoint.sh - Start crond for scheduled cleanup, then launch CLIProxyAPI
#
# Environment variables:
# CLEANUP_CRON_SCHEDULE - Cron schedule for token cleanup (default: "0 */6 * * *", every 6h)
# Examples:
# "0 */1 * * *" every hour
# "0 */6 * * *" every 6 hours (default)
# "0 2 * * *" daily at 2:00 AM
# CLEANUP_STARTUP_DELAY - Seconds to wait after service is ready before first cleanup (default: 10)
#
set -e
echo "[entrypoint] Starting up at $(date)"
CLEANUP_CRON_SCHEDULE="${CLEANUP_CRON_SCHEDULE:-0 */6 * * *}"
CLEANUP_STARTUP_DELAY="${CLEANUP_STARTUP_DELAY:-10}"
mkdir -p /tmp/logs /etc/crontabs
# Write crontab
cat > /etc/crontabs/root << EOF
# CLIProxyAPI invalid token cleanup
${CLEANUP_CRON_SCHEDULE} /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
EOF
echo "[entrypoint] Cleanup schedule: ${CLEANUP_CRON_SCHEDULE}"
# Start crond (busybox)
crond -l 8 -L /tmp/logs/crond.log
echo "[entrypoint] crond started"
# Wait for the API to be ready, then run the initial cleanup in background.
# This runs in a subshell so it doesn't block the main service from starting.
(
echo "[entrypoint] Waiting for API to be ready (polling localhost:7860)..."
MAX_WAIT=120
WAITED=0
while ! wget -q -O /dev/null http://localhost:7860/ 2>/dev/null; do
sleep 2
WAITED=$((WAITED + 2))
if [ "$WAITED" -ge "$MAX_WAIT" ]; then
echo "[entrypoint] WARNING: API did not become ready after ${MAX_WAIT}s, running cleanup anyway"
break
fi
done
echo "[entrypoint] API ready (waited ${WAITED}s). Sleeping ${CLEANUP_STARTUP_DELAY}s before cleanup..."
sleep "$CLEANUP_STARTUP_DELAY"
echo "[entrypoint] Running initial token cleanup..."
/app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
) &
echo "[entrypoint] Launching cli-proxy-api..."
exec /app/cli-proxy-api --config /app/config.yaml "$@"