Bopytex/bopytex/scheduler.py

73 lines
1.8 KiB
Python
Raw Normal View History

""" Scheduler for action to make """
2022-04-09 04:58:44 +00:00
from bopytex.tasks import Task
from bopytex.worker import Dispatcher
2022-04-09 04:56:57 +00:00
class Scheduler:
2022-04-09 19:46:24 +00:00
"""Scheduler is responsible of getting tasks (the tasks) and yield those that can be done"""
def __init__(self, dispatcher:Dispatcher, done: list[str] = None):
self._dispatcher = dispatcher
2022-04-09 04:56:57 +00:00
if done is None:
self._done = []
else:
self._done = done
self._tasks = []
@property
2022-04-09 19:23:28 +00:00
def tasks(self) -> list[Task]:
2022-04-09 19:46:24 +00:00
"""List all the tasks todo"""
return self._tasks
2022-04-09 15:08:05 +00:00
@property
2022-04-09 19:23:28 +00:00
def doable_tasks(self) -> list[Task]:
2022-04-09 19:46:24 +00:00
"""List all doable tasks"""
2022-04-09 15:08:05 +00:00
return [
task
for task in self.tasks
if not task.deps or all([d in self.done for d in task.deps])
]
2022-04-09 04:56:57 +00:00
@property
2022-04-13 09:29:30 +00:00
def all_deps(self) -> set[str]:
2022-04-09 19:46:24 +00:00
"""List dependencies of all tasks"""
2022-04-09 04:56:57 +00:00
return {d for task in self.tasks for d in task.deps}
@property
2022-04-13 09:29:30 +00:00
def all_output(self) -> set[str]:
2022-04-09 19:46:24 +00:00
"""List ouput of all tasks"""
2022-04-09 04:56:57 +00:00
return {task.output for task in self.tasks}
@property
2022-04-09 19:23:28 +00:00
def done(self) -> list[str]:
return self._done
2022-04-09 04:56:57 +00:00
def append(self, tasks: list[Task]):
self._tasks += tasks
2022-04-09 19:46:24 +00:00
def is_finishable(self):
return self.all_deps.issubset(self.all_output)
2022-04-08 20:04:47 +00:00
2022-04-09 19:46:24 +00:00
def next_task(self):
2022-04-08 20:04:47 +00:00
try:
2022-04-09 19:23:28 +00:00
task = self.doable_tasks[0]
2022-04-08 20:04:47 +00:00
except IndexError:
raise StopIteration
2022-04-09 19:23:28 +00:00
self._tasks.remove(task)
message = self._dispatcher(task)
if message.status == 0:
self._done.append(task.output)
return message
2022-04-09 04:56:57 +00:00
2022-04-09 19:46:24 +00:00
def backlog(self):
""" Yield tasks sorted according to dependencies """
while self.doable_tasks:
yield self.next_task()