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

Retry connection for Redis/cache #381

Merged
merged 1 commit into from
Dec 18, 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
33 changes: 27 additions & 6 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
defaultRefresh int = 300
// Default timeout to attempt backend reconnect
defaultBackendRetryTimeout int = 7
// Default timeout to attempt redis reconnect
defaultRedisRetryTimeout int = 7
)

// Paths
Expand Down Expand Up @@ -269,6 +271,13 @@ func init() {
EnvVars: []string{"REDIS_DB"},
Destination: &redisConfig.DB,
},
&cli.IntFlag{
Name: "redis-conn-retry",
Value: defaultRedisRetryTimeout,
Usage: "Time in seconds to retry the connection to the cache, if set to 0 the service will stop if the connection fails",
EnvVars: []string{"REDIS_CONN_RETRY"},
Destination: &redisConfig.ConnRetry,
},
&cli.BoolFlag{
Name: "db",
Aliases: []string{"d"},
Expand Down Expand Up @@ -407,7 +416,8 @@ func init() {

// Go go!
func osctrlAPIService() {
// Backend
// ////////////////////////////// Backend
log.Println("Initializing backend...")
for {
db, err = backend.CreateDBManager(dbConfig)
if db != nil {
Expand All @@ -423,10 +433,22 @@ func osctrlAPIService() {
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)
if err != nil {
log.Fatalf("Failed to connect to redis - %v", err)
// ////////////////////////////// Cache
log.Println("Initializing cache...")
for {
redis, err = cache.CreateRedisManager(redisConfig)
if redis != nil {
log.Println("Connection to cache successful!")
break
}
if err != nil {
log.Printf("Failed to connect to cache - %v", err)
if redisConfig.ConnRetry == 0 {
log.Fatalf("Connection to cache failed and no retry was set")
}
}
log.Printf("Cache NOT ready! Retrying in %d seconds...\n", redisConfig.ConnRetry)
time.Sleep(time.Duration(redisConfig.ConnRetry) * time.Second)
}
log.Println("Initialize users")
apiUsers = users.CreateUserManager(db.Conn, &jwtConfig)
Expand All @@ -445,7 +467,6 @@ func osctrlAPIService() {
filecarves = carves.CreateFileCarves(db.Conn, apiConfig.Carver, nil)
log.Println("Loading service settings")
loadingSettings()

// Ticker to reload environments
// FIXME Implement Redis cache
// FIXME splay this?
Expand Down
Loading