return False
- def is_idle(self):
- """
- Return True if the builder has no active jobs
- """
- if self.jobs:
- return False
-
- return True
-
# Maintenance
maintenance = Column(Boolean, nullable=False, default=False)
# Running Jobs
- async def get_running_jobs(self):
- stmt = (
+ @functools.cached_property
+ def running_jobs(self):
+ """
+ A CTE with all jobs currently running on this builder
+ """
+ return (
sqlalchemy
.select(
jobs.Job,
)
+ .select_from(
+ self.backend.jobs.running_jobs,
+ )
.where(
- jobs.Job.deleted_at == None,
- jobs.Job.started_at != None,
- jobs.Job.finished_at == None,
jobs.Job.builder == self,
)
- .order_by(
- jobs.Job.started_at.desc(),
+ .cte("builder_running_jobs")
+ )
+
+ async def get_running_jobs(self):
+ """
+ Returns the currently running jobs
+ """
+ stmt = (
+ sqlalchemy
+ .select(
+ jobs.Job,
+ )
+ .select_from(
+ self.running_jobs,
)
)
return await self.db.fetch_as_list(stmt)
+ async def num_running_jobs(self):
+ """
+ Returns the number of currently running jobs
+ """
+ stmt = (
+ sqlalchemy
+ .select(
+ sqlalchemy.func.count().label("running_jobs"),
+ )
+ .select_from(
+ self.running_jobs,
+ )
+ )
+
+ # Run the query
+ return await self.db.select_one(stmt, "running_jobs") or 0
+
# Max Jobs
max_jobs = Column(Integer, nullable=False, default=1)
- def is_full(self):
+ async def is_full(self):
"""
Returns True if the builder has enough jobs running
"""
- return len(self.jobs) >= self.max_jobs
+ return await self.num_running_jobs() >= self.max_jobs
+
+ async def is_idle(self):
+ """
+ Return True if the builder has no active jobs
+ """
+ return await self.num_running_jobs() == 0
# Name
return await self.db.fetch_one(stmt)
- def get_running(self):
+ @functools.cached_property
+ def running_jobs(self):
"""
- Returns all currently running jobs
+ Returns a CTE with all currently running jobs
"""
- stmt = (
+ return (
sqlalchemy
- .select(Job)
+ .select(
+ Job,
+ )
.where(
Job.deleted_at == None,
Job.started_at != None,
.order_by(
Job.started_at.desc(),
)
+ .cte("running_jobs")
+ )
+
+ def get_running(self):
+ """
+ Returns all currently running jobs
+ """
+ stmt = (
+ sqlalchemy
+ .select(
+ Job,
+ )
+ .select_from(
+ self.running_jobs,
+ )
)
return self.backend.db.fetch(stmt)