From d62e3dd855cd2a62ced63eaf589621ed04a2bace Mon Sep 17 00:00:00 2001 From: shohamroditimemphis Date: Wed, 20 Dec 2023 13:40:29 +0200 Subject: [PATCH 1/2] audit-logs-cleaning-background-task --- db/db.go | 20 ++++++++++++++++++++ server/background_tasks.go | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/db/db.go b/db/db.go index 677a1846a..2ac8a38d7 100644 --- a/db/db.go +++ b/db/db.go @@ -1099,6 +1099,26 @@ func RemoveAuditLogsByTenant(tenantName string) error { return nil } +func RemoveAuditLogsByTenantAndCreatedAt(tenantName string, createdAt time.Time) error { + ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) + defer cancelfunc() + conn, err := MetadataDbClient.Client.Acquire(ctx) + if err != nil { + return err + } + defer conn.Release() + query := `DELETE FROM audit_logs WHERE tenant_name = $1 AND created_at < $2` + stmt, err := conn.Conn().Prepare(ctx, "remove_audit_logs_by_tenant_and_created_at", query) + if err != nil { + return err + } + _, err = conn.Conn().Query(ctx, stmt.Name, tenantName, createdAt) + if err != nil { + return err + } + return nil +} + // Station Functions func GetActiveStationsPerTenant(tenantName string) ([]models.Station, error) { ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) diff --git a/server/background_tasks.go b/server/background_tasks.go index 0480f6769..d2b523ca7 100644 --- a/server/background_tasks.go +++ b/server/background_tasks.go @@ -625,6 +625,11 @@ func (s *Server) RemoveOldProducersAndConsumers() { if err != nil { serv.Errorf("[tenant: %v]RemoveOldProducersAndConsumers at DeleteOldProducersAndConsumers : %v", tenantName, err.Error()) } + time := time.Now().Add(-time.Hour * 3 * 24) + err = db.RemoveAuditLogsByTenantAndCreatedAt(tenantName, time) + if err != nil { + serv.Errorf("[tenant: %v]RemoveOldProducersAndConsumers at RemoveAuditLogsByTenantAndCreatedAt : %v", tenantName, err.Error()) + } } } } From 034fe980927dd60dc0da9253d3770aaccf986a77 Mon Sep 17 00:00:00 2001 From: shohamroditimemphis Date: Wed, 20 Dec 2023 16:41:33 +0200 Subject: [PATCH 2/2] change name to RemoveOldProducersAndConsumersAndAuditLogs --- server/background_tasks.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/background_tasks.go b/server/background_tasks.go index d2b523ca7..81ca2e682 100644 --- a/server/background_tasks.go +++ b/server/background_tasks.go @@ -343,7 +343,7 @@ func (s *Server) StartBackgroundTasks() error { go s.InitializeThroughputSampling() go s.UploadTenantUsageToDB() go s.RefreshFirebaseFunctionsKey() - go s.RemoveOldProducersAndConsumers() + go s.RemoveOldProducersAndConsumersAndAuditLogs() go ScheduledCloudCacheRefresh() go s.SendBillingAlertWhenNeeded() go s.CheckBrokenConnectedIntegrations() @@ -616,19 +616,19 @@ func (s *Server) RemoveOldDlsMsgs() { } } -func (s *Server) RemoveOldProducersAndConsumers() { +func (s *Server) RemoveOldProducersAndConsumersAndAuditLogs() { ticker := time.NewTicker(15 * time.Minute) for range ticker.C { for tenantName, rt := range s.opts.GCProducersConsumersRetentionHours { configurationTime := time.Now().Add(time.Hour * time.Duration(-rt)) err := db.DeleteOldProducersAndConsumers(configurationTime, tenantName) if err != nil { - serv.Errorf("[tenant: %v]RemoveOldProducersAndConsumers at DeleteOldProducersAndConsumers : %v", tenantName, err.Error()) + serv.Errorf("[tenant: %v]RemoveOldProducersAndConsumersAndAuditLogs at DeleteOldProducersAndConsumers : %v", tenantName, err.Error()) } time := time.Now().Add(-time.Hour * 3 * 24) err = db.RemoveAuditLogsByTenantAndCreatedAt(tenantName, time) if err != nil { - serv.Errorf("[tenant: %v]RemoveOldProducersAndConsumers at RemoveAuditLogsByTenantAndCreatedAt : %v", tenantName, err.Error()) + serv.Errorf("[tenant: %v]RemoveOldProducersAndConsumersAndAuditLogs at RemoveAuditLogsByTenantAndCreatedAt : %v", tenantName, err.Error()) } } }