Skip to content

Commit

Permalink
Configuration and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dehort committed Jul 12, 2024
1 parent 45d1bf7 commit 62b9299
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 273 deletions.
28 changes: 19 additions & 9 deletions cmd/cloud-connector/inventory_stale_timestamp_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"time"

"github.com/RedHatInsights/cloud-connector/internal/config"
Expand Down Expand Up @@ -36,6 +37,11 @@ func startInventoryStaleTimestampUpdater() {
logger.LogFatalError("Failed to connect to the database", err)
}

connectionRegistrar, err := connection_repository.NewSqlConnectionRegistrar(cfg, databaseConn)
if err != nil {
logger.LogFatalError("Failed to create connection registrar", err)
}

accountResolver, err := controller.NewAccountIdResolver(cfg.ClientIdToAccountIdImpl, cfg)
if err != nil {
logger.LogFatalError("Failed to create Account ID Resolver", err)
Expand Down Expand Up @@ -83,28 +89,32 @@ func startInventoryStaleTimestampUpdater() {

log.Debug("Processing stale connection")

identity, _, _, err := accountResolver.MapClientIdToAccountId(ctx, rhcClient.ClientID)
if rhcClient.TenantLookupFailureCount >= cfg.PurgeConnectionOnFailedTenantLookupCount {
logger.LogErrorWithAccountAndClientId("Tenant lookup failed for connection too many times. Purging connection...", err, rhcClient.Account, rhcClient.OrgID, rhcClient.ClientID)
connectionRegistrar.Unregister(ctx, rhcClient.ClientID)
return fmt.Errorf("Tenant lookup failed to many times")
}

identity, _, _, err := accountResolver.MapClientIdToAccountId(context.Background(), rhcClient.ClientID)
if err != nil {

rhcClient.TenantLookupFailureCount++
logger.LogErrorWithAccountAndClientId("Unable to retrieve identity for connection", err, rhcClient.Account, rhcClient.OrgID, rhcClient.ClientID)

connection_repository.RecordFailedTenantLookup(ctx, databaseConn, sqlTimeout, rhcClient)
dberr := connection_repository.RecordFailedTenantLookup(ctx, databaseConn, sqlTimeout, rhcClient)
if dberr != nil {
logger.LogErrorWithAccountAndClientId("Unable to record failed tenant lookup for connection", err, rhcClient.Account, rhcClient.OrgID, rhcClient.ClientID)
}

// FIXME: Send disconnect here?? Need to determine the type of failure!
logger.LogErrorWithAccountAndClientId("Unable to retrieve identity for connection", err, rhcClient.Account, rhcClient.OrgID, rhcClient.ClientID)
return err
}

// FIXME: reset the tenant lookup failure count
rhcClient.TenantLookupFailureCount = 0

err = connectedClientRecorder.RecordConnectedClient(ctx, identity, rhcClient)
if err != nil {
logger.LogErrorWithAccountAndClientId("Unable to sent host info to inventory", err, rhcClient.Account, rhcClient.OrgID, rhcClient.ClientID)
return err
}

connection_repository.UpdateStaleTimestampInDB(ctx, databaseConn, sqlTimeout, rhcClient)
connection_repository.RecordUpdatedStaleTimestamp(ctx, databaseConn, sqlTimeout, rhcClient)

return nil
})
Expand Down
Loading

0 comments on commit 62b9299

Please sign in to comment.