Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding db connection retry in osctrl-api #377

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
defJWTConfigurationFile = "config/jwt.json"
// Default refreshing interval in seconds
defaultRefresh int = 300
// Default timeout to attempt backend reconnect
defaultBackendRetryTimeout int = 7
)

// Paths
Expand Down Expand Up @@ -86,11 +88,6 @@ const (
apiSettingsPath = "/settings"
)

var (
// Wait for backend in seconds
backendWait = 7 * time.Second
)

// Global variables
var (
err error
Expand Down Expand Up @@ -344,6 +341,13 @@ func init() {
EnvVars: []string{"DB_CONN_MAX_LIFETIME"},
Destination: &dbConfig.ConnMaxLifetime,
},
&cli.IntFlag{
Name: "db-conn-retry",
Value: defaultBackendRetryTimeout,
Usage: "Time in seconds to retry the connection to the database, if set to 0 the service will stop if the connection fails",
EnvVars: []string{"DB_CONN_RETRY"},
Destination: &dbConfig.ConnRetry,
},
&cli.BoolFlag{
Name: "tls",
Aliases: []string{"t"},
Expand Down Expand Up @@ -411,10 +415,13 @@ func osctrlAPIService() {
break
}
if err != nil {
log.Fatalf("Failed to connect to backend - %v", err)
log.Printf("Failed to connect to backend - %v", err)
if dbConfig.ConnRetry == 0 {
log.Fatalf("Connection to backend failed and no retry was set")
}
}
log.Println("Backend NOT ready! waiting...")
time.Sleep(backendWait)
log.Printf("Backend NOT ready! Retrying in %d seconds...\n", dbConfig.ConnRetry)
time.Sleep(time.Duration(dbConfig.ConnRetry) * time.Second)
}
// Redis - cache
redis, err = cache.CreateRedisManager(redisConfig)
Expand Down