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-09 04:56:57 +00:00
|
|
|
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
class Scheduler:
|
2022-04-09 14:16:39 +00:00
|
|
|
def __init__(self, actions: dict, done: list[str] = None):
|
2022-04-08 19:24:43 +00:00
|
|
|
self.actions = actions
|
2022-04-09 04:56:57 +00:00
|
|
|
|
|
|
|
if done is None:
|
|
|
|
self._done = []
|
|
|
|
else:
|
|
|
|
self._done = done
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
self._tasks = []
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tasks(self):
|
|
|
|
return self._tasks
|
|
|
|
|
2022-04-09 04:56:57 +00:00
|
|
|
@property
|
|
|
|
def all_deps(self):
|
|
|
|
return {d for task in self.tasks for d in task.deps}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def all_output(self):
|
|
|
|
return {task.output for task in self.tasks}
|
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
@property
|
|
|
|
def done(self):
|
|
|
|
return self._done
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def dispatch(self, task):
|
2022-04-08 19:49:18 +00:00
|
|
|
"""Do a task"""
|
2022-04-09 04:32:56 +00:00
|
|
|
ans = self.actions[task.action](task.deps, task.args, task.output)
|
2022-04-08 19:24:43 +00:00
|
|
|
return ans
|
|
|
|
|
2022-04-08 20:04:47 +00:00
|
|
|
def __iter__(self):
|
|
|
|
return self
|
2022-04-09 04:13:40 +00:00
|
|
|
|
2022-04-08 19:24:43 +00:00
|
|
|
def __next__(self):
|
2022-04-08 20:04:47 +00:00
|
|
|
return self.next()
|
|
|
|
|
|
|
|
def next(self):
|
2022-04-08 19:49:18 +00:00
|
|
|
undoable = []
|
|
|
|
|
2022-04-08 20:04:47 +00:00
|
|
|
try:
|
|
|
|
task = self._tasks.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
raise StopIteration
|
|
|
|
|
2022-04-08 19:49:18 +00:00
|
|
|
while not all([d in self.done for d in task.deps]):
|
|
|
|
undoable.append(task)
|
2022-04-08 20:04:47 +00:00
|
|
|
try:
|
|
|
|
task = self._tasks.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
self.append(undoable)
|
|
|
|
raise StopIteration
|
2022-04-08 19:49:18 +00:00
|
|
|
|
|
|
|
self.append(undoable)
|
2022-04-08 19:29:35 +00:00
|
|
|
ans = self.dispatch(task)
|
2022-04-09 04:32:56 +00:00
|
|
|
self._done.append(task.output)
|
2022-04-08 19:29:35 +00:00
|
|
|
return ans
|
2022-04-08 20:04:47 +00:00
|
|
|
|
|
|
|
def run(self):
|
2022-04-09 04:13:40 +00:00
|
|
|
for _ in self:
|
2022-04-08 20:04:47 +00:00
|
|
|
pass
|
2022-04-09 04:56:57 +00:00
|
|
|
|
|
|
|
def is_finishable(self):
|
|
|
|
return self.all_deps.issubset(self.all_output)
|