#2 match owned nodes with pending/running tasks
All checks were successful
Pipeline was successful

This commit is contained in:
Tobias Trabelsi 2023-11-06 21:13:24 +01:00
parent 98d48f006f
commit bbdcedf6de
Signed by: lerentis
GPG Key ID: FF0C2839718CAF2E
2 changed files with 14 additions and 22 deletions

View File

@ -51,7 +51,7 @@ func main() {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"Caller": "Main", "Caller": "Main",
}).Infof("Currently owning %d Agents", len(ownedNodes)) }).Infof("Currently owning %d Agents", len(ownedNodes))
if pendingTasks { if pendingTasks < len(ownedNodes) {
server, err := hetzner.CreateNewAgent(cfg) server, err := hetzner.CreateNewAgent(cfg)
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
@ -86,7 +86,7 @@ func main() {
"Caller": "Main", "Caller": "Main",
}).Fatal(fmt.Sprintf("Error checking woodpecker queue: %s", err.Error())) }).Fatal(fmt.Sprintf("Error checking woodpecker queue: %s", err.Error()))
} }
if runningTasks { if runningTasks <= len(ownedNodes) {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"Caller": "Main", "Caller": "Main",
}).Info("Still found running tasks. No agent to be removed") }).Info("Still found running tasks. No agent to be removed")

View File

@ -35,56 +35,48 @@ func QueueInfo(cfg *config.Config, target interface{}) error {
return json.NewDecoder(resp.Body).Decode(target) 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, "=") expectedKV := strings.Split(cfg.WoodpeckerLabelSelector, "=")
queueInfo := new(models.QueueInfo) queueInfo := new(models.QueueInfo)
err := QueueInfo(cfg, queueInfo) err := QueueInfo(cfg, queueInfo)
if err != nil { 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.Stats.PendingCount > 0 {
if queueInfo.Pending != nil { if queueInfo.Pending != nil {
for _, pendingJobs := range queueInfo.Pending { for _, pendingJobs := range queueInfo.Pending {
val, exists := pendingJobs.Labels[expectedKV[0]] val, exists := pendingJobs.Labels[expectedKV[0]]
if exists && val == expectedKV[1] { if exists && val == expectedKV[1] {
count++
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"Caller": "CheckPending", "Caller": "CheckPending",
}).Info("Found pending job for us") }).Debugf("Currently serving %d Jobs", count)
return true, nil
} else {
log.WithFields(log.Fields{
"Caller": "CheckPending",
}).Info("No Jobs for us in Queue")
return false, nil
} }
} }
} }
} }
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, "=") expectedKV := strings.Split(cfg.WoodpeckerLabelSelector, "=")
queueInfo := new(models.QueueInfo) queueInfo := new(models.QueueInfo)
err := QueueInfo(cfg, queueInfo) err := QueueInfo(cfg, queueInfo)
if err != nil { 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 { if queueInfo.Stats.RunningCount > 0 {
for _, runningJobs := range queueInfo.Running { for _, runningJobs := range queueInfo.Running {
val, exists := runningJobs.Labels[expectedKV[0]] val, exists := runningJobs.Labels[expectedKV[0]]
if exists && val == expectedKV[1] { if exists && val == expectedKV[1] {
count++
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"Caller": "CheckRunning", "Caller": "CheckRunning",
}).Info("Found running job for us") }).Debugf("Currently serving %d Jobs", count)
return true, nil
} else {
log.WithFields(log.Fields{
"Caller": "CheckRunning",
}).Info("No running job for us")
return false, nil
} }
} }
} }
return false, nil return count, nil
} }