chore: build
This commit is contained in:
@@ -83,7 +83,8 @@ func processAutoAssignmentWithPriority(database *db.Database, geoService *servic
|
||||
}
|
||||
|
||||
// Tenter l'assignation
|
||||
success := tryAssignCommandWithPriority(database, geoService, commandID, address, priority, int(waitingTime.Minutes()))
|
||||
username, _ := cmd["username"].(string)
|
||||
success := tryAssignCommandWithPriority(database, geoService, commandID, username, address, priority, int(waitingTime.Minutes()))
|
||||
if success {
|
||||
assignedCount++
|
||||
} else {
|
||||
@@ -103,6 +104,7 @@ func tryAssignCommandWithPriority(
|
||||
database *db.Database,
|
||||
geoService *services.GeoService,
|
||||
commandID int,
|
||||
username string,
|
||||
address string,
|
||||
priority int,
|
||||
waitingMinutes int,
|
||||
@@ -114,6 +116,7 @@ func tryAssignCommandWithPriority(
|
||||
location, err := geoService.GeocodeAddress(address)
|
||||
if err != nil {
|
||||
log.Printf("❌ [CRON] Cmd %d - Géocodage échoué: %v", commandID, err)
|
||||
notifyGeocodeFailure(database, username, commandID, address)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -198,3 +201,26 @@ func tryAssignCommandWithPriority(
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// notifyGeocodeFailure avertit le client que l'adresse de sa commande n'a pas
|
||||
// pu être localisée, pour qu'il puisse la corriger. Le cron retente chaque
|
||||
// minute tant que la commande reste pending : un cooldown Redis d'une heure
|
||||
// évite de spammer le client à chaque cycle avec la même erreur.
|
||||
func notifyGeocodeFailure(database *db.Database, username string, commandID int, address string) {
|
||||
if username == "" {
|
||||
return
|
||||
}
|
||||
cooldownKey := fmt.Sprintf("notif:cooldown:geocode_fail:%d", commandID)
|
||||
set, err := db.Redis.SetNX(db.RedisCtx, cooldownKey, "1", time.Hour).Result()
|
||||
if err != nil || !set {
|
||||
return
|
||||
}
|
||||
clientOrderID := database.GetClientOrderID(commandID)
|
||||
msg := fmt.Sprintf(
|
||||
"Ta commande #%d ne peut pas être assignée : l'adresse \"%s\" n'a pas été trouvée. Merci de vérifier et corriger l'adresse de livraison.",
|
||||
clientOrderID, address,
|
||||
)
|
||||
if err := database.NotifyClient(username, commandID, "address_error", msg); err != nil {
|
||||
log.Printf("⚠️ [CRON] Cmd %d - Erreur notification échec géocodage: %v", commandID, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@ func StartRedisWorkers(database *db.Database) {
|
||||
|
||||
go AutoAssignWorker(database)
|
||||
|
||||
go StockCleanupWorker(database)
|
||||
|
||||
go PointsSyncWorker(database)
|
||||
|
||||
log.Println("✅ Tous les workers Redis sont démarrés")
|
||||
@@ -76,48 +74,6 @@ func AutoAssignWorker(database *db.Database) {
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// WORKER NETTOYAGE STOCK
|
||||
// ============================================
|
||||
|
||||
// StockCleanupWorker nettoie les réservations expirées
|
||||
func StockCleanupWorker(database *db.Database) {
|
||||
ticker := time.NewTicker(5 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
log.Println("🧹 Worker Nettoyage Stock démarré (check toutes les 5 min)")
|
||||
|
||||
for range ticker.C {
|
||||
cleanedCount := 0
|
||||
|
||||
// Récupérer toutes les clés de réservation
|
||||
keys, err := db.Redis.Keys(db.RedisCtx, "stock:reserve:*").Result()
|
||||
if err != nil {
|
||||
log.Printf("⚠️ Erreur récupération réservations: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
// Vérifier si la réservation est expirée
|
||||
ttl, err := db.Redis.TTL(db.RedisCtx, key).Result()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Si TTL <= 0, la réservation est expirée
|
||||
if ttl <= 0 {
|
||||
// Redis va automatiquement supprimer la clé
|
||||
// Mais on peut log pour traçabilité
|
||||
cleanedCount++
|
||||
}
|
||||
}
|
||||
|
||||
if cleanedCount > 0 {
|
||||
log.Printf("🧹 %d réservations de stock expirées nettoyées", cleanedCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// WORKER SYNCHRONISATION POINTS
|
||||
// ============================================
|
||||
|
||||
Reference in New Issue
Block a user