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"
@@ -144,7 +143,16 @@ func action(cli *cli.Context) error {
144143 return nil
145144 // check if we need to run an update on the peer and only do the update if all the changes are done successfully
146145 case <- time .After (10 * time .Minute ):
147- log .Debug ().Msg ("checking for updates in zos config" )
146+
147+ log .Debug ().Msg ("checking if node is maintaining a healty substrate connection" )
148+ cl , _ , err := manager .Raw ()
149+ if err == nil {
150+ // skip update if the connection is working properly
151+ log .Debug ().Msg ("the open connection is healty, no update needed" )
152+ cl .Client .Close ()
153+ continue
154+ }
155+
148156 env , err := environment .Get ()
149157 if err != nil {
150158 // skip update if we can't get env
@@ -163,26 +171,35 @@ func action(cli *cli.Context) error {
163171
164172 // skip update if substrate and relay urls did not change
165173 if slices .Equal (subURLs , updatedSubURLs ) && slices .Equal (relayURLs , updatedRelayURLs ) {
174+ log .Debug ().Msg ("zos-config doesn't have updated config to update the node with" )
166175 continue
167176 }
168177
169- // skip update if the old substrate connection is working fine
170- if ! slices .Equal (subURLs , updatedSubURLs ) {
171- if _ , err := gw .GetTime (); err == nil {
172- log .Debug ().Msg ("the open connection is already working, no update needed" )
173- continue
174- }
178+ log .Debug ().Strs ("relays_urls" , updatedRelayURLs ).Strs ("substrate_urls" , updatedSubURLs ).Msg ("detected new update in configuration" )
179+ newPeerCtx , newCancel := context .WithCancel (ctx )
180+
181+ newManager , err := environment .GetSubstrate ()
182+ if err != nil {
183+ // skip update if can't get sub manager
184+ log .Debug ().Err (err ).Msg ("failed to get substrate manager" )
185+ continue
175186 }
176187
177- newPeerCtx , newCancel := context .WithCancel (ctx )
178- if err := updatePeer (newPeerCtx , updatedSubURLs , updatedRelayURLs , pair .Seed (), router .Serve ); err != nil {
179- log .Debug ().Err (err ).Send ()
180- newCancel ()
188+ if _ , err = peer .NewPeer (
189+ newPeerCtx ,
190+ hex .EncodeToString (pair .Seed ()),
191+ newManager ,
192+ router .Serve ,
193+ peer .WithKeyType (peer .KeyTypeEd25519 ),
194+ peer .WithRelay (updatedRelayURLs ... ),
195+ peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
196+ ); err != nil {
197+ log .Debug ().Err (err ).Msg ("failed to start a new rmb peer" )
181198 continue
182199 }
183200
184201 if ! slices .Equal (subURLs , updatedSubURLs ) {
185- err = gw .UpdateSubstrateGatewayConnection (manager )
202+ err = gw .UpdateSubstrateGatewayConnection (newManager )
186203 if err != nil {
187204 log .Debug ().Err (err ).Msg ("failed to update substrate gateway with new manager" )
188205 newCancel ()
@@ -194,33 +211,11 @@ func action(cli *cli.Context) error {
194211 peerCtx = newPeerCtx
195212 cancel = newCancel
196213
214+ manager = newManager
197215 relayURLs = updatedRelayURLs
198216 subURLs = updatedSubURLs
217+
199218 log .Debug ().Strs ("relays_urls" , relayURLs ).Strs ("substrate_urls" , subURLs ).Msg ("updated substrate and relay urls" )
200219 }
201220 }
202221}
203-
204- func updatePeer (ctx context.Context , subURLs , relayURLs []string , seed []byte , serve peer.Handler ) error {
205- log .Debug ().Strs ("relays_urls" , relayURLs ).Strs ("substrate_urls" , subURLs ).Msg ("detected new update in configuration" )
206-
207- manager , err := environment .GetSubstrate ()
208- if err != nil {
209- // skip update if can't get sub manager
210- return errors .Wrap (err , "failed to get substrate manager" )
211- }
212-
213- if _ , err = peer .NewPeer (
214- ctx ,
215- hex .EncodeToString (seed ),
216- manager ,
217- serve ,
218- peer .WithKeyType (peer .KeyTypeEd25519 ),
219- peer .WithRelay (relayURLs ... ),
220- peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
221- ); err != nil {
222- return errors .Wrap (err , "failed to start a new rmb peer" )
223- }
224-
225- return nil
226- }
0 commit comments