2022-04-08 19:24:43 +00:00
|
|
|
""" Scheduler for action to make """
|
|
|
|
|
|
|
|
|
2022-04-09 04:58:44 +00:00
|
|
|
from bopytex.tasks import Task
|
2022-04-13 13:09:08 +00:00
|
|
|
from bopytex.worker import Dispatcher
|
2022-04-09 04:56:57 +00:00
|
|
|
|
|
|
|
|
2022-04-08 19:24:43 +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"""
|
|
|
|
|
2022-07-28 07:39:51 +00:00
|
|
|
def __init__(self, dispatcher: Dispatcher, output_done: list[str] = None):
|
2022-04-13 13:09:08 +00:00
|
|
|
self._dispatcher = dispatcher
|
2022-04-09 04:56:57 +00:00
|
|
|
|
2022-04-13 13:20:34 +00:00
|
|
|
if output_done is None:
|
|
|
|
self._output_done = []
|
2022-04-09 04:56:57 +00:00
|
|
|
else:
|
2022-04-13 13:20:34 +00:00
|
|
|
self._output_done = output_done
|
2022-04-09 04:56:57 +00:00
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
self._tasks = []
|
2022-04-13 13:20:34 +00:00
|
|
|
self._failed_tasks = []
|
2022-04-08 19:24:43 +00:00
|
|
|
|
|
|
|
@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"""
|
2022-04-08 19:24:43 +00:00
|
|
|
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
|
2022-04-13 13:20:34 +00:00
|
|
|
if not task.deps or all([d in self.output_done for d in task.deps])
|
2022-04-09 15:08:05 +00:00
|
|
|
]
|
|
|
|
|
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}
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
@property
|
2022-04-13 13:20:34 +00:00
|
|
|
def output_done(self) -> list[str]:
|
|
|
|
return self._output_done
|
|
|
|
|
|
|
|
@property
|
|
|
|
def failed_tasks(self) -> list[Task]:
|
|
|
|
return self._failed_tasks
|
2022-04-08 19:24:43 +00:00
|
|
|
|
2022-04-09 04:56:57 +00:00
|
|
|
def append(self, tasks: list[Task]):
|
2022-04-08 19:24:43 +00:00
|
|
|
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)
|
2022-04-13 13:09:08 +00:00
|
|
|
message = self._dispatcher(task)
|
|
|
|
if message.status == 0:
|
2022-04-13 13:20:34 +00:00
|
|
|
self._output_done.append(task.output)
|
|
|
|
else:
|
|
|
|
self._failed_tasks.append(task)
|
2022-04-13 13:09:08 +00:00
|
|
|
|
|
|
|
return message
|
2022-04-09 04:56:57 +00:00
|
|
|
|
2022-04-09 19:46:24 +00:00
|
|
|
def backlog(self):
|
2022-07-28 07:39:51 +00:00
|
|
|
"""Yield tasks sorted according to dependencies"""
|
2022-04-09 19:46:24 +00:00
|
|
|
while self.doable_tasks:
|
|
|
|
yield self.next_task()
|