Skip to content

Commit 78cd102

Browse files
committed
fix issue with boot new nodes
this fix allows the api to stay alive, hence submit node requests to register and avoid failing on starting rmb if node is still not registred
1 parent e952f47 commit 78cd102

1 file changed

Lines changed: 37 additions & 19 deletions

File tree

cmds/modules/api_gateway/main.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/hex"
77
"fmt"
88

9-
"github.com/pkg/errors"
9+
"github.com/cenkalti/backoff/v3"
1010
"github.com/rs/zerolog/log"
1111
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
1212
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
@@ -75,6 +75,23 @@ func action(cli *cli.Context) error {
7575
}
7676

7777
server.Register(zbus.ObjectID{Name: "api-gateway", Version: "0.0.1"}, gw)
78+
79+
ctx, _ := utils.WithSignal(context.Background())
80+
utils.OnDone(ctx, func(_ error) {
81+
log.Info().Msg("shutting down")
82+
})
83+
84+
go func() {
85+
for {
86+
if err := server.Run(ctx); err != nil && err != context.Canceled {
87+
log.Error().Err(err).Msg("unexpected error")
88+
continue
89+
}
90+
91+
break
92+
}
93+
}()
94+
7895
api, err := zosapi.NewZosAPI(manager, redis, msgBrokerCon)
7996
if err != nil {
8097
return fmt.Errorf("failed to create zos api: %w", err)
@@ -86,29 +103,30 @@ func action(cli *cli.Context) error {
86103
return err
87104
}
88105

89-
ctx, _ := utils.WithSignal(context.Background())
90-
utils.OnDone(ctx, func(_ error) {
91-
log.Info().Msg("shutting down")
92-
})
93-
_, err = peer.NewPeer(
94-
ctx,
95-
hex.EncodeToString(pair.Seed()),
96-
manager,
97-
router.Serve,
98-
peer.WithKeyType(peer.KeyTypeEd25519),
99-
peer.WithRelay(environment.MustGet().RelayURL...),
100-
)
101-
if err != nil {
102-
return fmt.Errorf("failed to start a new rmb peer: %w", err)
103-
}
106+
bo := backoff.NewExponentialBackOff()
107+
bo.MaxElapsedTime = 0
108+
backoff.Retry(func() error {
109+
_, err = peer.NewPeer(
110+
ctx,
111+
hex.EncodeToString(pair.Seed()),
112+
manager,
113+
router.Serve,
114+
peer.WithKeyType(peer.KeyTypeEd25519),
115+
peer.WithRelay(environment.MustGet().RelayURL...),
116+
)
117+
if err != nil {
118+
return fmt.Errorf("failed to start a new rmb peer: %w", err)
119+
}
120+
121+
return nil
122+
}, bo)
104123

105124
log.Info().
106125
Str("broker", msgBrokerCon).
107126
Uint("worker nr", workerNr).
108127
Msg("starting api-gateway module")
109128

110-
if err := server.Run(ctx); err != nil && err != context.Canceled {
111-
return errors.Wrap(err, "unexpected error")
112-
}
129+
// block forever
130+
<-ctx.Done()
113131
return nil
114132
}

0 commit comments

Comments
 (0)