WaitForTaskCompletion polls for task completion and returns an error if the task failed. This is a public wrapper that allows specifying a custom timeout. Proxmox task completion is determined by checking the EndTime field: - If EndTime > 0, the task has finished - If Status == "OK", the task succe
(upid string, operationName string, maxWait time.Duration)
| 155 | // |
| 156 | // Returns an error if the task fails or times out. |
| 157 | func (c *Client) WaitForTaskCompletion(upid string, operationName string, maxWait time.Duration) error { |
| 158 | c.logger.Debug("Waiting for task completion: %s (timeout: %v)", upid, maxWait) |
| 159 | |
| 160 | pollInterval := 2 * time.Second |
| 161 | start := time.Now() |
| 162 | |
| 163 | for time.Since(start) < maxWait { |
| 164 | tasks, err := c.GetClusterTasks() |
| 165 | if err != nil { |
| 166 | c.logger.Debug("Failed to get cluster tasks: %v", err) |
| 167 | time.Sleep(pollInterval) |
| 168 | continue |
| 169 | } |
| 170 | |
| 171 | // Find our task |
| 172 | for _, task := range tasks { |
| 173 | if task.UPID == upid { |
| 174 | c.logger.Debug("Found task %s, status: %q, endtime: %d", upid, task.Status, task.EndTime) |
| 175 | |
| 176 | // Task is complete when EndTime > 0 |
| 177 | if task.EndTime > 0 { |
| 178 | // Check if task succeeded |
| 179 | if task.Status == "OK" { |
| 180 | c.logger.Debug("Task %s completed successfully", upid) |
| 181 | return nil |
| 182 | } |
| 183 | // Task completed but failed - Status contains error message |
| 184 | errorMsg := task.Status |
| 185 | if errorMsg == "" { |
| 186 | errorMsg = "unknown error (empty status)" |
| 187 | } |
| 188 | c.logger.Debug("Task %s failed with status: %s", upid, errorMsg) |
| 189 | return fmt.Errorf("%s failed: %s", operationName, errorMsg) |
| 190 | } |
| 191 | // Task is still running (EndTime == 0), continue polling |
| 192 | break |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | time.Sleep(pollInterval) |
| 197 | } |
| 198 | |
| 199 | return fmt.Errorf("%s timed out after %v waiting for task %s", operationName, maxWait, upid) |
| 200 | } |
| 201 | |
| 202 | // waitForTaskCompletion is a private wrapper for backward compatibility with snapshots. |
| 203 | func (c *Client) waitForTaskCompletion(upid string, operationName string) error { |
no test coverage detected