waitForBackgroundManagersToStop wait for given BackgroundManagers to stop within given time
(ctx context.Context, waitTimeMax time.Duration, bgManagers []*BackgroundManager)
| 685 | |
| 686 | // waitForBackgroundManagersToStop wait for given BackgroundManagers to stop within given time |
| 687 | func waitForBackgroundManagersToStop(ctx context.Context, waitTimeMax time.Duration, bgManagers []*BackgroundManager) { |
| 688 | timeout := time.NewTicker(waitTimeMax) |
| 689 | defer timeout.Stop() |
| 690 | retryInterval := 1 * time.Millisecond |
| 691 | maxRetryInterval := 1 * time.Second |
| 692 | for { |
| 693 | select { |
| 694 | case <-timeout.C: |
| 695 | runningBackgroundManagerNames := "" |
| 696 | for _, bgManager := range bgManagers { |
| 697 | if !isBackgroundManagerStopped(bgManager.GetRunState()) { |
| 698 | runningBackgroundManagerNames += fmt.Sprintf(" %s", bgManager.GetName()) |
| 699 | } |
| 700 | } |
| 701 | base.WarnfCtx(ctx, "Background Managers [%s] failed to stop within deadline of %s.", runningBackgroundManagerNames, waitTimeMax) |
| 702 | return |
| 703 | case <-time.After(retryInterval): |
| 704 | stoppedServices := 0 |
| 705 | for _, bgManager := range bgManagers { |
| 706 | state := bgManager.GetRunState() |
| 707 | if isBackgroundManagerStopped(state) { |
| 708 | stoppedServices += 1 |
| 709 | } |
| 710 | } |
| 711 | if stoppedServices == len(bgManagers) { |
| 712 | return |
| 713 | } |
| 714 | |
| 715 | // exponential backoff with max wait |
| 716 | if retryInterval < maxRetryInterval { |
| 717 | retryInterval = retryInterval * 2 |
| 718 | if retryInterval > maxRetryInterval { |
| 719 | retryInterval = maxRetryInterval |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | func isBackgroundManagerStopped(state BackgroundProcessState) bool { |
| 727 | return state == BackgroundProcessStateStopped || state == BackgroundProcessStateCompleted || state == BackgroundProcessStateError || state == "" |