fix: Improve database recovery process

- Fix empty variable handling in data checks
- Terminate active connections before dropping database
- Increase agent threshold to 50 (9 core + 50 NODE2)
- Add better logging for agent sync verification
This commit is contained in:
Apple
2025-12-05 02:35:57 -08:00
parent db3b74e1ba
commit 06fe0c5204

View File

@@ -47,9 +47,14 @@ if [ "$MICRODAO_COUNT" -lt 5 ] || [ "$AGENT_COUNT" -lt 10 ]; then
log "📦 Found backup: $LATEST_BACKUP"
log "🔄 Restoring from backup..."
# Terminate all connections to the database first
log "🔒 Terminating active connections..."
docker exec daarion-postgres psql -U postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'daarion' AND pid <> pg_backend_pid();" 2>&1 | grep -v "terminate_backend\|^$" || true
sleep 2
# Drop and recreate database
docker exec daarion-postgres psql -U postgres -c "DROP DATABASE IF EXISTS daarion;"
docker exec daarion-postgres psql -U postgres -c "CREATE DATABASE daarion;"
docker exec daarion-postgres psql -U postgres -c "DROP DATABASE IF EXISTS daarion;" 2>&1 | grep -v "DROP DATABASE" || true
docker exec daarion-postgres psql -U postgres -c "CREATE DATABASE daarion;" 2>&1 | grep -v "CREATE DATABASE" || true
# Restore from backup
docker exec -i daarion-postgres psql -U postgres -d daarion < "$LATEST_BACKUP" 2>&1 | grep -v "already exists\|does not exist" || true
@@ -60,8 +65,13 @@ if [ "$MICRODAO_COUNT" -lt 5 ] || [ "$AGENT_COUNT" -lt 10 ]; then
docker exec -i daarion-postgres psql -U postgres -d daarion < "$f" 2>&1 | grep -v "already exists\|does not exist" || true
done
# Sync NODE2 agents
python3 scripts/sync-node2-dagi-agents.py 2>&1 | tail -5 || true
# Sync NODE2 agents (force sync even if they exist)
log "🤖 Syncing NODE2 agents..."
python3 scripts/sync-node2-dagi-agents.py 2>&1 | tail -10 || true
# Verify agent count after sync
AGENT_COUNT_AFTER=$(docker exec daarion-postgres psql -U postgres -d daarion -t -c "SELECT COUNT(*) FROM agents;" 2>/dev/null | tr -d ' \n' || echo "0")
log "📊 Agents after sync: $AGENT_COUNT_AFTER"
# Remove test agents
bash scripts/remove-test-agents.sh 2>&1 | tail -3 || true