github-actions[bot] commited on
Commit
1794eb9
·
1 Parent(s): bbfd266

chore(hf): deploy image mode @ 5e5ad75556b61a2c4a8fcf344d83bfe7760f2b42 (v3.1.6)

Browse files
Files changed (3) hide show
  1. .hf-sync +4 -4
  2. Dockerfile +1 -1
  3. sqlite-preflight.sh +93 -26
.hf-sync CHANGED
@@ -1,11 +1,11 @@
1
  adapter_repo=Viciy2023/grok2api
2
- adapter_commit=fca9405d97948810f29a29a4710040d66236e53e
3
- adapter_hash=a4e010317e25fb9e0e181ea9ddbdd8c4e8f98636a8984cfdfc28ae4392f06ef8
4
  build_mode=image
5
  upstream_repo=chenyme/grok2api
6
  upstream_ref=5e5ad75556b61a2c4a8fcf344d83bfe7760f2b42
7
  upstream_version=v3.1.6
8
  image_ref=ghcr.io/chenyme/grok2api@sha256:adb62afc79a0c31844d3af8d9e73b0eb034dedddca60f3f6d95b605c734d7a4f
9
  image_digest=sha256:adb62afc79a0c31844d3af8d9e73b0eb034dedddca60f3f6d95b605c734d7a4f
10
- deployed_at=2026-09-23T06:38:09Z
11
- deployed_by=https://github.com/Viciy2023/grok2api/actions/runs/35827654697
 
1
  adapter_repo=Viciy2023/grok2api
2
+ adapter_commit=b96a06a033647a6f85cc32adddf40e77be67ebdf
3
+ adapter_hash=b6eb9fc32d8a12e551db01a96e1ec0d5850b091ee0ae565fc5f1306f3ecc6cd9
4
  build_mode=image
5
  upstream_repo=chenyme/grok2api
6
  upstream_ref=5e5ad75556b61a2c4a8fcf344d83bfe7760f2b42
7
  upstream_version=v3.1.6
8
  image_ref=ghcr.io/chenyme/grok2api@sha256:adb62afc79a0c31844d3af8d9e73b0eb034dedddca60f3f6d95b605c734d7a4f
9
  image_digest=sha256:adb62afc79a0c31844d3af8d9e73b0eb034dedddca60f3f6d95b605c734d7a4f
10
+ deployed_at=2026-09-23T06:47:10Z
11
+ deployed_by=https://github.com/Viciy2023/grok2api/actions/runs/35828373128
Dockerfile CHANGED
@@ -26,7 +26,7 @@ FROM ${GROK2API_IMAGE}
26
  ARG GROK2API_IMAGE
27
  ARG GROK2API_CONFIG_REF=5e5ad75556b61a2c4a8fcf344d83bfe7760f2b42
28
  ARG GROK2API_VERSION=v3.1.6
29
- ARG GROK2API_ADAPTER_HASH=a4e010317e25fb9e0e181ea9ddbdd8c4e8f98636a8984cfdfc28ae4392f06ef8
30
 
31
  ENV TZ=Asia/Shanghai \
32
  SERVER_PORT=7860 \
 
26
  ARG GROK2API_IMAGE
27
  ARG GROK2API_CONFIG_REF=5e5ad75556b61a2c4a8fcf344d83bfe7760f2b42
28
  ARG GROK2API_VERSION=v3.1.6
29
+ ARG GROK2API_ADAPTER_HASH=b6eb9fc32d8a12e551db01a96e1ec0d5850b091ee0ae565fc5f1306f3ecc6cd9
30
 
31
  ENV TZ=Asia/Shanghai \
32
  SERVER_PORT=7860 \
sqlite-preflight.sh CHANGED
@@ -2,12 +2,12 @@
2
  #
3
  # Self-healing guard for the SQLite database on the Hugging Face bucket volume.
4
  #
5
- # /data lives on a network-backed bucket volume. That is fine for whole files,
6
- # but a container stopped in the middle of a SQLite write - which is exactly
7
- # what a Space rebuild does - can leave a torn page behind. Afterwards every
8
- # query touching the damaged table fails with "database disk image is
9
- # malformed": the service still starts, /healthz still answers, but the admin UI
10
- # can no longer list accounts.
11
  #
12
  # Runs on every boot, before the application starts:
13
  # 1. keeps a consistent rotating snapshot (sqlite3 .backup folds in the WAL)
@@ -15,15 +15,65 @@
15
  # 3. when verification fails, rebuilds the database with sqlite3 .recover and
16
  # only accepts the result if it verifies AND did not lose accounts;
17
  # otherwise it falls back to the newest snapshot that verifies
 
 
 
18
  #
19
- # The original file is never deleted - a failed repair leaves the database
20
- # exactly as it was, and every replaced database is kept for inspection.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  sqlite_preflight() {
23
  db_dir="${1:-/data}"
24
  db="$db_dir/backend.db"
25
 
26
- [ -f "$db" ] || return 0
27
  if ! command -v sqlite3 >/dev/null 2>&1; then
28
  echo "sqlite3 not installed; skipping database preflight" >&2
29
  return 0
@@ -32,6 +82,26 @@ sqlite_preflight() {
32
  backup_dir="$db_dir/backups"
33
  mkdir -p "$backup_dir" 2>/dev/null || true
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # ---- 1. rotating pre-boot snapshot ------------------------------------- #
36
  # Slot 1 is always "the database as it was found"; older slots hold previous
37
  # boots, so if slot 1 is itself damaged the fallback still has a candidate.
@@ -88,8 +158,11 @@ sqlite_preflight() {
88
  # The application opens the database with journal_mode(WAL); keep that.
89
  sqlite3 "$recovered" "PRAGMA journal_mode=WAL;" >/dev/null 2>&1 || true
90
  mv "$db" "$broken"
91
- rm -f "$db-wal" "$db-shm"
92
- mv "$recovered" "$db"
 
 
 
93
  echo "database rebuilt from recovered pages; accounts=$new_accounts (damaged copy kept at $broken)" >&2
94
  return 0
95
  fi
@@ -97,22 +170,16 @@ sqlite_preflight() {
97
  rm -f "$recovered"
98
 
99
  # ---- 3b. otherwise restore the newest snapshot that verifies ----------- #
100
- i=1
101
- while [ "$i" -le 4 ]; do
102
- snapshot="$backup_dir/backend.db.$i"
103
- if [ -f "$snapshot" ]; then
104
- snapshot_check="$(timeout 300 sqlite3 "$snapshot" 'PRAGMA integrity_check;' 2>/dev/null | head -n 1)"
105
- if [ "$snapshot_check" = "ok" ]; then
106
- snapshot_accounts="$(timeout 60 sqlite3 "$snapshot" 'SELECT COUNT(*) FROM provider_accounts;' 2>/dev/null | head -n 1)"
107
- mv "$db" "$broken"
108
- rm -f "$db-wal" "$db-shm"
109
- cp "$snapshot" "$db"
110
- echo "database restored from snapshot $snapshot (accounts=$snapshot_accounts; damaged copy kept at $broken)" >&2
111
- return 0
112
- fi
113
  fi
114
- i=$((i + 1))
115
- done
 
 
116
 
117
  echo "ERROR: automatic recovery failed and no valid snapshot exists; leaving the database untouched" >&2
118
  return 0
 
2
  #
3
  # Self-healing guard for the SQLite database on the Hugging Face bucket volume.
4
  #
5
+ # /data lives on a bucket volume. That is fine for whole files, but a container
6
+ # stopped in the middle of a SQLite write - which is exactly what a Space
7
+ # rebuild does - can leave a torn page behind. Afterwards every query touching
8
+ # the damaged table fails with "database disk image is malformed": the service
9
+ # still starts, /healthz still answers, but the admin UI can no longer list
10
+ # accounts.
11
  #
12
  # Runs on every boot, before the application starts:
13
  # 1. keeps a consistent rotating snapshot (sqlite3 .backup folds in the WAL)
 
15
  # 3. when verification fails, rebuilds the database with sqlite3 .recover and
16
  # only accepts the result if it verifies AND did not lose accounts;
17
  # otherwise it falls back to the newest snapshot that verifies
18
+ # 4. when the database file is missing outright - the volume can drop it while
19
+ # its WAL survives - restores/recover a snapshot instead of letting the
20
+ # application create an empty database and silently lose every account
21
  #
22
+ # The original file is never deleted: a failed repair leaves the database
23
+ # exactly as it was, and every replaced database is kept for inspection. A
24
+ # repaired database is additionally copied to backups/ under a fresh name, so a
25
+ # good copy exists independently of the swap in step 3.
26
+
27
+ # Install $1 as the live database $2. A plain byte copy, because the rename a
28
+ # previous repair used is what left the new file unsynced on the volume.
29
+ install_database() {
30
+ [ -f "$1" ] || return 1
31
+ rm -f "$2-wal" "$2-shm"
32
+ cp "$1" "$2"
33
+ }
34
+
35
+ # Pick a usable database from the snapshot directory. Sets CHOSEN_SNAPSHOT (a
36
+ # global, never stdout: the sqlite3 CLI prints banner text such as
37
+ # "defensive off" around .recover, which silently corrupts a command
38
+ # substitution and once made this guard install an empty database).
39
+ # A snapshot that does not verify is repaired on the spot, because the damaged
40
+ # copies are exactly the ones that still hold every account row.
41
+ choose_snapshot() {
42
+ _dir="$1"
43
+ _want="$2"
44
+ CHOSEN_SNAPSHOT=""
45
+ for _candidate in $(ls -1t "$_dir"/backend.db.* 2>/dev/null); do
46
+ case "$_candidate" in
47
+ *.tmp) continue ;;
48
+ esac
49
+ [ -f "$_candidate" ] || continue
50
+ if [ "$(timeout 300 sqlite3 "$_candidate" 'PRAGMA integrity_check;' 2>/dev/null | head -n 1)" = "ok" ]; then
51
+ CHOSEN_SNAPSHOT="$_candidate"
52
+ return 0
53
+ fi
54
+ _repaired="$_candidate.recovered"
55
+ rm -f "$_repaired"
56
+ if sqlite3 "$_candidate" .recover 2>/dev/null | sqlite3 "$_repaired" >/dev/null 2>&1; then
57
+ if [ -s "$_repaired" ] && [ "$(timeout 300 sqlite3 "$_repaired" 'PRAGMA integrity_check;' 2>/dev/null | head -n 1)" = "ok" ]; then
58
+ _accounts="$(timeout 60 sqlite3 "$_repaired" 'SELECT COUNT(*) FROM provider_accounts;' 2>/dev/null | head -n 1)"
59
+ case "$_accounts" in
60
+ ''|*[!0-9]*) _accounts="" ;;
61
+ esac
62
+ if [ -z "$_want" ] || [ -z "$_accounts" ] || [ "$_accounts" -ge "$_want" ]; then
63
+ CHOSEN_SNAPSHOT="$_repaired"
64
+ return 0
65
+ fi
66
+ fi
67
+ fi
68
+ rm -f "$_repaired"
69
+ done
70
+ return 1
71
+ }
72
 
73
  sqlite_preflight() {
74
  db_dir="${1:-/data}"
75
  db="$db_dir/backend.db"
76
 
 
77
  if ! command -v sqlite3 >/dev/null 2>&1; then
78
  echo "sqlite3 not installed; skipping database preflight" >&2
79
  return 0
 
82
  backup_dir="$db_dir/backups"
83
  mkdir -p "$backup_dir" 2>/dev/null || true
84
 
85
+ # ---- 0. the volume dropped the database but kept its WAL ---------------- #
86
+ if [ ! -f "$db" ]; then
87
+ if [ ! -f "$db-wal" ]; then
88
+ echo "$db not present yet; nothing to verify"
89
+ return 0
90
+ fi
91
+ echo "WARN: $db is missing while a WAL survives; the bucket volume lost the main file" >&2
92
+ if ! choose_snapshot "$backup_dir" "" || [ -z "$CHOSEN_SNAPSHOT" ]; then
93
+ echo "ERROR: no usable snapshot to restore after the main database went missing" >&2
94
+ return 0
95
+ fi
96
+ if install_database "$CHOSEN_SNAPSHOT" "$db"; then
97
+ sqlite3 "$db" "PRAGMA journal_mode=WAL;" >/dev/null 2>&1 || true
98
+ echo "database restored from $CHOSEN_SNAPSHOT after the volume lost the main file" >&2
99
+ else
100
+ echo "ERROR: could not restore $CHOSEN_SNAPSHOT to $db" >&2
101
+ fi
102
+ return 0
103
+ fi
104
+
105
  # ---- 1. rotating pre-boot snapshot ------------------------------------- #
106
  # Slot 1 is always "the database as it was found"; older slots hold previous
107
  # boots, so if slot 1 is itself damaged the fallback still has a candidate.
 
158
  # The application opens the database with journal_mode(WAL); keep that.
159
  sqlite3 "$recovered" "PRAGMA journal_mode=WAL;" >/dev/null 2>&1 || true
160
  mv "$db" "$broken"
161
+ install_database "$recovered" "$db"
162
+ # Keep the repaired content under its own name too: the swap above is a
163
+ # rename, and this volume has been observed not to sync the file that a
164
+ # rename creates.
165
+ cp "$db" "$backup_dir/backend.db.repaired-$stamp" 2>/dev/null || true
166
  echo "database rebuilt from recovered pages; accounts=$new_accounts (damaged copy kept at $broken)" >&2
167
  return 0
168
  fi
 
170
  rm -f "$recovered"
171
 
172
  # ---- 3b. otherwise restore the newest snapshot that verifies ----------- #
173
+ if choose_snapshot "$backup_dir" "$src_accounts" && [ -n "$CHOSEN_SNAPSHOT" ]; then
174
+ mv "$db" "$broken"
175
+ if install_database "$CHOSEN_SNAPSHOT" "$db"; then
176
+ echo "database restored from $CHOSEN_SNAPSHOT (damaged copy kept at $broken)" >&2
177
+ return 0
 
 
 
 
 
 
 
 
178
  fi
179
+ echo "ERROR: could not install $CHOSEN_SNAPSHOT; restoring the damaged original" >&2
180
+ mv "$broken" "$db" 2>/dev/null || true
181
+ return 0
182
+ fi
183
 
184
  echo "ERROR: automatic recovery failed and no valid snapshot exists; leaving the database untouched" >&2
185
  return 0