inputs: introduce new input manager class
Introduce a new class to manage inputs, `InputManger` and move the code to map inputs from the `Input` here. The main insight of why the logic should be place here is that certain information is needed to map inputs, independently of specific type: the path to the input directory, `root`, the store API, `storeapi` and the service manager instance to start the actual service. Instead of passing all this information again and again to the `Input` class, we now have a specialized (service) manager class for inputs that has all the needed information all the time.
This commit is contained in:
parent
923cc60fdb
commit
7eb58ea348
2 changed files with 22 additions and 14 deletions
|
|
@ -58,39 +58,46 @@ class Input:
|
|||
m.update(json.dumps(self.options, sort_keys=True).encode())
|
||||
return m.hexdigest()
|
||||
|
||||
def map(self,
|
||||
mgr: host.ServiceManager,
|
||||
storeapi: StoreServer,
|
||||
root: PathLike) -> Tuple[str, Dict]:
|
||||
|
||||
target = os.path.join(root, self.name)
|
||||
class InputManager:
|
||||
def __init__(self, mgr: host.ServiceManager, storeapi: StoreServer, root: PathLike) -> Dict:
|
||||
self.service_manager = mgr
|
||||
self.storeapi = storeapi
|
||||
self.root = root
|
||||
self.inputs = {}
|
||||
|
||||
def map(self, ip: Input) -> Tuple[str, Dict]:
|
||||
|
||||
target = os.path.join(self.root, ip.name)
|
||||
os.makedirs(target)
|
||||
|
||||
args = {
|
||||
# mandatory bits
|
||||
"origin": self.origin,
|
||||
"refs": self.refs,
|
||||
"origin": ip.origin,
|
||||
"refs": ip.refs,
|
||||
|
||||
"target": target,
|
||||
|
||||
# global options
|
||||
"options": self.options,
|
||||
"options": ip.options,
|
||||
|
||||
# API endpoints
|
||||
"api": {
|
||||
"store": storeapi.socket_address
|
||||
"store": self.storeapi.socket_address
|
||||
}
|
||||
}
|
||||
|
||||
client = mgr.start(f"input/{self.name}", self.info.path)
|
||||
client = self.service_manager.start(f"input/{ip.name}", ip.info.path)
|
||||
reply = client.call("map", args)
|
||||
|
||||
path = reply["path"]
|
||||
|
||||
if not path.startswith(root):
|
||||
if not path.startswith(self.root):
|
||||
raise RuntimeError(f"returned {path} has wrong prefix")
|
||||
|
||||
reply["path"] = os.path.relpath(path, root)
|
||||
reply["path"] = os.path.relpath(path, self.root)
|
||||
|
||||
self.inputs[ip.name] = reply
|
||||
|
||||
return reply
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from . import host
|
|||
from . import objectstore
|
||||
from . import remoteloop
|
||||
from .devices import Device, DeviceManager
|
||||
from .inputs import Input
|
||||
from .inputs import Input, InputManager
|
||||
from .mounts import Mount, MountManager
|
||||
from .sources import Source
|
||||
from .util import osrelease
|
||||
|
|
@ -203,8 +203,9 @@ class Stage:
|
|||
mgr = host.ServiceManager(monitor=monitor)
|
||||
cm.enter_context(mgr)
|
||||
|
||||
ipmgr = InputManager(mgr, storeapi, inputs_tmpdir)
|
||||
for key, ip in self.inputs.items():
|
||||
data = ip.map(mgr, storeapi, inputs_tmpdir)
|
||||
data = ipmgr.map(ip)
|
||||
inputs[key] = data
|
||||
|
||||
devmgr = DeviceManager(mgr, build_root.dev, tree)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue