Reconnect instead of exit when NATS is not available
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Adam Štrauch 2021-12-25 10:38:13 +01:00
parent 0e836b68a8
commit 55b3376d4c
Signed by: cx
GPG Key ID: 018304FFA8988F8D
3 changed files with 18 additions and 7 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
.history/
# Files with secrets
*secret*
tmp/

View File

@ -35,7 +35,7 @@ func init() {
discoveryStorage.LogChannel = make(chan string)
discoveryStorage.TTL = config.TTL
// localhost initization
// localhost initiation
localHost = server.LocalHost{
LabelsPath: config.LabelsPath,
HostnameOverride: config.HostName,

View File

@ -3,7 +3,9 @@ package nats_driver
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/by-cx/lobby/common"
"github.com/by-cx/lobby/server"
@ -53,13 +55,20 @@ func (d *Driver) Init() error {
return fmt.Errorf("please initiate LogChannel variable")
}
nc, err := nats.Connect(d.NATSUrl)
if err != nil {
return err
}
d.nc = nc
var nc *nats.Conn
_, err = nc.Subscribe(d.NATSDiscoveryChannel, d.handler)
for {
nc, err := nats.Connect(d.NATSUrl)
if err != nil {
log.Println("Can't connect to the NATS server, waiting for 5 seconds before I try it again.")
time.Sleep(time.Second * 5)
continue
}
d.nc = nc
break
}
_, err := nc.Subscribe(d.NATSDiscoveryChannel, d.handler)
if err != nil {
return err
}