Feat: scheduler handle dependencies (basic)

This commit is contained in:
Bertrand Benjamin 2022-04-08 21:49:18 +02:00
parent bfe9e6f91e
commit e0377a3e92
2 changed files with 33 additions and 1 deletions

View File

@ -24,7 +24,14 @@ class Scheduler:
return ans
def __next__(self):
undoable = []
task = self._tasks.pop(0)
while not all([d in self.done for d in task.deps]):
undoable.append(task)
task = self._tasks.pop(0)
self.append(undoable)
ans = self.dispatch(task)
self._done.append(ans)
return ans

View File

@ -59,3 +59,28 @@ def test_schedule_multiple_tasks():
assert result == "three done"
assert scheduler.tasks == []
assert scheduler.done == ["one done", "two done", "three done"]
def test_schedule_multiple_tasks_with_dependencies():
actions = {"DO": lambda deps, args: f"{args['task']} done"}
scheduler = Scheduler(actions)
t1 = Task(action="DO", args={"task": "one"}, deps=["three done"])
t2 = Task(action="DO", args={"task": "two"}, deps=["one done"])
t3 = Task(action="DO", args={"task": "three"}, deps=[])
scheduler.append([t1, t2, t3])
result = scheduler.__next__()
assert result == "three done"
assert scheduler.tasks == [t1, t2]
assert scheduler.done == ["three done"]
result = scheduler.__next__()
assert result == "one done"
assert scheduler.tasks == [t2]
assert scheduler.done == ["three done", "one done"]
result = scheduler.__next__()
assert result == "two done"
assert scheduler.tasks == []
assert scheduler.done == ["three done", "one done", "two done"]