From bbdcedf6deeb9eb471d8705924e3fe4e1da70262 Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Mon, 6 Nov 2023 21:13:24 +0100 Subject: [PATCH] #2 match owned nodes with pending/running tasks --- cmd/woodpecker-autoscaler.go | 4 ++-- internal/woodpecker/metrics.go | 32 ++++++++++++-------------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/cmd/woodpecker-autoscaler.go b/cmd/woodpecker-autoscaler.go index 0091f48..001dc91 100644 --- a/cmd/woodpecker-autoscaler.go +++ b/cmd/woodpecker-autoscaler.go @@ -51,7 +51,7 @@ func main() { log.WithFields(log.Fields{ "Caller": "Main", }).Infof("Currently owning %d Agents", len(ownedNodes)) - if pendingTasks { + if pendingTasks < len(ownedNodes) { server, err := hetzner.CreateNewAgent(cfg) if err != nil { log.WithFields(log.Fields{ @@ -86,7 +86,7 @@ func main() { "Caller": "Main", }).Fatal(fmt.Sprintf("Error checking woodpecker queue: %s", err.Error())) } - if runningTasks { + if runningTasks <= len(ownedNodes) { log.WithFields(log.Fields{ "Caller": "Main", }).Info("Still found running tasks. No agent to be removed") diff --git a/internal/woodpecker/metrics.go b/internal/woodpecker/metrics.go index 43dc92b..ba9a816 100644 --- a/internal/woodpecker/metrics.go +++ b/internal/woodpecker/metrics.go @@ -35,56 +35,48 @@ func QueueInfo(cfg *config.Config, target interface{}) error { return json.NewDecoder(resp.Body).Decode(target) } -func CheckPending(cfg *config.Config) (bool, error) { +func CheckPending(cfg *config.Config) (int, error) { expectedKV := strings.Split(cfg.WoodpeckerLabelSelector, "=") queueInfo := new(models.QueueInfo) err := QueueInfo(cfg, queueInfo) if err != nil { - return false, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error())) + return 0, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error())) } + count := 0 if queueInfo.Stats.PendingCount > 0 { if queueInfo.Pending != nil { for _, pendingJobs := range queueInfo.Pending { val, exists := pendingJobs.Labels[expectedKV[0]] if exists && val == expectedKV[1] { + count++ log.WithFields(log.Fields{ "Caller": "CheckPending", - }).Info("Found pending job for us") - return true, nil - } else { - log.WithFields(log.Fields{ - "Caller": "CheckPending", - }).Info("No Jobs for us in Queue") - return false, nil + }).Debugf("Currently serving %d Jobs", count) } } } } - return false, nil + return count, nil } -func CheckRunning(cfg *config.Config) (bool, error) { +func CheckRunning(cfg *config.Config) (int, error) { expectedKV := strings.Split(cfg.WoodpeckerLabelSelector, "=") queueInfo := new(models.QueueInfo) err := QueueInfo(cfg, queueInfo) if err != nil { - return false, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error())) + return 0, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error())) } + count := 0 if queueInfo.Stats.RunningCount > 0 { for _, runningJobs := range queueInfo.Running { val, exists := runningJobs.Labels[expectedKV[0]] if exists && val == expectedKV[1] { + count++ log.WithFields(log.Fields{ "Caller": "CheckRunning", - }).Info("Found running job for us") - return true, nil - } else { - log.WithFields(log.Fields{ - "Caller": "CheckRunning", - }).Info("No running job for us") - return false, nil + }).Debugf("Currently serving %d Jobs", count) } } } - return false, nil + return count, nil }