]> git.ipfire.org Git - people/jschlag/pbs.git/blobdiff - src/buildservice/jobqueue.py
Add command to create test jobs
[people/jschlag/pbs.git] / src / buildservice / jobqueue.py
index 23f6d4fbbc3ddbd11254cfa192ef038a730c2f09..f8032719fed4587cdbb650bae80ba722be84b6cf 100644 (file)
@@ -1,7 +1,13 @@
 #!/usr/bin/python
 
+import datetime
+import logging
+
 from . import base
 
+log = logging.getLogger("jobqueue")
+log.propagate = 1
+
 PENDING_STATE = "pending"
 
 class JobQueue(base.Object):
@@ -38,4 +44,36 @@ class JobQueue(base.Object):
                res = self.db.get("SELECT AVG(NOW() - COALESCE(jobs.start_not_before, jobs.time_created)) AS avg \
                        FROM jobs_queue queue LEFT JOIN jobs ON queue.job_id = jobs.id")
 
-               return res.avg
\ No newline at end of file
+               return res.avg
+
+       def create_test_jobs(self):
+               max_queue_length = self.backend.settings.get_int("test_queue_limit", 25)
+
+               threshold_days = self.backend.settings.get_int("test_threshold_days", 14)
+               threshold = datetime.datetime.utcnow() - datetime.timedelta(days=threshold_days)
+
+               for arch in self.backend.arches:
+                       # Skip adding new jobs if there are more too many jobs in the queue.
+                       limit = max_queue_length - self.backend.jobqueue.get_length_for_arch(arch)
+                       if limit <= 0:
+                               log.debug("Already too many jobs in queue of %s to create tests." % arch)
+                               continue
+
+                       # Get a list of builds, with potentially need a test build.
+                       # Randomize the output and do not return more jobs than we are
+                       # allowed to put into the build queue.
+                       builds = self.backend.builds._get_builds("SELECT builds.* FROM builds \
+                               LEFT JOIN jobs ON builds.id = jobs.build_id \
+                               WHERE builds.type = %s AND builds.state = ANY(%s) AND jobs.state = %s \
+                                       AND NOT EXISTS (SELECT 1 FROM jobs test_jobs \
+                                               WHERE test_jobs.build_id = builds.id AND jobs.type = %s \
+                                                       AND (test_jobs.state <> %s OR test_jobs.state = %s AND test_jobs.time_finished >= %s)) LIMIT %s",
+                               "release", ["stable", "testing"], "finished", "test", "finished", "finished", threshold, limit)
+
+                       # Search for the job with the right architecture in each
+                       # build and schedule a test job.
+                       for build in builds:
+                               for job in build:
+                                       if job.arch == arch:
+                                               job.schedule("test")
+                                               break