ohmyapi commited on
Commit
64d5006
·
verified ·
1 Parent(s): ee15c51

fix: make crond non-fatal with sleep-loop fallback

Browse files
Files changed (1) hide show
  1. entrypoint.sh +19 -12
entrypoint.sh CHANGED
@@ -4,36 +4,33 @@
4
  #
5
  # Environment variables:
6
  # CLEANUP_CRON_SCHEDULE - Cron schedule for token cleanup (default: "0 */6 * * *", every 6h)
7
- # Examples:
8
- # "0 */1 * * *" every hour
9
- # "0 */6 * * *" every 6 hours (default)
10
- # "0 2 * * *" daily at 2:00 AM
11
  # CLEANUP_STARTUP_DELAY - Seconds to wait after service is ready before first cleanup (default: 10)
12
  #
13
 
14
- set -e
15
-
16
  echo "[entrypoint] Starting up at $(date)"
17
 
18
  CLEANUP_CRON_SCHEDULE="${CLEANUP_CRON_SCHEDULE:-0 */6 * * *}"
19
  CLEANUP_STARTUP_DELAY="${CLEANUP_STARTUP_DELAY:-10}"
 
20
 
21
  mkdir -p /tmp/logs /tmp/crontabs
22
 
23
  # Write crontab to /tmp (HuggingFace root filesystem is read-only)
24
  cat > /tmp/crontabs/root << EOF
25
- # CLIProxyAPI invalid token cleanup
26
  ${CLEANUP_CRON_SCHEDULE} /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
27
  EOF
28
 
29
  echo "[entrypoint] Cleanup schedule: ${CLEANUP_CRON_SCHEDULE}"
30
 
31
- # Start crond (busybox) with custom crontab directory
32
- crond -c /tmp/crontabs -l 8 -L /tmp/logs/crond.log
33
- echo "[entrypoint] crond started"
 
 
 
 
34
 
35
- # Wait for the API to be ready, then run the initial cleanup in background.
36
- # This runs in a subshell so it doesn't block the main service from starting.
37
  (
38
  echo "[entrypoint] Waiting for API to be ready (polling localhost:7860)..."
39
  MAX_WAIT=120
@@ -50,6 +47,16 @@ echo "[entrypoint] crond started"
50
  sleep "$CLEANUP_STARTUP_DELAY"
51
  echo "[entrypoint] Running initial token cleanup..."
52
  /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
 
 
 
 
 
 
 
 
 
 
53
  ) &
54
 
55
  echo "[entrypoint] Launching cli-proxy-api..."
 
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
  # Write crontab to /tmp (HuggingFace root filesystem is read-only)
19
  cat > /tmp/crontabs/root << EOF
 
20
  ${CLEANUP_CRON_SCHEDULE} /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
21
  EOF
22
 
23
  echo "[entrypoint] Cleanup schedule: ${CLEANUP_CRON_SCHEDULE}"
24
 
25
+ # Try crond; if it fails (e.g. permission issues), fall back to a sleep loop
26
+ if crond -c /tmp/crontabs -l 8 -L /tmp/logs/crond.log 2>/dev/null; then
27
+ echo "[entrypoint] crond started"
28
+ CRON_OK=1
29
+ else
30
+ echo "[entrypoint] WARNING: crond failed, will use sleep-loop fallback for scheduled cleanup"
31
+ fi
32
 
33
+ # Background: wait for API ready, run initial cleanup, then optionally loop
 
34
  (
35
  echo "[entrypoint] Waiting for API to be ready (polling localhost:7860)..."
36
  MAX_WAIT=120
 
47
  sleep "$CLEANUP_STARTUP_DELAY"
48
  echo "[entrypoint] Running initial token cleanup..."
49
  /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
50
+
51
+ # If crond failed, run cleanup in a sleep loop (every 6 hours = 21600s)
52
+ if [ "$CRON_OK" = "0" ]; then
53
+ echo "[entrypoint] Starting sleep-loop cleanup (every 6h)..."
54
+ while true; do
55
+ sleep 21600
56
+ echo "[cleanup-loop] Running scheduled cleanup at $(date)"
57
+ /app/cleanup_tokens.sh >> /tmp/logs/cleanup.log 2>&1
58
+ done
59
+ fi
60
  ) &
61
 
62
  echo "[entrypoint] Launching cli-proxy-api..."