Skip to content

Commit

Permalink
Merge pull request #376 from jmpsec/admin-backend-reconnect
Browse files Browse the repository at this point in the history
Adding db connection retry
  • Loading branch information
javuto authored Dec 10, 2023
2 parents 3abd819 + 32c4fe5 commit 2463595
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
serviceDescription string = "Admin service for osctrl"
// Application description
appDescription string = serviceDescription + ", a fast and efficient osquery management"
// Default timeout to attempt backend reconnect
defaultBackendRetryTimeout int = 7
)

// Paths
Expand Down Expand Up @@ -101,11 +103,6 @@ const (
defOsqueryTablesFile string = "data/" + defOsqueryTablesVersion + ".json"
)

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

// Global general variables
var (
err error
Expand Down Expand Up @@ -422,6 +419,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 @@ -618,10 +622,13 @@ func osctrlAdminService() {
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)
}
log.Println("Initializing cache...")
redis, err = cache.CreateRedisManager(redisConfig)
Expand Down

0 comments on commit 2463595

Please sign in to comment.