Skip to content

Commit

Permalink
improve infinite loops
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Oct 4, 2023
1 parent 668257c commit e86109c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pluralsh/deployment-operator/pkg/manifests"
deploysync "github.com/pluralsh/deployment-operator/pkg/sync"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/workqueue"
Expand Down Expand Up @@ -91,13 +92,12 @@ func (agent *Agent) Run() {
}
}()

for {
wait.PollInfinite(agent.refresh, func() (done bool, err error) {
log.Info("fetching services for cluster")
svcs, err := agent.consoleClient.GetServices()
if err != nil {
log.Error(err, "failed to fetch service list from deployments service")
time.Sleep(agent.refresh)
continue
return false, nil
}

for _, svc := range svcs {
Expand All @@ -108,12 +108,12 @@ func (agent *Agent) Run() {
info, err := agent.discoveryClient.ServerVersion()
if err != nil {
log.Error(err, "failed to fetch cluster version")
return false, nil
}
v := fmt.Sprintf("%s.%s", info.Major, info.Minor)
if err := agent.consoleClient.Ping(v); err != nil {
log.Error(err, "failed to ping cluster after scheduling syncs")
}

time.Sleep(agent.refresh)
}
return false, nil
})
}
10 changes: 5 additions & 5 deletions pkg/sync/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"fmt"
"runtime/debug"
"time"

"github.com/argoproj/gitops-engine/pkg/sync"
"github.com/argoproj/gitops-engine/pkg/sync/common"
"github.com/samber/lo"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
)

func (engine *Engine) ControlLoop() {
Expand All @@ -25,12 +25,12 @@ func (engine *Engine) ControlLoop() {

engine.RegisterHandlers()

for {
wait.PollInfinite(syncDelay, func() (done bool, err error) {
log.Info("Polling for new service updates")

item, shutdown := engine.svcQueue.Get()
if shutdown {
break
return true, nil
}

if err := engine.processItem(item); err != nil {
Expand All @@ -39,8 +39,8 @@ func (engine *Engine) ControlLoop() {

engine.syncing = ""

time.Sleep(time.Duration(syncDelay))
}
return false, nil
})
}

func (engine *Engine) processItem(item interface{}) error {
Expand Down

0 comments on commit e86109c

Please sign in to comment.