MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / waitForBackgroundManagersToStop

Function waitForBackgroundManagersToStop

db/database.go:687–724  ·  view source on GitHub ↗

waitForBackgroundManagersToStop wait for given BackgroundManagers to stop within given time

(ctx context.Context, waitTimeMax time.Duration, bgManagers []*BackgroundManager)

Source from the content-addressed store, hash-verified

685
686// waitForBackgroundManagersToStop wait for given BackgroundManagers to stop within given time
687func 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
726func isBackgroundManagerStopped(state BackgroundProcessState) bool {
727 return state == BackgroundProcessStateStopped || state == BackgroundProcessStateCompleted || state == BackgroundProcessStateError || state == ""

Callers 2

CloseMethod · 0.85

Calls 6

WarnfCtxFunction · 0.92
GetRunStateMethod · 0.80
AfterMethod · 0.80
StopMethod · 0.65
GetNameMethod · 0.65

Tested by 1