99 "time"
1010
1111 "github.com/cenkalti/backoff/v3"
12- "github.com/pkg/errors"
1312 "github.com/rs/zerolog/log"
1413 substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1514 "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
@@ -113,6 +112,8 @@ func action(cli *cli.Context) error {
113112 bo .MaxElapsedTime = 0
114113
115114 peerCtx , cancel := context .WithCancel (ctx )
115+ defer cancel ()
116+
116117 backoff .Retry (func () error {
117118 _ , err = peer .NewPeer (
118119 peerCtx ,
@@ -135,79 +136,75 @@ func action(cli *cli.Context) error {
135136 Uint ("worker nr" , workerNr ).
136137 Msg ("starting api-gateway module" )
137138
138- updatePeer := func (ctx context.Context , updatedSubURLs , updatedRelayURLs []string ) (substrate.Manager , error ) {
139- var newManager substrate.Manager
140-
141- if ! slices .Equal (subURLs , updatedSubURLs ) {
142- newManager , err = environment .GetSubstrate ()
143- if err != nil {
144- return nil , errors .Wrapf (err , "failed to create substrate manager" )
145- }
146- err = gw .UpdateSubstrateGatewayConnection (manager )
147- if err != nil {
148- return nil , errors .Wrapf (err , "failed to update substrate gateway with new manager" )
149- }
150-
151- }
152-
153- _ , err = peer .NewPeer (
154- peerCtx ,
155- hex .EncodeToString (pair .Seed ()),
156- newManager ,
157- router .Serve ,
158- peer .WithKeyType (peer .KeyTypeEd25519 ),
159- peer .WithRelay (relayURLs ... ),
160- peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
161- )
162- if err != nil {
163- errors .Wrapf (err , "failed to start a new rmb peer" )
164- }
165- return newManager , nil
166- }
167-
168139 // block forever
169140 for {
170141 select {
171142 case <- ctx .Done ():
172143 return nil
173- case <- peerCtx .Done ():
174- return nil
144+ // check if we need to run an update on the peer and only do the update if all the changes are done successfully
175145 case <- time .After (10 * time .Minute ):
176146 env , err := environment .Get ()
177- // skip update if any problem causing the update to fail
178147 if err != nil {
148+ // skip update if we can't get env
179149 log .Debug ().Err (err ).Msg ("failed to load node environment" )
180150 continue
181151 }
182152
183153 updatedSubURLs := env .SubstrateURL
184154 updatedRelayURLs := environment .GetRelaysURLs ()
185155
186- // continue if the urls did not change
187- if slices .Equal (subURLs , updatedSubURLs ) || ! slices .Equal (relayURLs , updatedRelayURLs ) {
156+ // make sure urls are sorted for comparison
157+ slices .Sort (subURLs )
158+ slices .Sort (relayURLs )
159+ slices .Sort (updatedSubURLs )
160+ slices .Sort (updatedRelayURLs )
161+
162+ // skip update if the urls did not change
163+ if slices .Equal (subURLs , updatedSubURLs ) && slices .Equal (relayURLs , updatedRelayURLs ) {
188164 continue
189165 }
190166
191- newPeerCtx , newCancel := context . WithCancel ( ctx )
167+ log . Debug (). Strs ( "relays_urls" , updatedRelayURLs ). Strs ( "substrate_urls" , env . SubstrateURL ). Msg ( "detected new update in configuration" )
192168
193- newManager , err := updatePeer ( newPeerCtx , updatedSubURLs , updatedRelayURLs )
169+ manager , err = environment . GetSubstrate ( )
194170 if err != nil {
171+ // skip update if can't get sub manager
172+ log .Debug ().Err (err ).Msg ("failed to get substrate manager" )
173+ continue
174+ }
175+
176+ newPeerCtx , newCancel := context .WithCancel (ctx )
177+ if _ , err = peer .NewPeer (
178+ newPeerCtx ,
179+ hex .EncodeToString (pair .Seed ()),
180+ manager ,
181+ router .Serve ,
182+ peer .WithKeyType (peer .KeyTypeEd25519 ),
183+ peer .WithRelay (updatedRelayURLs ... ),
184+ peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
185+ ); err != nil {
195186 newCancel ()
196- log .Debug ().Err (err ).Send ( )
187+ log .Debug ().Err (err ).Msg ( "failed to start a new rmb peer" )
197188 continue
198189 }
199190
200- // only update urls and cancel the context after peer is created successfully
201- cancel ()
191+ if ! slices .Equal (subURLs , updatedSubURLs ) {
192+ err = gw .UpdateSubstrateGatewayConnection (manager )
193+ if err != nil {
194+ newCancel ()
195+ log .Debug ().Err (err ).Msg ("failed to update substrate gateway with new manager" )
196+ continue
197+ }
198+ }
202199
200+ cancel ()
203201 peerCtx = newPeerCtx
204202 cancel = newCancel
205203
206- subURLs = updatedSubURLs
207204 relayURLs = updatedRelayURLs
205+ subURLs = updatedSubURLs
208206
209- manager = newManager
210- log .Debug ().Strs ("relays_urls" , updatedRelayURLs ).Strs ("substrate_urls" , updatedSubURLs ).Msg ("updating substrate and relay urls" )
207+ log .Debug ().Strs ("relays_urls" , relayURLs ).Strs ("substrate_urls" , subURLs ).Msg ("updated substrate and relay urls" )
211208 }
212209 }
213210}
0 commit comments