MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / pollForConfigChange

Method pollForConfigChange

internal/ui/components/vm_config.go:1143–1212  ·  view source on GitHub ↗

pollForConfigChange polls the Proxmox API to verify that a configuration change has propagated to both the config endpoint and the cluster resources endpoint before refreshing the UI. This prevents race conditions where config is updated but cluster resources still show old names.

(vm *api.VM, expectedName string, nameChanged bool, expectedTags string, tagsChanged bool)

Source from the content-addressed store, hash-verified

1141// to both the config endpoint and the cluster resources endpoint before refreshing the UI.
1142// This prevents race conditions where config is updated but cluster resources still show old names.
1143func (app *App) pollForConfigChange(vm *api.VM, expectedName string, nameChanged bool, expectedTags string, tagsChanged bool) {
1144 client, err := app.getClientForVM(vm)
1145 if err != nil {
1146 client = app.client
1147 }
1148
1149 // Poll every 500ms for up to 15 seconds (increased timeout for cluster resources propagation)
1150 maxAttempts := 30
1151 if !nameChanged {
1152 maxAttempts = 10
1153 }
1154 for attempt := 0; attempt < maxAttempts; attempt++ {
1155 time.Sleep(500 * time.Millisecond)
1156
1157 // First check if the config endpoint has the new name using the existing API function
1158 config, err := client.GetVMConfig(vm)
1159 configUpdated := true
1160
1161 if err == nil && config != nil {
1162 if nameChanged {
1163 if vm.Type == api.VMTypeQemu && config.Name != expectedName {
1164 configUpdated = false
1165 } else if vm.Type == api.VMTypeLXC && config.Hostname != expectedName {
1166 configUpdated = false
1167 }
1168 }
1169
1170 if tagsChanged && config.Tags != expectedTags {
1171 configUpdated = false
1172 }
1173 } else {
1174 configUpdated = false
1175 }
1176
1177 // If config is updated, also check if cluster resources reflect the change when needed
1178 if configUpdated && nameChanged {
1179 // Use the existing GetVmList function to check cluster resources
1180 vmList, err := client.GetVmList(context.Background())
1181 if err == nil {
1182 for _, vmData := range vmList {
1183 if resType, exists := vmData["type"].(string); exists && resType == vm.Type {
1184 if nodeName, exists := vmData["node"].(string); exists && nodeName == vm.Node {
1185 if vmID, exists := vmData["vmid"].(float64); exists && int(vmID) == vm.ID {
1186 if name, exists := vmData["name"].(string); exists && name == expectedName {
1187 // Both config and cluster resources show the new name, we can proceed
1188 app.QueueUpdateDraw(func() {
1189 app.manualRefresh()
1190 })
1191 return
1192 }
1193 break
1194 }
1195 }
1196 }
1197 }
1198 }
1199 } else if configUpdated {
1200 app.QueueUpdateDraw(func() {

Callers 1

NewVMConfigPageFunction · 0.80

Calls 6

getClientForVMMethod · 0.95
manualRefreshMethod · 0.95
GetVMConfigMethod · 0.80
GetVmListMethod · 0.80
QueueUpdateDrawMethod · 0.80
ShowWarningMethod · 0.65

Tested by

no test coverage detected