jerecom commited on
Commit
5a1ce7b
·
verified ·
1 Parent(s): f286931

Update entrypoint.sh

Browse files
Files changed (1) hide show
  1. entrypoint.sh +75 -18
entrypoint.sh CHANGED
@@ -43,19 +43,71 @@ if [ ! -d "/home/user" ]; then
43
  fi
44
 
45
  # ============================================
46
- # STEP 2: START OPENCODE
47
  # ============================================
48
- echo '=== [STEP 2] STARTING OPENCODE ==='
49
- export OPENCODE_SERVER_USERNAME=${OPENCODE_SERVER_USERNAME}
50
- export OPENCODE_SERVER_PASSWORD=${OPENCODE_SERVER_PASSWORD}
51
- opencode web --port 7860 --hostname 0.0.0.0 > /tmp/opencode.log 2>&1 &
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  sleep 10
53
- echo '=== [STEP 2] OPENCODE STARTED ==='
 
 
54
 
55
  # ============================================
56
- # STEP 3: INOTIFY-BASED SMART SYNC
57
  # ============================================
58
- echo '=== [STEP 3] STARTING SMART SYNC (inotifywait) ==='
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  while true; do
60
  # Check if OpenCode is still running
61
  if ! pgrep -f 'opencode' > /dev/null; then
@@ -63,17 +115,22 @@ while true; do
63
  exit 1
64
  fi
65
 
66
- # Wait for any file change in /home (blocks until change detected)
67
- # Excludes .mdb files, .cache folder, .npm folder from triggering sync
68
- inotifywait -r -e modify,create,delete,move \
69
  --exclude '.*\.mdb$' \
70
  --exclude '.*\.log$' \
71
- --exclude '.*/\.cache(/.*)?$' \
72
- --exclude '.*/\.npm(/.*)?$' \
73
  -q "$SOURCE"
74
 
75
- # Change detected! Sync to bucket (excluding .mdb, .cache, .npm)
76
- echo "=== [STEP 3] Change detected! Syncing to bucket... ==="
77
- hf sync "$SOURCE" "$BUCKET" --delete --exclude "*.mdb" --exclude "*.log"
78
- echo "=== [STEP 3] Sync done! Waiting for next change... ==="
79
- done
 
 
 
 
 
 
 
43
  fi
44
 
45
  # ============================================
46
+ # STEP 2: START OPENCODE (with RAM watchdog)
47
  # ============================================
48
+
49
+ # RAM limit: 14GB in KB
50
+ RAM_LIMIT_KB=$((14 * 1024 * 1024))
51
+
52
+ start_opencode() {
53
+ echo '=== [STEP 2] STARTING OPENCODE ==='
54
+ export OPENCODE_SERVER_USERNAME=${OPENCODE_SERVER_USERNAME}
55
+ export OPENCODE_SERVER_PASSWORD=${OPENCODE_SERVER_PASSWORD}
56
+ opencode web --port 7860 --hostname 0.0.0.0 > /tmp/opencode.log 2>&1 &
57
+ OPENCODE_PID=$!
58
+ echo "=== [STEP 2] OPENCODE STARTED (PID: $OPENCODE_PID) ==="
59
+ }
60
+
61
+ ram_watchdog() {
62
+ while true; do
63
+ if [ -n "$OPENCODE_PID" ] && kill -0 "$OPENCODE_PID" 2>/dev/null; then
64
+ # OpenCode + uske saare child processes ki total RAM (KB)
65
+ USED_KB=$(ps --no-headers -o rss --ppid "$OPENCODE_PID" 2>/dev/null | awk '{sum+=$1} END {print sum+0}')
66
+ SELF_KB=$(ps --no-headers -o rss -p "$OPENCODE_PID" 2>/dev/null | awk '{print $1+0}')
67
+ TOTAL_KB=$((USED_KB + SELF_KB))
68
+
69
+ echo "=== [RAM] OpenCode using: $((TOTAL_KB / 1024))MB / 14336MB ==="
70
+
71
+ if [ "$TOTAL_KB" -ge "$RAM_LIMIT_KB" ]; then
72
+ echo "=== [RAM] LIMIT EXCEEDED! Killing OpenCode (${TOTAL_KB}KB >= ${RAM_LIMIT_KB}KB) ==="
73
+ kill -9 "$OPENCODE_PID" 2>/dev/null
74
+ sleep 3
75
+ echo '=== [RAM] Restarting OpenCode... ==='
76
+ start_opencode
77
+ fi
78
+ else
79
+ # OpenCode kisi aur wajah se mar gaya — restart
80
+ echo '=== [RAM] OpenCode not running! Restarting... ==='
81
+ start_opencode
82
+ fi
83
+ done
84
+ }
85
+
86
+ start_opencode
87
  sleep 10
88
+
89
+ # RAM watchdog background mein chalao
90
+ ram_watchdog &
91
 
92
  # ============================================
93
+ # STEP 3: INOTIFY TRIGGER + 30s DEBOUNCE SYNC
94
  # ============================================
95
+ echo '=== [STEP 3] STARTING SMART SYNC (inotify + 30s debounce) ==='
96
+
97
+ do_sync() {
98
+ echo "=== [STEP 3] Syncing to bucket... ==="
99
+ hf sync "$SOURCE" "$BUCKET" --delete \
100
+ --exclude "*.mdb" \
101
+ --exclude "*.log" \
102
+ --exclude ".cache" \
103
+ --exclude ".npm" \
104
+ --exclude ".check_for_update_done" \
105
+ --exclude "rg"
106
+ echo "=== [STEP 3] Sync done! ==="
107
+ }
108
+
109
+ LAST_SYNC=0
110
+
111
  while true; do
112
  # Check if OpenCode is still running
113
  if ! pgrep -f 'opencode' > /dev/null; then
 
115
  exit 1
116
  fi
117
 
118
+ # Wait for ANY change modify, create, delete, move, rename, attrib
119
+ inotifywait -r -e modify,create,delete,move,attrib \
 
120
  --exclude '.*\.mdb$' \
121
  --exclude '.*\.log$' \
122
+ --exclude '.*\.check_for_update_done$' \
123
+ --exclude '.*/rg$' \
124
  -q "$SOURCE"
125
 
126
+ # Debounce: agar last sync 30s pehle hua ho tabhi sync karo
127
+ NOW=$(date +%s)
128
+ DIFF=$((NOW - LAST_SYNC))
129
+
130
+ if [ "$DIFF" -ge 30 ]; then
131
+ do_sync
132
+ LAST_SYNC=$(date +%s)
133
+ else
134
+ echo "=== [STEP 3] Change detected but debounce active (${DIFF}s < 30s), waiting... ==="
135
+ fi
136
+ done