ohmyapi commited on
Commit
492f303
·
verified ·
1 Parent(s): 6f54bdd

refactor: disable cleanup by default, set ENABLE_CLEANUP=true to activate

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -1
  2. entrypoint.sh +36 -41
Dockerfile CHANGED
@@ -2,7 +2,7 @@ FROM eceasy/cli-proxy-api:latest
2
 
3
  USER root
4
 
5
- RUN apk add --no-cache bash libc6-compat gcompat jq curl
6
 
7
  WORKDIR /app
8
  RUN cp /CLIProxyAPI/CLIProxyAPI ./cli-proxy-api && chmod +x ./cli-proxy-api
@@ -12,6 +12,7 @@ COPY config.yaml /app/config.yaml
12
  COPY cleanup_tokens.sh /app/cleanup_tokens.sh
13
  COPY entrypoint.sh /app/entrypoint.sh
14
 
 
15
  RUN cp /app/config.yaml /app/config.example.yaml
16
  RUN chmod +x /app/cleanup_tokens.sh /app/entrypoint.sh
17
 
 
2
 
3
  USER root
4
 
5
+ RUN apk add --no-cache bash libc6-compat gcompat
6
 
7
  WORKDIR /app
8
  RUN cp /CLIProxyAPI/CLIProxyAPI ./cli-proxy-api && chmod +x ./cli-proxy-api
 
12
  COPY cleanup_tokens.sh /app/cleanup_tokens.sh
13
  COPY entrypoint.sh /app/entrypoint.sh
14
 
15
+ # 修复 config.example.yaml 缺失
16
  RUN cp /app/config.yaml /app/config.example.yaml
17
  RUN chmod +x /app/cleanup_tokens.sh /app/entrypoint.sh
18
 
entrypoint.sh CHANGED
@@ -1,64 +1,59 @@
1
  #!/bin/sh
2
  #
3
- # entrypoint.sh - Start crond for scheduled cleanup, then launch CLIProxyAPI
4
  #
5
- # Environment variables:
6
- # CLEANUP_CRON_SCHEDULE - Cron schedule for token cleanup (default: "0 */6 * * *", every 6h)
7
- # CLEANUP_STARTUP_DELAY - Seconds to wait after service is ready before first cleanup (default: 10)
8
  #
9
 
10
- echo "[entrypoint] Starting up at $(date)"
 
11
 
12
- CLEANUP_CRON_SCHEDULE="${CLEANUP_CRON_SCHEDULE:-0 */6 * * *}"
13
- CLEANUP_STARTUP_DELAY="${CLEANUP_STARTUP_DELAY:-10}"
14
- CRON_OK=0
15
 
16
- mkdir -p /tmp/logs /tmp/crontabs
17
 
18
- # Create a wrapper that injects env vars before running cleanup
19
- # (busybox crond does not inherit container environment)
20
- cat > /tmp/run_cleanup.sh << WRAPPER
21
  #!/bin/sh
22
  export MANAGEMENT_PASSWORD='$(printf '%s' "$MANAGEMENT_PASSWORD" | sed "s/'/'\\\\''/g")'
23
  export FEISHU_WEBHOOK_URL='$(printf '%s' "$FEISHU_WEBHOOK_URL" | sed "s/'/'\\\\''/g")'
24
  export CLEANUP_CONCURRENCY='${CLEANUP_CONCURRENCY:-20}'
25
  exec /app/cleanup_tokens.sh
26
  WRAPPER
27
- chmod +x /tmp/run_cleanup.sh
28
 
29
- cat > /tmp/crontabs/root << EOF
30
  ${CLEANUP_CRON_SCHEDULE} /tmp/run_cleanup.sh >> /tmp/logs/cleanup.log 2>&1
31
  EOF
32
 
33
- echo "[entrypoint] Cleanup schedule: ${CLEANUP_CRON_SCHEDULE}"
34
 
35
- # Try crond; if it fails, fall back to a sleep loop
36
- if crond -c /tmp/crontabs -l 8 -L /tmp/logs/crond.log 2>/dev/null; then
37
- echo "[entrypoint] crond started"
38
- CRON_OK=1
39
- else
40
- echo "[entrypoint] WARNING: crond failed, will use sleep-loop fallback"
41
- fi
42
-
43
- # Background: wait for API ready, run initial cleanup, then optionally loop
44
- (
45
- MAX_WAIT=120
46
- WAITED=0
47
- while ! wget -q -O /dev/null http://localhost:7860/ 2>/dev/null; do
48
- sleep 2
49
- WAITED=$((WAITED + 2))
50
- [ "$WAITED" -ge "$MAX_WAIT" ] && break
51
- done
52
- sleep "${CLEANUP_STARTUP_DELAY}"
53
- /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
54
 
55
- if [ "$CRON_OK" = "0" ]; then
56
- while true; do
57
- sleep 21600
58
- /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
 
59
  done
60
- fi
61
- ) &
 
 
 
 
 
 
 
 
 
62
 
63
- echo "[entrypoint] Launching cli-proxy-api..."
64
  exec /app/cli-proxy-api --config /app/config.yaml "$@"
 
1
  #!/bin/sh
2
  #
3
+ # entrypoint.sh - Launch CLIProxyAPI, optionally with scheduled token cleanup
4
  #
5
+ # Set ENABLE_CLEANUP=true to activate automatic invalid token cleanup.
6
+ # When disabled (default), behaves identically to a plain CMD ["./cli-proxy-api"].
 
7
  #
8
 
9
+ if [ "${ENABLE_CLEANUP}" = "true" ]; then
10
+ echo "[entrypoint] Cleanup enabled, starting up at $(date)"
11
 
12
+ CLEANUP_CRON_SCHEDULE="${CLEANUP_CRON_SCHEDULE:-0 */6 * * *}"
13
+ CLEANUP_STARTUP_DELAY="${CLEANUP_STARTUP_DELAY:-10}"
14
+ CRON_OK=0
15
 
16
+ mkdir -p /tmp/logs /tmp/crontabs
17
 
18
+ # Wrapper script that injects env vars (busybox crond doesn't inherit them)
19
+ cat > /tmp/run_cleanup.sh << WRAPPER
 
20
  #!/bin/sh
21
  export MANAGEMENT_PASSWORD='$(printf '%s' "$MANAGEMENT_PASSWORD" | sed "s/'/'\\\\''/g")'
22
  export FEISHU_WEBHOOK_URL='$(printf '%s' "$FEISHU_WEBHOOK_URL" | sed "s/'/'\\\\''/g")'
23
  export CLEANUP_CONCURRENCY='${CLEANUP_CONCURRENCY:-20}'
24
  exec /app/cleanup_tokens.sh
25
  WRAPPER
26
+ chmod +x /tmp/run_cleanup.sh
27
 
28
+ cat > /tmp/crontabs/root << EOF
29
  ${CLEANUP_CRON_SCHEDULE} /tmp/run_cleanup.sh >> /tmp/logs/cleanup.log 2>&1
30
  EOF
31
 
32
+ echo "[entrypoint] Cleanup schedule: ${CLEANUP_CRON_SCHEDULE}"
33
 
34
+ if crond -c /tmp/crontabs -l 8 -L /tmp/logs/crond.log 2>/dev/null; then
35
+ echo "[entrypoint] crond started"
36
+ CRON_OK=1
37
+ else
38
+ echo "[entrypoint] WARNING: crond failed, will use sleep-loop fallback"
39
+ fi
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ (
42
+ MAX_WAIT=120; WAITED=0
43
+ while ! wget -q -O /dev/null http://localhost:7860/ 2>/dev/null; do
44
+ sleep 2; WAITED=$((WAITED + 2))
45
+ [ "$WAITED" -ge "$MAX_WAIT" ] && break
46
  done
47
+ sleep "${CLEANUP_STARTUP_DELAY}"
48
+ /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
49
+
50
+ if [ "$CRON_OK" = "0" ]; then
51
+ while true; do
52
+ sleep 21600
53
+ /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
54
+ done
55
+ fi
56
+ ) &
57
+ fi
58
 
 
59
  exec /app/cli-proxy-api --config /app/config.yaml "$@"