Reconnect instead of exit when NATS is not available
All checks were successful
continuous-integration/drone/push Build is passing

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 # Files with secrets
*secret* *secret*
tmp/ tmp/

View File

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

View File

@ -3,7 +3,9 @@ package nats_driver
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"strings" "strings"
"time"
"github.com/by-cx/lobby/common" "github.com/by-cx/lobby/common"
"github.com/by-cx/lobby/server" "github.com/by-cx/lobby/server"
@ -53,13 +55,20 @@ func (d *Driver) Init() error {
return fmt.Errorf("please initiate LogChannel variable") return fmt.Errorf("please initiate LogChannel variable")
} }
nc, err := nats.Connect(d.NATSUrl) var nc *nats.Conn
if err != nil {
return err
}
d.nc = nc
_, 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 { if err != nil {
return err return err
} }