Skip to content

Commit b8225b2

Browse files
committed
manually clean cached profiles
1 parent 6d90b83 commit b8225b2

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed

Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,17 @@ RUN chown nextjs:nodejs .next
4747
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
4848
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
4949

50+
# Copy and set up the profile cache cleaner script
51+
COPY --chown=nextjs:nodejs lib/shell/profile-cache-cleaner.sh /app/lib/shell/profile-cache-cleaner.sh
52+
RUN chmod +x /app/lib/shell/profile-cache-cleaner.sh
53+
5054
USER nextjs
5155

5256
EXPOSE 3000
5357

5458
ENV PORT=3000
5559
ENV HOSTNAME=0.0.0.0
5660

57-
CMD ["node", "--max-old-space-size=1800", "server.js"]
61+
# Start both the cache cleaner (in background) and the node server (in foreground)
62+
# Background process output will appear in docker logs
63+
CMD ["sh", "-c", "/app/lib/shell/profile-cache-cleaner.sh 2>&1 & exec node server.js"]

guides/DEBUG_CACHE_CLEANER.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Debugging Profile Cache Cleaner
2+
3+
## How to Verify It's Running
4+
5+
### 1. Check if the process is running inside the container
6+
7+
SSH to your server and run:
8+
9+
```bash
10+
docker exec gitranks-ui ps aux | grep profile-cache-cleaner
11+
```
12+
13+
You should see the script process running.
14+
15+
### 2. View Docker Logs
16+
17+
The script logs to stdout/stderr, which Docker captures. View logs with:
18+
19+
```bash
20+
# View all logs
21+
docker logs gitranks-ui
22+
23+
# Follow logs in real-time
24+
docker logs -f gitranks-ui
25+
26+
# View only the last 100 lines
27+
docker logs --tail 100 gitranks-ui
28+
29+
# Filter logs for cache cleaner messages
30+
docker logs gitranks-ui 2>&1 | grep "profile-cache-cleaner"
31+
```
32+
33+
### 3. Verify It Runs Every 10 Minutes
34+
35+
The script logs every time it runs (every 600 seconds). Check timestamps:
36+
37+
```bash
38+
docker logs gitranks-ui 2>&1 | grep "profile-cache-cleaner" | tail -20
39+
```
40+
41+
You should see log entries approximately every 10 minutes with timestamps like:
42+
```
43+
Mon Jan 20 10:00:00 UTC 2025 [profile-cache-cleaner] Found 15000 profile dirs
44+
Mon Jan 20 10:10:00 UTC 2025 [profile-cache-cleaner] Found 15000 profile dirs
45+
Mon Jan 20 10:20:00 UTC 2025 [profile-cache-cleaner] Found 15000 profile dirs
46+
```
47+
48+
### 4. Check Script Execution Directly
49+
50+
Execute the script manually inside the container to test:
51+
52+
```bash
53+
docker exec gitranks-ui /app/lib/shell/profile-cache-cleaner.sh
54+
```
55+
56+
(Note: This will run in foreground - press Ctrl+C to stop)
57+
58+
### 5. Verify the Script File Exists and is Executable
59+
60+
```bash
61+
docker exec gitranks-ui ls -la /app/lib/shell/profile-cache-cleaner.sh
62+
docker exec gitranks-ui test -x /app/lib/shell/profile-cache-cleaner.sh && echo "Executable" || echo "Not executable"
63+
```
64+
65+
### 6. Check if Profile Directory Exists
66+
67+
```bash
68+
docker exec gitranks-ui ls -la /app/.next/server/app/profile
69+
docker exec gitranks-ui ls -d /app/.next/server/app/profile/*/ 2>/dev/null | wc -l
70+
```
71+
72+
## Expected Log Output
73+
74+
When working correctly, you should see logs like:
75+
76+
```
77+
Starting profile cache cleaner. Dir: /app/.next/server/app/profile, max dirs: 30000, interval: 600s
78+
Mon Jan 20 10:00:00 UTC 2025 [profile-cache-cleaner] Found 15000 profile dirs
79+
Mon Jan 20 10:10:00 UTC 2025 [profile-cache-cleaner] Found 15200 profile dirs
80+
Mon Jan 20 10:20:00 UTC 2025 [profile-cache-cleaner] Found 15100 profile dirs
81+
```
82+
83+
If the directory doesn't exist yet:
84+
```
85+
Mon Jan 20 10:00:00 UTC 2025 [profile-cache-cleaner] Directory /app/.next/server/app/profile does not exist
86+
```
87+
88+
If cleanup is needed:
89+
```
90+
Mon Jan 20 10:00:00 UTC 2025 [profile-cache-cleaner] Found 35000 profile dirs
91+
Mon Jan 20 10:00:00 UTC 2025 [profile-cache-cleaner] Need to remove 5000 oldest dirs
92+
removing /app/.next/server/app/profile/abc123/
93+
removing /app/.next/server/app/profile/def456/
94+
...
95+
```
96+
97+
## Troubleshooting
98+
99+
### Script not running?
100+
1. Check container logs: `docker logs gitranks-ui`
101+
2. Check if process exists: `docker exec gitranks-ui ps aux | grep profile`
102+
3. Verify script permissions: `docker exec gitranks-ui ls -la /app/lib/shell/profile-cache-cleaner.sh`
103+
104+
### No logs appearing?
105+
1. The script might be failing silently - check with: `docker exec gitranks-ui sh -c "/app/lib/shell/profile-cache-cleaner.sh"` (run manually)
106+
2. Check if the script has proper shebang: `docker exec gitranks-ui head -1 /app/lib/shell/profile-cache-cleaner.sh`
107+
108+
### Script runs but no cleanup happens?
109+
1. Check if directory exists: `docker exec gitranks-ui test -d /app/.next/server/app/profile && echo "Exists" || echo "Missing"`
110+
2. Check current count: `docker exec gitranks-ui ls -d /app/.next/server/app/profile/*/ 2>/dev/null | wc -l`
111+
3. Verify MAX_DIRS threshold (currently 30000) - if count is below this, no cleanup will occur
112+

lib/shell/profile-cache-cleaner.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
PROFILE_DIR="/app/.next/server/app/profile"
4+
MAX_DIRS=30000 # change as needed
5+
INTERVAL=600 # 10 minutes
6+
7+
echo "Starting profile cache cleaner. Dir: $PROFILE_DIR, max dirs: $MAX_DIRS, interval: ${INTERVAL}s"
8+
9+
while true; do
10+
if [ ! -d "$PROFILE_DIR" ]; then
11+
echo "$(date) [profile-cache-cleaner] Directory $PROFILE_DIR does not exist"
12+
else
13+
COUNT=$(ls -d "$PROFILE_DIR"/*/ 2>/dev/null | wc -l)
14+
echo "$(date) [profile-cache-cleaner] Found $COUNT profile dirs"
15+
16+
if [ "$COUNT" -gt "$MAX_DIRS" ]; then
17+
EXTRA=$((COUNT - MAX_DIRS))
18+
echo "$(date) [profile-cache-cleaner] Need to remove $EXTRA oldest dirs"
19+
20+
ls -dt "$PROFILE_DIR"/*/ 2>/dev/null | tail -n "$EXTRA" | while read DIR; do
21+
if [ -n "$DIR" ]; then
22+
echo " removing $DIR"
23+
rm -rf "$DIR"
24+
fi
25+
done
26+
fi
27+
fi
28+
29+
sleep "$INTERVAL"
30+
done

0 commit comments

Comments
 (0)