Skip to content

Commit

Permalink
Merge pull request #872 from MolSSI/svc_sort
Browse files Browse the repository at this point in the history
Simplify & fix sorting of task queue for services
  • Loading branch information
bennybp authored Dec 21, 2024
2 parents 4bd800c + 256228b commit 011be51
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 25 deletions.
29 changes: 5 additions & 24 deletions qcfractal/qcfractal/components/record_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,31 +1189,12 @@ def iterate_service(self, session: Session, service_orm: ServiceQueueORM) -> boo
# The sort date should almost always be the created_on of the parent service
# This way, earlier services will have their tasks run first, rather than having to wait
# for all other services' tasks to finish
# But, it should be the earliest time for all services that depend on that record

br = aliased(BaseRecordORM) # BaseRecord for a dependent record
br_svc = aliased(BaseRecordORM) # BaseRecord for the parent service(s)

earliest = func.least(br.created_on, func.min(br_svc.created_on)).label("created_on")
stmt = select(br.id.label("record_id"), earliest)
stmt = stmt.join(ServiceDependencyORM, ServiceDependencyORM.record_id == br.id, isouter=True)
stmt = stmt.join(ServiceQueueORM, ServiceQueueORM.id == ServiceDependencyORM.service_id, isouter=True)
stmt = stmt.join(br_svc, br_svc.id == ServiceQueueORM.record_id, isouter=True)
stmt = stmt.where(ServiceQueueORM.id == service_orm.id)
stmt = stmt.group_by(br.id)

least_dates = session.execute(stmt).all()
least_dates = {x[0]: x[1] for x in least_dates}

# But, it could be the original submission date of the record
parent_created_on = service_orm.record.created_on
for svc_record in service_orm.dependencies:
# print("HERE", svc_record.record_id, svc_record.record.status, svc_record.record.task)
if svc_record.record_id not in least_dates:
continue

if svc_record.record.is_service:
raise RuntimeError("Cannot have a service as a dependency of a service (yet)")
elif svc_record.record.task is not None:
svc_record.record.task.sort_date = least_dates[svc_record.record_id]
if svc_record.record.status != RecordStatusEnum.complete and svc_record.record.task is not None:
record_sort_date = svc_record.record.task.sort_date
svc_record.record.task.sort_date = min(record_sort_date, parent_created_on)

return finished

Expand Down
2 changes: 1 addition & 1 deletion qcfractal/qcfractal/components/tasks/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TaskQueueORM(BaseORM):
# when claiming tasks don't work
required_programs = Column(ARRAY(TEXT), nullable=False)

sort_date = Column(TIMESTAMP(timezone=True), default=now_at_utc(), nullable=False)
sort_date = Column(TIMESTAMP(timezone=True), default=now_at_utc, nullable=False)
tag = Column(String, nullable=False)
priority = Column(Integer, nullable=False)
available = Column(Boolean, nullable=False)
Expand Down

0 comments on commit 011be51

Please sign in to comment.