Skip to content
Snippets Groups Projects
Commit 3ac0dbe5 authored by André Sterba's avatar André Sterba Committed by Neil-Jocelyn Schark
Browse files

Don't crash on not working database connection


See merge request !902

Co-authored-by: default avatarNeil-Jocelyn Schark <neil.schark@h-da.de>
Co-authored-by: default avatarAndré Sterba <andre@sterba.dev>
parent 473a3663
No related branches found
No related tags found
1 merge request!902Don't crash on not working database connection
Pipeline #203640 passed
...@@ -99,7 +99,10 @@ func initialize() error { ...@@ -99,7 +99,10 @@ func initialize() error {
return err return err
} }
db := database.GetDatabaseConnection() db, err := database.GetDatabaseConnection()
if err != nil {
return err
}
nodeService := nodes.NewNodeService(nodes.NewNodeStore(db), eventService) nodeService := nodes.NewNodeService(nodes.NewNodeStore(db), eventService)
portService := ports.NewPortService(ports.NewPortStore(db), eventService) portService := ports.NewPortService(ports.NewPortStore(db), eventService)
......
...@@ -3,7 +3,6 @@ package database ...@@ -3,7 +3,6 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"time" "time"
"code.fbi.h-da.de/danet/gosdn/controller/config" "code.fbi.h-da.de/danet/gosdn/controller/config"
...@@ -18,6 +17,9 @@ const ( ...@@ -18,6 +17,9 @@ const (
timeout = 5 * time.Second timeout = 5 * time.Second
// DatabaseName is the name of the mongoDB database used. // DatabaseName is the name of the mongoDB database used.
DatabaseName = "gosdn" DatabaseName = "gosdn"
connectionRetries = 60
connectionTimeout = time.Second * 2
) )
// Connect Retrieves a client to the MongoDB. // Connect Retrieves a client to the MongoDB.
...@@ -28,15 +30,13 @@ func Connect() (*mongo.Database, error) { ...@@ -28,15 +30,13 @@ func Connect() (*mongo.Database, error) {
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoConnection)) client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoConnection))
if err != nil { if err != nil {
log.Printf("Failed to create client: %v", err) return nil, fmt.Errorf("failed to create client: %w", err)
return nil, err
} }
// Force a connection to verify our connection string // Force a connection to verify our connection string
err = client.Ping(ctx, nil) err = client.Ping(ctx, nil)
if err != nil { if err != nil {
log.Printf("Failed to connect to database: %v\n", err) return nil, fmt.Errorf("failed to connect to database: %w", err)
return nil, err
} }
db := client.Database(DatabaseName) db := client.Database(DatabaseName)
...@@ -47,18 +47,25 @@ func Connect() (*mongo.Database, error) { ...@@ -47,18 +47,25 @@ func Connect() (*mongo.Database, error) {
return db, nil return db, nil
} }
func GetDatabaseConnection() *mongo.Database { func GetDatabaseConnection() (*mongo.Database, error) {
var db *mongo.Database var db *mongo.Database
storeMode := store.GetStoreMode() storeMode := store.GetStoreMode()
if storeMode == store.Database {
db, err := Connect() switch storeMode {
if err != nil { case store.Database:
logrus.Infof("Could not connect to database") for i := 0; i < connectionRetries; i++ {
db, err := Connect()
if err == nil {
return db, nil
}
logrus.Errorf("could not connect to mongo with error: %s. Retrying in 2 seconds.", err.Error())
time.Sleep(connectionTimeout)
} }
return db return nil, fmt.Errorf("could not connect to mongo after %d retries", connectionRetries)
}
return db default:
return db, nil
}
} }
...@@ -122,7 +122,7 @@ func (s *DatabaseNetworkElementStore) Add(ctx context.Context, networkElementToA ...@@ -122,7 +122,7 @@ func (s *DatabaseNetworkElementStore) Add(ctx context.Context, networkElementToA
func (s *DatabaseNetworkElementStore) Update(ctx context.Context, networkElementToUpdate networkelement.NetworkElement) (err error) { func (s *DatabaseNetworkElementStore) Update(ctx context.Context, networkElementToUpdate networkelement.NetworkElement) (err error) {
var updatedLoadedNetworkElement networkelement.LoadedNetworkElement var updatedLoadedNetworkElement networkelement.LoadedNetworkElement
db, err := database.Connect() db, err := database.GetDatabaseConnection()
if err != nil { if err != nil {
return err return err
} }
......
...@@ -134,7 +134,7 @@ func (s *DatabaseUserStore) GetAll(ctx context.Context) (loadedUsers []rbac.Load ...@@ -134,7 +134,7 @@ func (s *DatabaseUserStore) GetAll(ctx context.Context) (loadedUsers []rbac.Load
func (s *DatabaseUserStore) Update(ctx context.Context, userToUpdate rbac.User) (err error) { func (s *DatabaseUserStore) Update(ctx context.Context, userToUpdate rbac.User) (err error) {
var updatedLoadedUser rbac.LoadedUser var updatedLoadedUser rbac.LoadedUser
db, err := database.Connect() db, err := database.GetDatabaseConnection()
if err != nil { if err != nil {
return err return err
} }
......
...@@ -133,7 +133,7 @@ func (s *DatabaseNodeStore) Add(ctx context.Context, node Node) (err error) { ...@@ -133,7 +133,7 @@ func (s *DatabaseNodeStore) Add(ctx context.Context, node Node) (err error) {
func (s *DatabaseNodeStore) Update(ctx context.Context, node Node) (err error) { func (s *DatabaseNodeStore) Update(ctx context.Context, node Node) (err error) {
var updatedLoadedNodes Node var updatedLoadedNodes Node
db, err := database.Connect() db, err := database.GetDatabaseConnection()
if err != nil { if err != nil {
return err return err
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment