From 3bbec5ff0c0e016a92ca3d521a8f42d5a8d2e7ca Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Mar 01 2024 10:13:32 +0000
Subject: [PATCH 1/3] Basic host data for scheduler


Related: https://pagure.io/koji/issue/4030

---

diff --git a/koji/daemon.py b/koji/daemon.py
index 2b8e700..14f2368 100644
--- a/koji/daemon.py
+++ b/koji/daemon.py
@@ -1025,9 +1025,21 @@ class TaskManager(object):
                 else:
                     self.logger.info("Lingering task %r (pid %r)" % (id, pid))
 
+    def _get_host_data(self):
+        data = {
+            'methods': list(self.handlers.keys()),
+            'maxjobs': self.options.maxjobs,
+            # TODO: now it would be duplicated by updateHost
+            #'ready': self.ready,
+            #'task_load': self.task_load,
+            #cpu_load, free_mem, free_disk, ...
+        }
+        return data
+
     def getNextTask(self):
         self.ready = self.readyForTask()
         self.session.host.updateHost(self.task_load, self.ready)
+        self.session.host.setHostData(self._get_host_data())
         if not self.ready:
             self.logger.info("Not ready for task")
             return False

From ed63d144237b088e1ac0bdba01671147227caa32 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Mar 01 2024 10:24:21 +0000
Subject: [PATCH 2/3] json conversion


---

diff --git a/koji/daemon.py b/koji/daemon.py
index 14f2368..1fab163 100644
--- a/koji/daemon.py
+++ b/koji/daemon.py
@@ -24,6 +24,7 @@ from __future__ import absolute_import, division
 
 import errno
 import hashlib
+import json
 import logging
 import os
 import re
@@ -1039,7 +1040,7 @@ class TaskManager(object):
     def getNextTask(self):
         self.ready = self.readyForTask()
         self.session.host.updateHost(self.task_load, self.ready)
-        self.session.host.setHostData(self._get_host_data())
+        self.session.host.setHostData(json.dumps(self._get_host_data()))
         if not self.ready:
             self.logger.info("Not ready for task")
             return False

From 0ca6c6464ebf860f51547974dd62022b3db438a7 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Mar 04 2024 14:49:18 +0000
Subject: [PATCH 3/3] Use also builder's maxjobs setting in scheduler


Related: https://pagure.io/koji/issue/4038

---

diff --git a/kojihub/scheduler.py b/kojihub/scheduler.py
index b35d4d6..f3c26e0 100644
--- a/kojihub/scheduler.py
+++ b/kojihub/scheduler.py
@@ -298,6 +298,7 @@ class TaskScheduler(object):
             h_refused = refusals.get(task['task_id'], {})
             for host in self.hosts_by_bin.get(task['_bin'], []):
                 if (host['ready'] and host['_ntasks'] < self.maxjobs and
+                        host['_ntasks'] < host['data']['maxjobs'] and
                         host['capacity'] - host['_load'] > min_avail and
                         host['id'] not in h_refused):
                     task['_hosts'].append(host)
@@ -324,7 +325,8 @@ class TaskScheduler(object):
                          [(h['name'], "%(_rank).2f" % h) for h in task['_hosts']])
             for host in task['_hosts']:
                 if (host['capacity'] - host['_load'] > min_avail and
-                        host['_ntasks'] < self.maxjobs):
+                        host['_ntasks'] < self.maxjobs and
+                        host['_ntasks'] < host['data']['maxjobs']):
                     # add run entry
                     self.assign(task, host)
                     # update our totals and rank
@@ -535,6 +537,7 @@ class TaskScheduler(object):
             ('host.ready', 'ready'),
             ('host_config.arches', 'arches'),
             ('host_config.capacity', 'capacity'),
+            ('scheduler_host_data.data', 'data'),
         )
         fields, aliases = zip(*fields)
 
@@ -547,7 +550,8 @@ class TaskScheduler(object):
                 'host_config.active IS TRUE',
             ],
             joins=[
-                'host_config ON host.id = host_config.host_id'
+                'LEFT JOIN host_config ON host.id = host_config.host_id',
+                'LEFT JOIN scheduler_host_data ON host.id = scheduler_host_data.host_id',
             ]
         )
 

