showStatus polls the codespace for a list of post create states and their status. It will keep polling until all states have finished. Once all states have finished, we poll once more to check if any new states have been introduced and stop polling otherwise.
(ctx context.Context, codespace *api.Codespace)
| 450 | // until all states have finished. Once all states have finished, we poll once more to check if any new |
| 451 | // states have been introduced and stop polling otherwise. |
| 452 | func (a *App) showStatus(ctx context.Context, codespace *api.Codespace) error { |
| 453 | var ( |
| 454 | lastState codespaces.PostCreateState |
| 455 | breakNextState bool |
| 456 | ) |
| 457 | |
| 458 | finishedStates := make(map[string]bool) |
| 459 | ctx, stopPolling := context.WithCancel(ctx) |
| 460 | defer stopPolling() |
| 461 | |
| 462 | poller := func(states []codespaces.PostCreateState) { |
| 463 | var inProgress bool |
| 464 | for _, state := range states { |
| 465 | if _, found := finishedStates[state.Name]; found { |
| 466 | continue // skip this state as we've processed it already |
| 467 | } |
| 468 | |
| 469 | if state.Name != lastState.Name { |
| 470 | a.StartProgressIndicatorWithLabel(state.Name) |
| 471 | |
| 472 | if state.Status == codespaces.PostCreateStateRunning { |
| 473 | inProgress = true |
| 474 | lastState = state |
| 475 | break |
| 476 | } |
| 477 | |
| 478 | finishedStates[state.Name] = true |
| 479 | a.StopProgressIndicator() |
| 480 | } else { |
| 481 | if state.Status == codespaces.PostCreateStateRunning { |
| 482 | inProgress = true |
| 483 | break |
| 484 | } |
| 485 | |
| 486 | finishedStates[state.Name] = true |
| 487 | a.StopProgressIndicator() |
| 488 | lastState = codespaces.PostCreateState{} // reset the value |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if !inProgress { |
| 493 | if breakNextState { |
| 494 | stopPolling() |
| 495 | return |
| 496 | } |
| 497 | breakNextState = true |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | err := codespaces.PollPostCreateStates(ctx, a, a.apiClient, codespace, poller) |
| 502 | if err != nil { |
| 503 | if errors.Is(err, context.Canceled) && breakNextState { |
| 504 | return nil // we cancelled the context to stop polling, we can ignore the error |
| 505 | } |
| 506 | |
| 507 | return fmt.Errorf("failed to poll state changes from codespace: %w", err) |
| 508 | } |
| 509 |
no test coverage detected