Feat: move action to worker with a dispatcher
This commit is contained in:
parent
3f1464f3f6
commit
08411bd42d
@ -4,6 +4,6 @@ from bopytex.tasks import Task
|
|||||||
def simple(options: dict) -> list[Task]:
|
def simple(options: dict) -> list[Task]:
|
||||||
"""Simple planner with options['quantity'] tasks and no dependencies"""
|
"""Simple planner with options['quantity'] tasks and no dependencies"""
|
||||||
return [
|
return [
|
||||||
Task("Do", args={"number": i}, deps=[], output=f"{i}")
|
Task("DO", args={"number": i}, deps=[], output=f"{i}")
|
||||||
for i in range(options["quantity"])
|
for i in range(options["quantity"])
|
||||||
]
|
]
|
||||||
|
@ -5,14 +5,13 @@
|
|||||||
Producing then compiling templates
|
Producing then compiling templates
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from bopytex.actions import ACTIONS
|
|
||||||
from bopytex.scheduler import Scheduler
|
from bopytex.scheduler import Scheduler
|
||||||
|
|
||||||
|
|
||||||
def orcherstrator(
|
def orcherstrator(
|
||||||
options: dict,
|
options: dict,
|
||||||
planner,
|
planner,
|
||||||
actions: dict = ACTIONS,
|
dispatcher,
|
||||||
):
|
):
|
||||||
tasks = planner(options)
|
tasks = planner(options)
|
||||||
|
|
||||||
@ -20,7 +19,7 @@ def orcherstrator(
|
|||||||
scheduler.append(tasks)
|
scheduler.append(tasks)
|
||||||
|
|
||||||
for task in scheduler.backlog():
|
for task in scheduler.backlog():
|
||||||
yield task
|
yield from dispatcher(task)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
23
bopytex/worker/__init__.py
Normal file
23
bopytex/worker/__init__.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
class ActionNotFound(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Dispatcher:
|
||||||
|
def __init__(self, actions: list):
|
||||||
|
self._actions = actions
|
||||||
|
|
||||||
|
def __call__(self, task):
|
||||||
|
try:
|
||||||
|
choosen_action = self._actions[task.action]
|
||||||
|
except KeyError:
|
||||||
|
raise ActionNotFound(f"The action {task.action} is not in {self._actions.keys()}")
|
||||||
|
|
||||||
|
return choosen_action(
|
||||||
|
args=task.args,
|
||||||
|
deps=task.deps,
|
||||||
|
output=task.output
|
||||||
|
)
|
||||||
|
|
||||||
|
def fake_worker(args, deps, output):
|
||||||
|
yield f"FAKE - {args} - {deps} - {output}"
|
||||||
|
|
@ -1,3 +1,4 @@
|
|||||||
|
""" A worker consumes tasks """
|
||||||
def generate():
|
def generate():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -18,10 +19,10 @@ def clean():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
ACTIONS = {
|
WORKERS = {
|
||||||
"GENERATE": generate,
|
"GENERATE": generate,
|
||||||
"COMPILE": compile,
|
"COMPILE": compile,
|
||||||
"ACTIVATE_CORR": activate_corr,
|
"ACTIVATE_CORR": activate_corr,
|
||||||
"JOIN_PDF": join_pdf,
|
"JOIN_PDF": join_pdf,
|
||||||
"clean": clean,
|
"CLEAN": clean,
|
||||||
}
|
}
|
@ -1,10 +1,14 @@
|
|||||||
from bopytex.planner import fake_planner
|
from bopytex.planner import fake_planner
|
||||||
from bopytex.service import orcherstrator
|
from bopytex.service import orcherstrator
|
||||||
from bopytex.tasks import Task
|
from bopytex.tasks import Task
|
||||||
|
from bopytex.worker import Dispatcher, fake_worker
|
||||||
|
|
||||||
|
|
||||||
def test_service():
|
def test_service():
|
||||||
options = {"quantity": 3, "template": "tpl_src.tex"}
|
options = {"quantity": 3, "template": "tpl_src.tex"}
|
||||||
service = orcherstrator(options, fake_planner.simple)
|
dispatcher = Dispatcher(actions={"DO": fake_worker})
|
||||||
for i, task in enumerate(service):
|
|
||||||
assert task == Task("Do", args={"number": i}, deps=[], output=f"{i}")
|
service = orcherstrator(options, fake_planner.simple, dispatcher)
|
||||||
|
|
||||||
|
for i, task_output in enumerate(service):
|
||||||
|
assert task_output == f"FAKE - {{'number': {i}}} - [] - {i}"
|
||||||
|
Loading…
Reference in New Issue
Block a user