Feat: add doable_task to scheduler

This commit is contained in:
Bertrand Benjamin 2022-04-09 17:08:05 +02:00
parent dc12a919d0
commit 03482d4b3d
2 changed files with 20 additions and 4 deletions

View File

@ -19,6 +19,14 @@ class Scheduler:
def tasks(self):
return self._tasks
@property
def doable_tasks(self):
return [
task
for task in self.tasks
if not task.deps or all([d in self.done for d in task.deps])
]
@property
def all_deps(self):
return {d for task in self.tasks for d in task.deps}

View File

@ -31,7 +31,9 @@ def test_schedule_dispatch():
def test_schedule_one_task():
scheduler = Scheduler(actions)
scheduler.append([Task(action="DO", args={}, deps=[], output="end")])
tasks = [Task(action="DO", args={}, deps=[], output="end")]
scheduler.append(tasks)
assert scheduler.doable_tasks == tasks
result = scheduler.next()
assert result == "[] - {} - end - done"
assert scheduler.tasks == []
@ -54,7 +56,8 @@ def test_schedule_multiple_tasks():
t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
assert scheduler.is_finishable() == True
assert scheduler.doable_tasks == [t1, t2, t3]
assert scheduler.is_finishable()
result = scheduler.next()
assert result == "[] - {'task': 'one'} - one - done"
@ -79,17 +82,20 @@ def test_schedule_multiple_tasks_with_dependencies():
t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
assert scheduler.is_finishable() == True
assert scheduler.doable_tasks == [t3]
assert scheduler.is_finishable()
result = scheduler.next()
assert result == "[] - {'task': 'three'} - three - done"
assert scheduler.tasks == [t1, t2]
assert scheduler.done == ["three"]
assert scheduler.doable_tasks == [t1]
result = scheduler.next()
assert result == "['three'] - {'task': 'one'} - one - done"
assert scheduler.tasks == [t2]
assert scheduler.done == ["three", "one"]
assert scheduler.doable_tasks == [t2]
result = scheduler.next()
assert result == "['one'] - {'task': 'two'} - two - done"
@ -110,8 +116,10 @@ def test_schedule_multiple_tasks_with_undoable_dependencies():
t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two")
scheduler.append([t1, t2])
assert scheduler.is_finishable() == False
assert scheduler.doable_tasks == [t2]
assert not scheduler.is_finishable()
scheduler.run()
assert scheduler.tasks == [t1]
assert scheduler.done == ["two"]
assert scheduler.doable_tasks == []