From 964874dedc644ab20a8eb5c983e9d4c881b58652 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Mon, 23 Sep 2024 00:03:16 +0530 Subject: [PATCH] fix: start job history channel fetch before adding items --- job/job.go | 16 +++++++++++++++- upstream/controllers.go | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/job/job.go b/job/job.go index 1b833fe7..64d05bed 100644 --- a/job/job.go +++ b/job/job.go @@ -39,7 +39,21 @@ const ( maxJitterDuration = time.Minute * 15 ) -var EvictedJobs chan uuid.UUID +var ( + EvictedJobs chan uuid.UUID + startedJobHistory = false +) + +func StartJobHistoryEvictor(ctx context.Context) { + if !startedJobHistory { + if EvictedJobs == nil { + EvictedJobs = make(chan uuid.UUID, 1000) + } + go deleteEvictedJobs(ctx) + startedJobHistory = true + } + +} // deleteEvictedJobs deletes job_history rows from the DB every job.eviction.period(1m), // jobs send rows to be deleted by maintaining a circular buffer by status type diff --git a/upstream/controllers.go b/upstream/controllers.go index 0e250e75..70a9b82c 100644 --- a/upstream/controllers.go +++ b/upstream/controllers.go @@ -9,6 +9,7 @@ import ( "github.com/flanksource/commons/logger" "github.com/flanksource/duty/api" "github.com/flanksource/duty/context" + "github.com/flanksource/duty/job" "github.com/flanksource/duty/models" "github.com/labstack/echo/v4" "github.com/patrickmn/go-cache" @@ -112,6 +113,7 @@ func addJobHistoryToRing(ctx context.Context, agentID string, histories []models if ringManager == nil { return } + job.StartJobHistoryEvictor(ctx) for _, history := range histories { ringManager.Add(ctx, agentID, history)