meltedchoco commited on
Commit
c0881b0
Β·
verified Β·
1 Parent(s): 909accc

Update entrypoint.sh

Browse files
Files changed (1) hide show
  1. entrypoint.sh +11 -10
entrypoint.sh CHANGED
@@ -2,13 +2,10 @@
2
  set -euo pipefail
3
 
4
  # 1. Configuration
5
- # Live database lives in the fast, ephemeral container storage
6
  PGDATA="/tmp/postgres"
7
  PGHOST="/tmp"
8
  PG_PORT="5432"
9
  SEKAI_DB_URL="postgresql://sekai:sekai@/sekai?host=${PGHOST}&port=${PG_PORT}"
10
-
11
- # Our Hugging Face bucket is now safely used just for backups
12
  BACKUP_DIR="/data/db_backups"
13
  BACKUP_FILE="${BACKUP_DIR}/sekai_backup.sql"
14
 
@@ -40,21 +37,25 @@ else
40
  echo "No existing backup found. Starting fresh."
41
  fi
42
 
43
- # 6. Start Background Auto-Backup Process
44
  (
45
  while true; do
46
- sleep 300 # Wait 5 minutes
47
-
48
- # Dump to a temporary file first, then move it.
49
- # This prevents the backup from corrupting if the container crashes mid-save.
50
  pg_dump -h "${PGHOST}" -p "${PG_PORT}" -U sekai -d sekai --clean --if-exists > "${BACKUP_FILE}.tmp"
51
  mv "${BACKUP_FILE}.tmp" "${BACKUP_FILE}"
52
  echo "[Backup] Database automatically saved to persistent storage."
53
  done
54
  ) &
55
 
 
 
 
 
56
  export SEKAI_DATABASE_URL="${SEKAI_DB_URL}"
57
  echo "PostgreSQL ready at ${PGHOST}:${PG_PORT}"
58
 
59
- # 7. Start the app
60
- exec uv run python main.py
 
 
 
 
2
  set -euo pipefail
3
 
4
  # 1. Configuration
 
5
  PGDATA="/tmp/postgres"
6
  PGHOST="/tmp"
7
  PG_PORT="5432"
8
  SEKAI_DB_URL="postgresql://sekai:sekai@/sekai?host=${PGHOST}&port=${PG_PORT}"
 
 
9
  BACKUP_DIR="/data/db_backups"
10
  BACKUP_FILE="${BACKUP_DIR}/sekai_backup.sql"
11
 
 
37
  echo "No existing backup found. Starting fresh."
38
  fi
39
 
40
+ # 6. Start Background Auto-Backup Process (Every 5 mins)
41
  (
42
  while true; do
43
+ sleep 300
 
 
 
44
  pg_dump -h "${PGHOST}" -p "${PG_PORT}" -U sekai -d sekai --clean --if-exists > "${BACKUP_FILE}.tmp"
45
  mv "${BACKUP_FILE}.tmp" "${BACKUP_FILE}"
46
  echo "[Backup] Database automatically saved to persistent storage."
47
  done
48
  ) &
49
 
50
+ # ── NEW: Graceful Shutdown Trap ──────────────────────────────────────
51
+ # If Hugging Face restarts or puts the Space to sleep, force a backup instantly.
52
+ trap 'echo "Container stopping! Forcing a final database backup..."; pg_dump -h "${PGHOST}" -p "${PG_PORT}" -U sekai -d sekai --clean --if-exists > "${BACKUP_FILE}.tmp"; mv "${BACKUP_FILE}.tmp" "${BACKUP_FILE}"; echo "Final backup saved! Goodbye."; exit 0' SIGTERM SIGINT
53
+
54
  export SEKAI_DATABASE_URL="${SEKAI_DB_URL}"
55
  echo "PostgreSQL ready at ${PGHOST}:${PG_PORT}"
56
 
57
+ # 7. Start the app (Removed 'exec' so the trap works)
58
+ uv run python main.py &
59
+
60
+ # Wait for the python app to finish or receive a kill signal
61
+ wait $!