SimpleChatbot / sync-entrypoint.sh
Really-amin's picture
Upload 3 files
e66e94d verified
Raw History Blame Contribute Delete
2.31 kB
#!/bin/bash
# Keeps OmniRoute's SQLite state alive across Space restarts/sleeps by
# syncing /app/data to a private HuggingFace Dataset repo.
#
# Required Space secrets (Settings β†’ Variables and secrets):
# HF_TOKEN β€” a HF token with WRITE access to the dataset below
# HF_STATE_DATASET β€” e.g. "your-username/omniroute-state" (make it PRIVATE β€”
# it will contain encrypted provider keys)
# JWT_SECRET, API_KEY_SECRET, STORAGE_ENCRYPTION_KEY, INITIAL_PASSWORD
# β€” same as any other OmniRoute deployment
set -u
DATA_DIR=/app/data
mkdir -p "$DATA_DIR"
: "${HF_TOKEN:?Set HF_TOKEN as a Space secret}"
: "${HF_STATE_DATASET:?Set HF_STATE_DATASET as a Space secret, e.g. you/omniroute-state}"
echo "[sync] attempting to restore prior state from ${HF_STATE_DATASET}..."
python3 - <<PYEOF || echo "[sync] no snapshot found β€” starting fresh"
import os, shutil
from huggingface_hub import snapshot_download
path = snapshot_download(
repo_id="${HF_STATE_DATASET}",
repo_type="dataset",
token="${HF_TOKEN}",
)
for item in os.listdir(path):
if item.startswith("."):
continue
src = os.path.join(path, item)
dst = os.path.join("${DATA_DIR}", item)
if os.path.isfile(src):
shutil.copy2(src, dst)
print(f"[sync] restored {item}")
PYEOF
push_state() {
python3 - <<PYEOF
from huggingface_hub import HfApi
api = HfApi(token="${HF_TOKEN}")
api.upload_folder(
folder_path="${DATA_DIR}",
repo_id="${HF_STATE_DATASET}",
repo_type="dataset",
commit_message="omniroute state sync",
)
PYEOF
}
# Push state every 5 minutes so a sudden restart loses at most that much.
(
while true; do
sleep 300
echo "[sync] periodic push..."
push_state || echo "[sync] periodic push failed, will retry next cycle"
done
) &
SYNC_PID=$!
# Best-effort final push if the container gets a graceful stop signal.
trap 'echo "[sync] shutdown signal β€” final push"; push_state || true; kill $SYNC_PID 2>/dev/null; exit 0' SIGTERM SIGINT
# Hand off to the image's normal server process (Next.js standalone server β€”
# this is the base image's own default CMD, reproduced here explicitly
# since our CMD override replaced it).
node dev/run-standalone.mjs &
SERVER_PID=$!
wait "$SERVER_PID"