]> git.ipfire.org Git - pbs.git/commitdiff
builders: Refactor dispatching jobs
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 14:01:32 +0000 (14:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 26 Jan 2025 14:01:32 +0000 (14:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/builders.py
src/buildservice/jobs.py

index ba8fb2a0f844a0cfccd5ce5969a86decce99fc91..687e728b858c2abb75015f540918c8e102273f3f 100644 (file)
@@ -4,6 +4,7 @@ import asyncio
 import botocore.exceptions
 import datetime
 import functools
+import json
 import logging
 
 import sqlalchemy
@@ -909,42 +910,22 @@ class Builder(database.Base, database.BackendMixin, database.SoftDeleteMixin):
 
        connected_from = Column(INET)
 
-       def dispatch_job(self, job):
-               # Throw an error if the builder isn't online any more
-               if not self.is_online():
-                       raise BuilderNotOnlineError
+       # Send Message
 
-               log.debug("Dispatching build job %s to %s" % (job, self))
-
-               # Assign this builder to the job
-               job.assign(builder=self)
-
-               # Send a message to the builder
-               self.connection.write_message({
-                       "type" : "job",
-                       "data" : {
-                               # Add job information
-                               "id"      : "%s" % job.uuid,
-                               "name"    : "%s" % job,
-                               "arch"    : job.arch,
-
-                               # ccache path
-                               "ccache"  : {
-                                       "enabled" : job.ccache_enabled,
-                                       "path"    : job.ccache_path,
-                               },
-
-                               # Is this a test job?
-                               "test"    : job.is_test(),
-
-                               # Send the pakfire configuration without using any mirrors
-                               "conf"    : "%s" % job.pakfire(build=True, mirrored=False),
+       async def send_message(self, message):
+               """
+                       Sends a message to the builder
+               """
+               # Raise an error if the builder is not online
+               if not self.connection:
+                       raise BuilderNotOnlineError(self)
 
-                               # URL to the package
-                               "pkg"     : job.pkg.download_url,
-                       },
-               })
+               # Log action
+               log.debug("Sending message to %s:\n%s",
+                       self, json.dumps(message, indent=4, sort_keys=True))
 
+               # Write the message to the control connection
+               return self.connection.write_message(message)
 
        # Uploads
 
index f28a527646e4d91d9de686ba7be727970b5ce822..3020460ed3cced3a75dd8d6d59ceb9ade5e318e9 100644 (file)
@@ -372,7 +372,7 @@ class Queue(base.Object):
                                                                continue
 
                                                        # If we have a job, we dispatch it to the builder
-                                                       builder.dispatch_job(job)
+                                                       await job.dispatch(builder)
 
                                                        # Once we dispatched a job, we are done
                                                        break
@@ -641,18 +641,51 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin):
                # Return the remaining time
                return self.timeout - self.duration
 
-       def assign(self, builder):
+       async def dispatch(self, builder):
                """
-                       Assigns this job to a builder
+                       Called to dispatch this job to the given builder
                """
-               log.info("Starting job %s on %s" % (self, builder))
+               log.debug("Dispatching %s to %s" % (self, builder))
 
-               # Store the assigned builder
+               # Store the builder
                self.builder = builder
 
                # Store the time
                self.started_at = sqlalchemy.func.current_timestamp()
 
+               # Launch Pakfire in build mode without any mirrors
+               pakfire = self.pakfire(build=True, mirrored=False)
+
+               # Generate the configuration file
+               config = await pakfire.config()
+
+               # Send a message to the builder
+               await builder.send_message({
+                       "type" : "job",
+
+                       "data" : {
+                               # Add job information
+                               "id"      : "%s" % self.uuid,
+                               "name"    : "%s" % self,
+                               "arch"    : self.arch,
+
+                               # ccache path
+                               "ccache"  : {
+                                       "enabled" : self.ccache_enabled,
+                                       "path"    : self.ccache_path,
+                               },
+
+                               # Is this a test job?
+                               "test"    : self.is_test(),
+
+                               # Configuration
+                               "config"  : config.getvalue(),
+
+                               # URL to the package
+                               "pkg"     : self.pkg.download_url,
+                       },
+               })
+
        def connected(self, connection):
                """
                        Called when a builder has connected