feat: run the scheduler
This commit is contained in:
parent
e0377a3e92
commit
2b0c325203
@ -1,6 +1,9 @@
|
|||||||
""" Scheduler for action to make """
|
""" Scheduler for action to make """
|
||||||
|
|
||||||
|
|
||||||
|
from logging import exception
|
||||||
|
|
||||||
|
|
||||||
class Scheduler:
|
class Scheduler:
|
||||||
def __init__(self, actions: list):
|
def __init__(self, actions: list):
|
||||||
self.actions = actions
|
self.actions = actions
|
||||||
@ -23,15 +26,32 @@ class Scheduler:
|
|||||||
ans = self.actions[task.action](task.deps, task.args)
|
ans = self.actions[task.action](task.deps, task.args)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
|
return self.next()
|
||||||
|
|
||||||
|
def next(self):
|
||||||
undoable = []
|
undoable = []
|
||||||
|
|
||||||
|
try:
|
||||||
task = self._tasks.pop(0)
|
task = self._tasks.pop(0)
|
||||||
|
except IndexError:
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
while not all([d in self.done for d in task.deps]):
|
while not all([d in self.done for d in task.deps]):
|
||||||
undoable.append(task)
|
undoable.append(task)
|
||||||
|
try:
|
||||||
task = self._tasks.pop(0)
|
task = self._tasks.pop(0)
|
||||||
|
except IndexError:
|
||||||
|
self.append(undoable)
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
self.append(undoable)
|
self.append(undoable)
|
||||||
ans = self.dispatch(task)
|
ans = self.dispatch(task)
|
||||||
self._done.append(ans)
|
self._done.append(ans)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
for i in self:
|
||||||
|
pass
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from bopytex.planner import Task
|
from bopytex.planner import Task
|
||||||
from bopytex.scheduler import Scheduler
|
from bopytex.scheduler import Scheduler
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def test_schedule_append():
|
def test_schedule_append():
|
||||||
@ -22,7 +23,7 @@ def test_schedule_one_task():
|
|||||||
actions = {"DO": lambda deps, args: "done"}
|
actions = {"DO": lambda deps, args: "done"}
|
||||||
scheduler = Scheduler(actions)
|
scheduler = Scheduler(actions)
|
||||||
scheduler.append([Task(action="DO", args={}, deps=[])])
|
scheduler.append([Task(action="DO", args={}, deps=[])])
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
assert result == "done"
|
assert result == "done"
|
||||||
assert scheduler.tasks == []
|
assert scheduler.tasks == []
|
||||||
assert scheduler.done == ["done"]
|
assert scheduler.done == ["done"]
|
||||||
@ -32,7 +33,7 @@ def test_schedule_one_task_with_args():
|
|||||||
actions = {"DO": lambda deps, args: f"{args['task']} done"}
|
actions = {"DO": lambda deps, args: f"{args['task']} done"}
|
||||||
scheduler = Scheduler(actions)
|
scheduler = Scheduler(actions)
|
||||||
scheduler.append([Task(action="DO", args={"task": "one"}, deps=[])])
|
scheduler.append([Task(action="DO", args={"task": "one"}, deps=[])])
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
assert result == "one done"
|
assert result == "one done"
|
||||||
assert scheduler.tasks == []
|
assert scheduler.tasks == []
|
||||||
assert scheduler.done == ["one done"]
|
assert scheduler.done == ["one done"]
|
||||||
@ -45,17 +46,17 @@ def test_schedule_multiple_tasks():
|
|||||||
t2 = Task(action="DO", args={"task": "two"}, deps=[])
|
t2 = Task(action="DO", args={"task": "two"}, deps=[])
|
||||||
t3 = Task(action="DO", args={"task": "three"}, deps=[])
|
t3 = Task(action="DO", args={"task": "three"}, deps=[])
|
||||||
scheduler.append([t1, t2, t3])
|
scheduler.append([t1, t2, t3])
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
assert result == "one done"
|
assert result == "one done"
|
||||||
assert scheduler.tasks == [t2, t3]
|
assert scheduler.tasks == [t2, t3]
|
||||||
assert scheduler.done == ["one done"]
|
assert scheduler.done == ["one done"]
|
||||||
|
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
assert result == "two done"
|
assert result == "two done"
|
||||||
assert scheduler.tasks == [t3]
|
assert scheduler.tasks == [t3]
|
||||||
assert scheduler.done == ["one done", "two done"]
|
assert scheduler.done == ["one done", "two done"]
|
||||||
|
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
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"]
|
||||||
@ -69,18 +70,37 @@ def test_schedule_multiple_tasks_with_dependencies():
|
|||||||
t3 = Task(action="DO", args={"task": "three"}, deps=[])
|
t3 = Task(action="DO", args={"task": "three"}, deps=[])
|
||||||
scheduler.append([t1, t2, t3])
|
scheduler.append([t1, t2, t3])
|
||||||
|
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
assert result == "three done"
|
assert result == "three done"
|
||||||
assert scheduler.tasks == [t1, t2]
|
assert scheduler.tasks == [t1, t2]
|
||||||
assert scheduler.done == ["three done"]
|
assert scheduler.done == ["three done"]
|
||||||
|
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
assert result == "one done"
|
assert result == "one done"
|
||||||
assert scheduler.tasks == [t2]
|
assert scheduler.tasks == [t2]
|
||||||
assert scheduler.done == ["three done", "one done"]
|
assert scheduler.done == ["three done", "one done"]
|
||||||
|
|
||||||
result = scheduler.__next__()
|
result = scheduler.next()
|
||||||
assert result == "two done"
|
assert result == "two done"
|
||||||
assert scheduler.tasks == []
|
assert scheduler.tasks == []
|
||||||
assert scheduler.done == ["three done", "one done", "two done"]
|
assert scheduler.done == ["three done", "one done", "two done"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_schedule_empty_task():
|
||||||
|
actions = {"DO": lambda deps, args: f"{args['task']} done"}
|
||||||
|
scheduler = Scheduler(actions)
|
||||||
|
scheduler.append([])
|
||||||
|
with pytest.raises(StopIteration):
|
||||||
|
scheduler.next()
|
||||||
|
|
||||||
|
|
||||||
|
def test_schedule_multiple_tasks_with_undoable_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=[])
|
||||||
|
scheduler.append([t1, t2])
|
||||||
|
scheduler.run()
|
||||||
|
assert scheduler.tasks == [t1]
|
||||||
|
assert scheduler.done == ["two done"]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user