From: Michael Tremer Date: Thu, 6 Feb 2025 17:52:05 +0000 (+0000) Subject: jobs: Flush and refresh to read the finished timestamp X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d64e226747d6dd07429785fc213332bdc3694a47;p=pbs.git jobs: Flush and refresh to read the finished timestamp This feels like a stupid hack but this at least prevents utter mayhem happening after this. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/database.py b/src/buildservice/database.py index 8238d25f..cb0173a5 100644 --- a/src/buildservice/database.py +++ b/src/buildservice/database.py @@ -426,6 +426,26 @@ class Connection(object): # Commit! await session.commit() + async def flush(self, *objects): + """ + Manually triggers a flush + """ + # Fetch our session + session = await self.session() + + # Flush! + await session.flush(objects) + + async def refresh(self, o): + """ + Refreshes the given object + """ + # Fetch our session + session = await self.session() + + # Refresh! + await session.refresh(o) + class BackendMixin: @functools.cached_property diff --git a/src/buildservice/jobs.py b/src/buildservice/jobs.py index 008d2bf1..a34d0799 100644 --- a/src/buildservice/jobs.py +++ b/src/buildservice/jobs.py @@ -666,6 +666,13 @@ 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) + # Import log if logfile: await self._import_logfile(logfile) @@ -747,13 +754,10 @@ class Job(database.Base, database.BackendMixin, database.SoftDeleteMixin): """ Returns True if this job has finished, been aborted, etc. """ - # This check is checking if finished_at is set because SQLAlchemy is behaving - # a little bit stupid when we set finished_at to sqlalchemy.func.current_timestamp() - # because since this is a server function, it cannot know the value before we commit. - if self.finished_at is None: - return False + if self.finished_at: + return True - return True + return False # Failed