package agentsvc import ( "time" llmgwv1 "gitea.mynodes.xyz/ilker/llmgw/api/gen/llmgw/v1" ) // SetAgentSettings gives the service each node's agent settings from the // cluster's (BACKLOG E3), as YAML, read at each use so a change is what is // sent next. func (s *Service) SetAgentSettings(f func(node string) string) { s.mu.Lock() s.agentSettings = f s.mu.Unlock() } func (s *Service) agentSettingsMsg(node string) *llmgwv1.ControlMessage { s.mu.RLock() f := s.agentSettings s.mu.RUnlock() if f == nil { return nil } return &llmgwv1.ControlMessage{Msg: &llmgwv1.ControlMessage_AgentSettings{ AgentSettings: &llmgwv1.AgentSettings{Yaml: f(node)}}} } // agentSettingsGap is the pause between telling one node and the next: an // agent whose settings changed starts again, and a whole cluster's agents // doing so in the same second would all re-register at once. var agentSettingsGap = time.Second // BroadcastAgentSettings tells every connected agent what the cluster's // settings now say it runs on, a moment apart. A node not connected is told // when it registers. func (s *Service) BroadcastAgentSettings() { for _, node := range s.ConnectedNodes() { msg := s.agentSettingsMsg(node) if msg == nil { return } if err := s.send(node, msg); err != nil { s.log.Warn("could not tell a node its agent settings; it is told when it next registers", "node", node, "err", err) continue } time.Sleep(agentSettingsGap) } }