Feat: scheduler handle dependencies (basic)
This commit is contained in:
parent
bfe9e6f91e
commit
e0377a3e92
@ -19,12 +19,19 @@ class Scheduler:
|
|||||||
self._tasks += tasks
|
self._tasks += tasks
|
||||||
|
|
||||||
def dispatch(self, task):
|
def dispatch(self, task):
|
||||||
""" Do a task """
|
"""Do a task"""
|
||||||
ans = self.actions[task.action](task.deps, task.args)
|
ans = self.actions[task.action](task.deps, task.args)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
|
undoable = []
|
||||||
|
|
||||||
task = self._tasks.pop(0)
|
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)
|
ans = self.dispatch(task)
|
||||||
self._done.append(ans)
|
self._done.append(ans)
|
||||||
return ans
|
return ans
|
||||||
|
@ -59,3 +59,28 @@ def test_schedule_multiple_tasks():
|
|||||||
assert result == "three done"
|
assert result == "three done"
|
||||||
assert scheduler.tasks == []
|
assert scheduler.tasks == []
|
||||||
assert scheduler.done == ["one done", "two done", "three done"]
|
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"]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user