Repository Public
1#!/bin/zsh -l2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : Vrai-Prix8# File : scripts/immoka-sync.sh9# Module : scripts10# Repository : https://git.spboucher.ai/vrai-prix11# Created : 2026-09-0812# Last Modified : 2026-09-0813#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940# Auteur : Simon-Pierre Boucher — contact@spboucher.ai41# scripts/immoka-sync.sh — rafraîchissement nocturne des propriétés à vendre.42#43# 1. instantané cohérent de la base Immo-Ka (sqlite3 .backup) sur le nœud44# hôte d'Immo-Ka (M4M64b, LAN — SSH inter-nœuds via Tailscale bloqué) ;45# 2. rapatriement rsync (delta contre la copie précédente) → data/immoka.db.new ;46# 3. ré-évaluation de toutes les annonces vivantes par le moteur Vrai-Prix47# (scripts/run-eval-shards.sh, N shards parallèles) → table vp_eval ;48# 4. agrégats marché → data/marche-stats.json (lu à chaud par /stats et49# /stats/marche via src/lib/marche-stats.ts — pas de rebuild) ;50# 5. index + FTS5 pré-construits (src/lib/immoka.ts) ;51# 6. bascule atomique data/immoka.db.new → data/immoka.db, puis pm2 restart52# (le handle better-sqlite3 du serveur est un singleton : sans restart il53# continuerait de lire l'ancien inode).54#55# Usage (racine du dépôt, sur le nœud) :56# scripts/immoka-sync.sh # complet57# SKIP_FETCH=1 scripts/immoka-sync.sh # réutilise data/immoka.db.new déjà téléchargé58# SHARDS=24 scripts/immoka-sync.sh # nombre de processus d'évaluation (défaut : cœurs − 4)59# NO_RESTART=1 … # ne redémarre pas PM2 (tests)60#61# Variables : IMMOKA_HOST (défaut MacStudio-de-simon-pierre-4.local = M4M64b),62# IMMOKA_REMOTE_DB (défaut ~/apps/immo-ka/data/immoka.db),63# IMMOKA_SSH_KEY (défaut ~/.ssh/ka_guardian_ed25519, autorisée sur M4M64b),64# PM2_APP (défaut vrai-prix).65set -u66set -o pipefail67cd "$(dirname "$0")/.."68export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:$PATH"6970IMMOKA_HOST=${IMMOKA_HOST:-MacStudio-de-simon-pierre-4.local}71IMMOKA_REMOTE_DB=${IMMOKA_REMOTE_DB:-/Users/simon-pierreboucher/apps/immo-ka/data/immoka.db}72IMMOKA_SSH_KEY=${IMMOKA_SSH_KEY:-$HOME/.ssh/ka_guardian_ed25519}73PM2_APP=${PM2_APP:-vrai-prix}74NCPU=$(sysctl -n hw.ncpu 2>/dev/null || echo 8)75SHARDS=${SHARDS:-$(( NCPU > 8 ? NCPU - 4 : 4 ))}76LIVE=data/immoka.db77NEW=data/immoka.db.new78SNAP=/tmp/immoka-vp-snap.db79LOG=tmp/immoka-sync.log80mkdir -p data tmp81T0=$(date +%s)82log() { local m="[$(date '+%Y-%m-%d %H:%M:%S')] $*"; echo "$m"; echo "$m" >> "$LOG"; }83die() { log "ÉCHEC : $*"; exit 1; }8485SSH_OPTS=(-i "$IMMOKA_SSH_KEY" -o IdentitiesOnly=yes -o BatchMode=yes -o ConnectTimeout=1586 -o StrictHostKeyChecking=accept-new -o ControlMaster=no -o ControlPath=none)87REMOTE="simon-pierreboucher@$IMMOKA_HOST"8889log "=== immoka-sync — hôte $IMMOKA_HOST · shards $SHARDS ==="9091if [[ "${SKIP_FETCH:-0}" != "1" ]]; then92 # 1. instantané cohérent côté Immo-Ka (la base est chaude : WAL en écriture continue)93 log "1/6 instantané distant (.backup)…"94 ssh "${SSH_OPTS[@]}" "$REMOTE" "rm -f $SNAP && /usr/bin/sqlite3 '$IMMOKA_REMOTE_DB' '.backup $SNAP' && ls -l $SNAP" >> "$LOG" 2>&1 \95 || die "instantané distant impossible (SSH LAN vers $IMMOKA_HOST ?)"9697 # 2. rapatriement (delta contre la copie précédente si elle existe)98 log "2/6 rsync → $NEW…"99 [[ -f "$LIVE" && ! -f "$NEW" ]] && cp "$LIVE" "$NEW"100 rsync -a --inplace --no-whole-file -e "ssh ${SSH_OPTS[*]}" "$REMOTE:$SNAP" "$NEW" >> "$LOG" 2>&1 \101 || die "rsync de l'instantané"102 ssh "${SSH_OPTS[@]}" "$REMOTE" "rm -f $SNAP" >> "$LOG" 2>&1103 rm -f "$NEW-wal" "$NEW-shm"104fi105[[ -f "$NEW" ]] || die "$NEW absent"106107N_LIVE=$(/usr/bin/sqlite3 "$NEW" "select count(*) from listings where status='a-vendre' and published=1 and active=1 and dup_hidden=0 and price>0" 2>/dev/null || echo 0)108log " annonces vivantes dans l'instantané : $N_LIVE"109(( N_LIVE > 10000 )) || die "instantané suspect ($N_LIVE annonces vivantes) — bascule annulée"110111# 3. ré-évaluation par le moteur (shards parallèles) → tmp/eval-shards/*.jsonl112log "3/6 évaluation de $N_LIVE annonces ($SHARDS shards)…"113IMMOKA_DB="$PWD/$NEW" SHARDS=$SHARDS scripts/run-eval-shards.sh >> "$LOG" 2>&1 || die "run-eval-shards"114N_EVAL=$(cat tmp/eval-shards/*.jsonl 2>/dev/null | wc -l | tr -d ' ')115log " $N_EVAL évaluations produites"116(( N_EVAL > 10000 )) || die "trop peu d'évaluations ($N_EVAL) — bascule annulée"117118# 4. table vp_eval + agrégats marché (fichier lu à chaud par le site)119log "4/6 table vp_eval + data/marche-stats.json…"120IMMOKA_DB="$PWD/$NEW" MARCHE_STATS_OUT="$PWD/data/marche-stats.json.new" node scripts/build-marche-stats.mjs >> "$LOG" 2>&1 || die "build-marche-stats"121122# 5. index + FTS5 (sinon la première requête du site les construirait)123log "5/6 index + FTS5…"124IMMOKA_DB="$PWD/$NEW" npx tsx -e "import('./src/lib/immoka.ts').then((m) => { m.getImmoDb(); console.log('index ok'); })" >> "$LOG" 2>&1 || die "index/FTS"125126# 6. bascule atomique + restart127log "6/6 bascule → $LIVE + restart $PM2_APP…"128mv -f data/marche-stats.json.new data/marche-stats.json129rm -f "$LIVE-wal" "$LIVE-shm"130mv -f "$NEW" "$LIVE"131rm -f "$NEW-wal" "$NEW-shm"132if [[ "${NO_RESTART:-0}" != "1" ]] && command -v pm2 >/dev/null 2>&1; then133 pm2 restart "$PM2_APP" --update-env >> "$LOG" 2>&1 || log "avertissement : pm2 restart $PM2_APP a échoué"134fi135rm -f tmp/eval-shards/*.jsonl136log "=== terminé en $(( $(date +%s) - T0 )) s — $N_LIVE annonces, $N_EVAL évaluées ==="137