@@ -4,13 +4,13 @@ import (
44 "fmt"
55 "time"
66
7- "github.com/alitto/pond"
87 "github.com/argoproj/gitops-engine/pkg/cache"
98 "github.com/argoproj/gitops-engine/pkg/engine"
109 "github.com/pluralsh/deployment-operator/pkg/client"
1110 "github.com/pluralsh/deployment-operator/pkg/manifests"
1211 deploysync "github.com/pluralsh/deployment-operator/pkg/sync"
1312 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13+ "k8s.io/apimachinery/pkg/util/wait"
1414 "k8s.io/client-go/discovery"
1515 "k8s.io/client-go/tools/clientcmd"
1616 "k8s.io/client-go/util/workqueue"
@@ -26,6 +26,7 @@ type Agent struct {
2626 discoveryClient * discovery.DiscoveryClient
2727 engine * deploysync.Engine
2828 deathChan chan interface {}
29+ svcQueue workqueue.RateLimitingInterface
2930 cleanup engine.StopFunc
3031 refresh time.Duration
3132}
@@ -73,44 +74,46 @@ func New(clientConfig clientcmd.ClientConfig, refresh time.Duration, consoleUrl,
7374 consoleClient : consoleClient ,
7475 engine : engine ,
7576 deathChan : deathChan ,
77+ svcQueue : svcQueue ,
7678 cleanup : cleanup ,
7779 refresh : refresh ,
7880 }, nil
7981}
8082
8183func (agent * Agent ) Run () {
8284 defer agent .cleanup ()
85+ defer agent .svcQueue .ShutDown ()
8386 defer agent .engine .WipeCache ()
84- panicHandler := func (p interface {}) {
85- fmt .Printf ("Task panicked: %v" , p )
86- }
87+ go func () {
88+ for {
89+ go agent .engine .ControlLoop ()
90+ failure := <- agent .deathChan
91+ fmt .Printf ("recovered from panic %v\n " , failure )
92+ }
93+ }()
8794
88- for {
95+ wait . PollInfinite ( agent . refresh , func () ( done bool , err error ) {
8996 log .Info ("fetching services for cluster" )
9097 svcs , err := agent .consoleClient .GetServices ()
9198 if err != nil {
92- log .Error (err , "failed to fetch service list from deployments service %v" , err )
93- time .Sleep (agent .refresh )
94- continue
99+ log .Error (err , "failed to fetch service list from deployments service" )
100+ return false , nil
95101 }
96- pool := pond . New ( 20 , 100 , pond . MinWorkers ( 20 ), pond . PanicHandler ( panicHandler ))
102+
97103 for _ , svc := range svcs {
98- log .Info ("sending update for" , "service" , svc .ID , "namespace" , svc .Namespace , "name" , svc .Name )
99- pool .TrySubmit (func () {
100- if err := agent .engine .ProcessItem (svc .ID ); err != nil {
101- log .Error (err , "found unprocessable error" )
102- }
103- })
104+ log .Info ("sending update for" , "service" , svc .ID )
105+ agent .svcQueue .Add (svc .ID )
104106 }
105- pool . StopAndWait ()
107+
106108 info , err := agent .discoveryClient .ServerVersion ()
107109 if err != nil {
108110 log .Error (err , "failed to fetch cluster version" )
111+ return false , nil
109112 }
110113 v := fmt .Sprintf ("%s.%s" , info .Major , info .Minor )
111114 if err := agent .consoleClient .Ping (v ); err != nil {
112115 log .Error (err , "failed to ping cluster after scheduling syncs" )
113116 }
114- time . Sleep ( agent . refresh )
115- }
117+ return false , nil
118+ })
116119}
0 commit comments