@@ -5,8 +5,11 @@ import (
55 "crypto/ed25519"
66 "encoding/hex"
77 "fmt"
8+ "slices"
9+ "time"
810
911 "github.com/cenkalti/backoff/v3"
12+ "github.com/pkg/errors"
1013 "github.com/rs/zerolog/log"
1114 substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1215 "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
@@ -63,6 +66,8 @@ func action(cli *cli.Context) error {
6366 return err
6467 }
6568
69+ subURLs := environment .MustGet ().SubstrateURL
70+ relayURLs := environment .GetRelaysURLs ()
6671 manager , err := environment .GetSubstrate ()
6772 if err != nil {
6873 return fmt .Errorf ("failed to create substrate manager: %w" , err )
@@ -92,6 +97,7 @@ func action(cli *cli.Context) error {
9297 }
9398 }()
9499
100+ // no need to restart zos-api here as it only tries to get farm and twin, donesn't mentain any open connections
95101 api , err := zosapi .NewZosAPI (manager , redis , msgBrokerCon )
96102 if err != nil {
97103 return fmt .Errorf ("failed to create zos api: %w" , err )
@@ -105,14 +111,16 @@ func action(cli *cli.Context) error {
105111
106112 bo := backoff .NewExponentialBackOff ()
107113 bo .MaxElapsedTime = 0
114+
115+ peerCtx , cancel := context .WithCancel (ctx )
108116 backoff .Retry (func () error {
109117 _ , err = peer .NewPeer (
110- ctx ,
118+ peerCtx ,
111119 hex .EncodeToString (pair .Seed ()),
112120 manager ,
113121 router .Serve ,
114122 peer .WithKeyType (peer .KeyTypeEd25519 ),
115- peer .WithRelay (environment . GetRelaysURLs () ... ),
123+ peer .WithRelay (relayURLs ... ),
116124 peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
117125 )
118126 if err != nil {
@@ -127,7 +135,79 @@ func action(cli *cli.Context) error {
127135 Uint ("worker nr" , workerNr ).
128136 Msg ("starting api-gateway module" )
129137
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+
130168 // block forever
131- <- ctx .Done ()
132- return nil
169+ for {
170+ select {
171+ case <- ctx .Done ():
172+ return nil
173+ case <- peerCtx .Done ():
174+ return nil
175+ case <- time .After (10 * time .Minute ):
176+ env , err := environment .Get ()
177+ // skip update if any problem causing the update to fail
178+ if err != nil {
179+ log .Debug ().Err (err ).Msg ("failed to load node environment" )
180+ continue
181+ }
182+
183+ updatedSubURLs := env .SubstrateURL
184+ updatedRelayURLs := environment .GetRelaysURLs ()
185+
186+ // continue if the urls did not change
187+ if slices .Equal (subURLs , updatedSubURLs ) || ! slices .Equal (relayURLs , updatedRelayURLs ) {
188+ continue
189+ }
190+
191+ newPeerCtx , newCancel := context .WithCancel (ctx )
192+
193+ newManager , err := updatePeer (newPeerCtx , updatedSubURLs , updatedRelayURLs )
194+ if err != nil {
195+ newCancel ()
196+ log .Debug ().Err (err ).Send ()
197+ continue
198+ }
199+
200+ // only update urls and cancel the context after peer is created successfully
201+ cancel ()
202+
203+ peerCtx = newPeerCtx
204+ cancel = newCancel
205+
206+ subURLs = updatedSubURLs
207+ relayURLs = updatedRelayURLs
208+
209+ manager = newManager
210+ log .Debug ().Strs ("relays_urls" , updatedRelayURLs ).Strs ("substrate_urls" , updatedSubURLs ).Msg ("updating substrate and relay urls" )
211+ }
212+ }
133213}
0 commit comments