feat: auto cleanup orphan files

This commit is contained in:
LoiTra 2025-07-15 11:44:47 +07:00 committed by loitragg
parent f440e9bfbe
commit e15a864301
No known key found for this signature in database
GPG key ID: 96292BAF3E28CFF5
3 changed files with 59 additions and 3 deletions

View file

@ -39,7 +39,7 @@ RUN npm ci --force
COPY . . COPY . .
ENV APP_BUILD_HASH=${BUILD_HASH} ENV APP_BUILD_HASH=${BUILD_HASH}
RUN npm run build RUN NODE_OPTIONS="--max_old_space_size=4096" npm run build
######## WebUI backend ######## ######## WebUI backend ########
FROM python:3.11-slim-bookworm AS base FROM python:3.11-slim-bookworm AS base
@ -116,10 +116,9 @@ RUN echo -n 00000000-0000-0000-0000-000000000000 > $HOME/.cache/chroma/telemetry
# Make sure the user has access to the app and root directory # Make sure the user has access to the app and root directory
RUN chown -R $UID:$GID /app $HOME RUN chown -R $UID:$GID /app $HOME
# Install common system dependencies
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
git build-essential pandoc gcc netcat-openbsd curl jq \ git build-essential pandoc gcc netcat-openbsd curl jq cron \
python3-dev \ python3-dev \
ffmpeg libsm6 libxext6 \ ffmpeg libsm6 libxext6 \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
@ -167,6 +166,12 @@ COPY --chown=$UID:$GID --from=build /app/package.json /app/package.json
# copy backend files # copy backend files
COPY --chown=$UID:$GID ./backend . COPY --chown=$UID:$GID ./backend .
# Create directories for logs
RUN mkdir -p /var/log && \
touch /var/log/clean-orphaned-files.log && \
chmod 644 /var/log/clean-orphaned-files.log && \
chown $UID:$GID /var/log/clean-orphaned-files.log
EXPOSE 8080 EXPOSE 8080
HEALTHCHECK CMD curl --silent --fail http://localhost:${PORT:-8080}/health | jq -ne 'input.status == true' || exit 1 HEALTHCHECK CMD curl --silent --fail http://localhost:${PORT:-8080}/health | jq -ne 'input.status == true' || exit 1

30
backend/setup-cron.sh Normal file
View file

@ -0,0 +1,30 @@
#!/bin/bash
# Create the wrapper script
cat > /app/backend/run-cleanup-script.sh << 'EOL'
#!/bin/bash
# This script runs the clean-orphaned-files.py script with the proper environment variables
# Export all environment variables to the script
export DATABASE_URL="${DATABASE_URL}"
export ADMIN_TOKEN="${ADMIN_TOKEN:-your_admin_token}"
export TEAMS_WEBHOOK_URL="${TEAMS_WEBHOOK_URL:-your_ms_teams_incoming_webhook_url}"
# Check if the script exists (it should be in a mounted volume)
if [ -f /app/backend/data/scripts/clean-orphaned-files.py ]; then
/usr/local/bin/python /app/backend/data/scripts/clean-orphaned-files.py --admin-token "${ADMIN_TOKEN}" --teams-webhook-url "${TEAMS_WEBHOOK_URL}" >> /var/log/clean-orphaned-files.log 2>&1
else
echo "$(date): Warning - clean-orphaned-files.py not found in mounted volume" >> /var/log/clean-orphaned-files.log
fi
EOL
# Make the wrapper script executable
chmod +x /app/backend/run-cleanup-script.sh
# Create the cron job entry
CRONJOB="0 */6 * * * /app/backend/run-cleanup-script.sh"
# Setup the crontab
(crontab -l 2>/dev/null || echo "") | grep -v "run-cleanup-script.sh" | { cat; echo "$CRONJOB"; } | crontab -
echo "Cron job has been set up successfully."

View file

@ -35,6 +35,27 @@ if test "$WEBUI_SECRET_KEY $WEBUI_JWT_SECRET_KEY" = " "; then
WEBUI_SECRET_KEY=$(cat "$KEY_FILE") WEBUI_SECRET_KEY=$(cat "$KEY_FILE")
fi fi
# Setup and start cron service
echo "Setting up cron service..."
if [ -x "$(command -v cron)" ]; then
# Create necessary directories for scripts if they don't exist
mkdir -p /app/backend/data/scripts
# Make the setup-cron.sh script executable
chmod +x "$SCRIPT_DIR/setup-cron.sh"
# Run the cron setup script
"$SCRIPT_DIR/setup-cron.sh"
# Start the cron service
echo "Starting cron service..."
service cron start || cron
echo "Cron service started."
else
echo "Warning: cron is not installed. Scheduled tasks will not run."
fi
if [[ "${USE_OLLAMA_DOCKER,,}" == "true" ]]; then if [[ "${USE_OLLAMA_DOCKER,,}" == "true" ]]; then
echo "USE_OLLAMA is set to true, starting ollama serve." echo "USE_OLLAMA is set to true, starting ollama serve."
ollama serve & ollama serve &