99 "time"
1010
1111 "github.com/cenkalti/backoff/v3"
12+ "github.com/pkg/errors"
1213 "github.com/rs/zerolog/log"
1314 substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1415 "github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
@@ -143,6 +144,11 @@ func action(cli *cli.Context) error {
143144 return nil
144145 // check if we need to run an update on the peer and only do the update if all the changes are done successfully
145146 case <- time .After (10 * time .Minute ):
147+ // if we can get time from substrate then we don't need to update the connection
148+ if _ , err := gw .GetTime (); err == nil {
149+ continue
150+ }
151+
146152 env , err := environment .Get ()
147153 if err != nil {
148154 // skip update if we can't get env
@@ -164,35 +170,18 @@ func action(cli *cli.Context) error {
164170 continue
165171 }
166172
167- log .Debug ().Strs ("relays_urls" , updatedRelayURLs ).Strs ("substrate_urls" , env .SubstrateURL ).Msg ("detected new update in configuration" )
168-
169- manager , err = environment .GetSubstrate ()
170- 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-
176173 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 {
174+ if err := updatePeer (newPeerCtx , updatedSubURLs , updatedRelayURLs , pair .Seed (), router .Serve ); err != nil {
175+ log .Debug ().Err (err ).Send ()
186176 newCancel ()
187- log .Debug ().Err (err ).Msg ("failed to start a new rmb peer" )
188177 continue
189178 }
190179
191180 if ! slices .Equal (subURLs , updatedSubURLs ) {
192181 err = gw .UpdateSubstrateGatewayConnection (manager )
193182 if err != nil {
194- newCancel ()
195183 log .Debug ().Err (err ).Msg ("failed to update substrate gateway with new manager" )
184+ newCancel ()
196185 continue
197186 }
198187 }
@@ -203,8 +192,31 @@ func action(cli *cli.Context) error {
203192
204193 relayURLs = updatedRelayURLs
205194 subURLs = updatedSubURLs
206-
207195 log .Debug ().Strs ("relays_urls" , relayURLs ).Strs ("substrate_urls" , subURLs ).Msg ("updated substrate and relay urls" )
208196 }
209197 }
210198}
199+
200+ func updatePeer (ctx context.Context , subURLs , relayURLs []string , seed []byte , serve peer.Handler ) error {
201+ log .Debug ().Strs ("relays_urls" , relayURLs ).Strs ("substrate_urls" , subURLs ).Msg ("detected new update in configuration" )
202+
203+ manager , err := environment .GetSubstrate ()
204+ if err != nil {
205+ // skip update if can't get sub manager
206+ return errors .Wrap (err , "failed to get substrate manager" )
207+ }
208+
209+ if _ , err = peer .NewPeer (
210+ ctx ,
211+ hex .EncodeToString (seed ),
212+ manager ,
213+ serve ,
214+ peer .WithKeyType (peer .KeyTypeEd25519 ),
215+ peer .WithRelay (relayURLs ... ),
216+ peer .WithInMemoryExpiration (6 * 60 * 60 ), // 6 hours
217+ ); err != nil {
218+ return errors .Wrap (err , "failed to start a new rmb peer" )
219+ }
220+
221+ return nil
222+ }
0 commit comments