feat: enhance desktop client presence management and add monitoring for active platforms
Backend CI / Backend (push) Successful in 14m43s
Backend CI / Backend (push) Successful in 14m43s
This commit is contained in:
@@ -56,6 +56,16 @@ type monitorDesktopTaskTarget struct {
|
||||
ClientID uuid.UUID
|
||||
}
|
||||
|
||||
type monitorDesktopTaskTargetCandidate struct {
|
||||
Target monitorDesktopTaskTarget
|
||||
HealthRank int
|
||||
OnlineRank int
|
||||
SourceRank int
|
||||
OnlineSinceUnixMilli int64
|
||||
RecordOrder int
|
||||
ClientOrder int
|
||||
}
|
||||
|
||||
type monitorDesktopInterruptTarget struct {
|
||||
TaskID string
|
||||
MonitorTaskID int64
|
||||
@@ -334,7 +344,7 @@ func (s *MonitoringService) loadMonitorDesktopTaskTargets(
|
||||
}
|
||||
|
||||
accountIDs := monitorDesktopAccountCandidateAccountIDs(candidates)
|
||||
accountPresence := loadDesktopAccountPresence(ctx, s.redis, accountIDs)
|
||||
accountPresence := loadDesktopAccountPresenceClients(ctx, s.redis, accountIDs)
|
||||
clientIDs := monitorDesktopAccountCandidateClientIDs(candidates, accountPresence)
|
||||
onlineClients, err := s.loadOnlineMonitorDesktopClients(ctx, workspaceID, clientIDs)
|
||||
if err != nil {
|
||||
@@ -373,15 +383,17 @@ func monitorDesktopAccountCandidateAccountIDs(candidates []monitorDesktopAccount
|
||||
return uniqueUUIDs(result)
|
||||
}
|
||||
|
||||
func monitorDesktopAccountCandidateClientIDs(candidates []monitorDesktopAccountCandidate, accountPresence map[uuid.UUID]uuid.UUID) []uuid.UUID {
|
||||
func monitorDesktopAccountCandidateClientIDs(candidates []monitorDesktopAccountCandidate, accountPresence map[uuid.UUID][]desktopAccountPresenceClient) []uuid.UUID {
|
||||
result := make([]uuid.UUID, 0, len(candidates)*2)
|
||||
for _, candidate := range candidates {
|
||||
if candidate.DBClientID != nil && *candidate.DBClientID != uuid.Nil {
|
||||
result = append(result, *candidate.DBClientID)
|
||||
}
|
||||
if accountPresence != nil {
|
||||
if clientID, ok := accountPresence[candidate.AccountID]; ok && clientID != uuid.Nil {
|
||||
result = append(result, clientID)
|
||||
for _, client := range accountPresence[candidate.AccountID] {
|
||||
if client.ClientID != uuid.Nil {
|
||||
result = append(result, client.ClientID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -430,35 +442,95 @@ func (s *MonitoringService) loadOnlineMonitorDesktopClients(ctx context.Context,
|
||||
|
||||
func selectMonitorDesktopTaskTargets(
|
||||
candidates []monitorDesktopAccountCandidate,
|
||||
accountPresence map[uuid.UUID]uuid.UUID,
|
||||
accountPresence map[uuid.UUID][]desktopAccountPresenceClient,
|
||||
onlineClients map[uuid.UUID]bool,
|
||||
) map[string]monitorDesktopTaskTarget {
|
||||
result := make(map[string]monitorDesktopTaskTarget)
|
||||
targetsByPlatform := make(map[string][]monitorDesktopTaskTarget)
|
||||
bestByPlatform := make(map[string]monitorDesktopTaskTargetCandidate)
|
||||
for _, candidate := range candidates {
|
||||
platformID := normalizeMonitoringPlatformID(candidate.PlatformID)
|
||||
if platformID == "" || candidate.AccountID == uuid.Nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if clientID, ok := accountPresence[candidate.AccountID]; ok && clientID != uuid.Nil && onlineClients[clientID] {
|
||||
targetsByPlatform[platformID] = append(targetsByPlatform[platformID], monitorDesktopTaskTarget{AccountID: candidate.AccountID, ClientID: clientID})
|
||||
seenClientIDs := make(map[uuid.UUID]struct{})
|
||||
if accountPresence != nil {
|
||||
for index, client := range accountPresence[candidate.AccountID] {
|
||||
if client.ClientID == uuid.Nil || !onlineClients[client.ClientID] {
|
||||
continue
|
||||
}
|
||||
seenClientIDs[client.ClientID] = struct{}{}
|
||||
item := monitorDesktopTaskTargetCandidate{
|
||||
Target: monitorDesktopTaskTarget{
|
||||
AccountID: candidate.AccountID,
|
||||
ClientID: client.ClientID,
|
||||
},
|
||||
HealthRank: candidate.HealthRank,
|
||||
OnlineRank: 0,
|
||||
SourceRank: 0,
|
||||
OnlineSinceUnixMilli: client.OnlineSinceUnixMilli,
|
||||
RecordOrder: candidate.RecordOrder,
|
||||
ClientOrder: index,
|
||||
}
|
||||
if current, ok := bestByPlatform[platformID]; !ok || item.betterThan(current) {
|
||||
bestByPlatform[platformID] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
if candidate.DBClientID == nil || *candidate.DBClientID == uuid.Nil || !onlineClients[*candidate.DBClientID] {
|
||||
continue
|
||||
}
|
||||
if candidate.DBClientID != nil && *candidate.DBClientID != uuid.Nil && onlineClients[*candidate.DBClientID] {
|
||||
targetsByPlatform[platformID] = append(targetsByPlatform[platformID], monitorDesktopTaskTarget{AccountID: candidate.AccountID, ClientID: *candidate.DBClientID})
|
||||
if _, exists := seenClientIDs[*candidate.DBClientID]; exists {
|
||||
continue
|
||||
}
|
||||
item := monitorDesktopTaskTargetCandidate{
|
||||
Target: monitorDesktopTaskTarget{
|
||||
AccountID: candidate.AccountID,
|
||||
ClientID: *candidate.DBClientID,
|
||||
},
|
||||
HealthRank: candidate.HealthRank,
|
||||
OnlineRank: 0,
|
||||
SourceRank: 1,
|
||||
RecordOrder: candidate.RecordOrder,
|
||||
}
|
||||
if current, ok := bestByPlatform[platformID]; !ok || item.betterThan(current) {
|
||||
bestByPlatform[platformID] = item
|
||||
}
|
||||
}
|
||||
for platformID, targets := range targetsByPlatform {
|
||||
if len(targets) == 0 {
|
||||
continue
|
||||
}
|
||||
index := randomIndex(len(targets))
|
||||
result[platformID] = targets[index]
|
||||
for platformID, candidate := range bestByPlatform {
|
||||
result[platformID] = candidate.Target
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (candidate monitorDesktopTaskTargetCandidate) betterThan(current monitorDesktopTaskTargetCandidate) bool {
|
||||
if candidate.HealthRank != current.HealthRank {
|
||||
return candidate.HealthRank < current.HealthRank
|
||||
}
|
||||
if candidate.SourceRank != current.SourceRank {
|
||||
return candidate.SourceRank < current.SourceRank
|
||||
}
|
||||
if candidate.OnlineSinceUnixMilli != current.OnlineSinceUnixMilli {
|
||||
if candidate.OnlineSinceUnixMilli == 0 {
|
||||
return false
|
||||
}
|
||||
if current.OnlineSinceUnixMilli == 0 {
|
||||
return true
|
||||
}
|
||||
return candidate.OnlineSinceUnixMilli < current.OnlineSinceUnixMilli
|
||||
}
|
||||
if candidate.OnlineRank != current.OnlineRank {
|
||||
return candidate.OnlineRank < current.OnlineRank
|
||||
}
|
||||
if candidate.RecordOrder != current.RecordOrder {
|
||||
return candidate.RecordOrder < current.RecordOrder
|
||||
}
|
||||
if candidate.ClientOrder != current.ClientOrder {
|
||||
return candidate.ClientOrder < current.ClientOrder
|
||||
}
|
||||
return candidate.Target.ClientID.String() < current.Target.ClientID.String()
|
||||
}
|
||||
|
||||
func (s *MonitoringService) upsertMonitorDesktopTask(
|
||||
ctx context.Context,
|
||||
tx pgx.Tx,
|
||||
|
||||
Reference in New Issue
Block a user