From: Michael Tremer Date: Thu, 6 Feb 2025 20:30:27 +0000 (+0000) Subject: builds: Add a combined operation to flush and refresh X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=624a05161b3e6a64dc6d84904cc5ba28622debdb;p=pbs.git builds: Add a combined operation to flush and refresh Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/builds.py b/src/buildservice/builds.py index 113b57ea..c55263d4 100644 --- a/src/buildservice/builds.py +++ b/src/buildservice/builds.py @@ -699,6 +699,9 @@ class Build(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Mark as finished self.finished_at = sqlalchemy.func.current_timestamp() + # Flush and refresh + await self.db.flush_and_refresh(self) + # Mark as failed if the build was not successful if not success: self.failed = True diff --git a/src/buildservice/database.py b/src/buildservice/database.py index cb0173a5..d5ac2d61 100644 --- a/src/buildservice/database.py +++ b/src/buildservice/database.py @@ -446,6 +446,20 @@ class Connection(object): # Refresh! await session.refresh(o) + async def flush_and_refresh(self, *objects): + """ + Flushes and refreshes in one go. + """ + # Fetch our session + session = await self.session() + + # Flush! + await session.flush(objects) + + # Refresh! + for o in objects: + await session.refresh(o) + class BackendMixin: @functools.cached_property diff --git a/src/buildservice/jobs.py b/src/buildservice/jobs.py index 57e757ef..239cd2f6 100644 --- a/src/buildservice/jobs.py +++ b/src/buildservice/jobs.py @@ -664,12 +664,8 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin): # Store the time self.finished_at = sqlalchemy.func.current_timestamp() - # Flush to the database - await self.db.flush(self) - - # Read back the timestamp so that we won't trigger - # a read from synchronous code later. - await self.db.refresh(self) + # Flush to the database and read back the timestamp + await self.db.flush_and_refresh(self) # Import log if logfile: