Files
wevads-platform/scripts/send_engine_rotation.sh
2026-02-26 04:53:11 +01:00

192 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# Script de rotation automatique des moteurs d'envoi
LOG_FILE="/opt/wevads/logs/send_rotation.log"
CONFIG_DIR="/opt/wevads/configs/send_engines"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] SEND_ROTATION: $1" | tee -a "$LOG_FILE"
}
log_stderr() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] SEND_ROTATION: $1" >> "$LOG_FILE"
}
check_engine_health() {
local engine=$1
local config="$CONFIG_DIR/${engine}_config.json"
if [ ! -f "$config" ]; then
log_stderr "❌ Configuration manquante pour $engine"
return 1
fi
# Simuler une vérification de santé
case $engine in
"office365")
# Vérifier les tokens OAuth
log_stderr "🔍 Vérification Office365..."
echo "healthy"
;;
"gmail")
# Vérifier les app passwords
log_stderr "🔍 Vérification Gmail..."
echo "healthy"
;;
"amazon_ses")
# Vérifier les credentials AWS
log_stderr "🔍 Vérification Amazon SES..."
echo "healthy"
;;
"sendgrid")
# Vérifier l'API key
log_stderr "🔍 Vérification SendGrid..."
echo "healthy"
;;
*)
log_stderr "⚠️ Moteur inconnu: $engine"
echo "unknown"
;;
esac
}
rotate_engine() {
local current=$1
local reason=$2
log "🔄 Rotation demandée pour $current (raison: $reason)"
# Déterminer le prochain moteur
engines=("office365" "gmail" "amazon_ses" "sendgrid")
for engine in "${engines[@]}"; do
if [ "$engine" != "$current" ]; then
health=$(check_engine_health "$engine")
if [ "$health" = "healthy" ]; then
echo "[$(date +%H:%M:%S)] ✅ Rotation vers $engine"
# Mettre à jour la configuration active
echo "{\"active_engine\": \"$engine\", \"rotated_at\": \"$(date -Iseconds)\"}" \
> "$CONFIG_DIR/active_engine.json"
# Loguer la rotation
echo "{\"from\": \"$current\", \"to\": \"$engine\", \"reason\": \"$reason\", \"time\": \"$(date -Iseconds)\"}" \
>> "$LOG_FILE"
echo "$engine"
return 0
fi
fi
done
echo "[$(date +%H:%M:%S)] ❌ Aucun moteur alternatif sain disponible"
echo "$current"
}
monitor_performance() {
log "📊 Monitoring performance moteurs..."
# Simuler la récupération des stats
stats=$(cat << EOF
{
"office365": {"sent_today": 2450, "bounce_rate": 1.2, "inbox_rate": 86},
"gmail": {"sent_today": 320, "bounce_rate": 3.5, "inbox_rate": 72},
"amazon_ses": {"sent_today": 12500, "bounce_rate": 8.1, "inbox_rate": 91},
"sendgrid": {"sent_today": 28750, "bounce_rate": 4.8, "inbox_rate": 88}
}
EOF
)
echo "$stats"
}
auto_rotate_if_needed() {
local current_engine=$1
local stats=$2
# Extraire les stats pour le moteur courant
bounce_rate=$(echo "$stats" | jq -r ".$current_engine.bounce_rate")
inbox_rate=$(echo "$stats" | jq -r ".$current_engine.inbox_rate")
# Conditions de rotation automatique
if (( $(echo "$bounce_rate > 5" | bc -l) )); then
rotate_engine "$current_engine" "high_bounce_rate_${bounce_rate}%"
return 0
fi
if (( $(echo "$inbox_rate < 70" | bc -l) )); then
rotate_engine "$current_engine" "low_inbox_rate_${inbox_rate}%"
return 0
fi
# Vérifier la limite quotidienne
sent_today=$(echo "$stats" | jq -r ".$current_engine.sent_today")
limit_config="$CONFIG_DIR/${current_engine}_config.json"
daily_limit=$(jq -r ".${current_engine}.daily_limit" "$limit_config" 2>/dev/null || echo "1000")
if [ "$sent_today" -ge "$((daily_limit * 90 / 100))" ]; then
rotate_engine "$current_engine" "approaching_daily_limit_${sent_today}/${daily_limit}"
return 0
fi
echo "$current_engine"
}
main() {
log "🚀 Démarrage service rotation moteurs"
# Charger le moteur actif
if [ -f "$CONFIG_DIR/active_engine.json" ]; then
current_engine=$(jq -r '.active_engine' "$CONFIG_DIR/active_engine.json")
else
current_engine="office365"
echo "{\"active_engine\": \"$current_engine\"}" > "$CONFIG_DIR/active_engine.json"
fi
log "Moteur actuel: $current_engine"
# Vérifier la santé
health=$(check_engine_health "$current_engine")
if [ "$health" != "healthy" ]; then
echo "[$(date +%H:%M:%S)] ⚠️ Moteur $current_engine non sain ($health)"
new_engine=$(rotate_engine "$current_engine" "unhealthy_$health")
current_engine="$new_engine"
fi
# Monitorer les performances
stats=$(monitor_performance)
# Rotation automatique si nécessaire
new_engine=$(auto_rotate_if_needed "$current_engine" "$stats")
if [ "$new_engine" != "$current_engine" ]; then
echo "[$(date +%H:%M:%S)] 🔄 Rotation effectuée: $current_engine$new_engine"
current_engine="$new_engine"
fi
# Générer un rapport
echo "[$(date +%H:%M:%S)] 📋 RAPPORT FINAL:"
echo "[$(date +%H:%M:%S)] Moteur actif: $current_engine"
echo "[$(date +%H:%M:%S)] Prochaine vérification: $(date -d '+15 minutes' '+%H:%M')"
echo "$current_engine"
}
case "$1" in
"rotate")
current=${2:-"office365"}
reason=${3:-"manual"}
rotate_engine "$current" "$reason"
;;
"monitor")
monitor_performance
;;
"run")
main
;;
*)
echo "Usage: $0 {rotate|monitor|run}"
;;
esac