forked from M-Labs/artiq
1
0
Fork 0

master: simplify worker/parent RPC

This commit is contained in:
Sebastien Bourdeauducq 2015-10-28 17:35:57 +08:00
parent 4e35a247d1
commit 1ada15ae5d
2 changed files with 21 additions and 27 deletions

View File

@ -160,8 +160,7 @@ class Worker:
return True return True
elif action == "pause": elif action == "pause":
return False return False
del obj["action"] elif action == "create_watchdog":
if action == "create_watchdog":
func = self.create_watchdog func = self.create_watchdog
elif action == "delete_watchdog": elif action == "delete_watchdog":
func = self.delete_watchdog func = self.delete_watchdog
@ -172,7 +171,7 @@ class Worker:
if getattr(func, "worker_pass_rid", False): if getattr(func, "worker_pass_rid", False):
func = partial(func, self.rid) func = partial(func, self.rid)
try: try:
data = func(**obj) data = func(*obj["args"], **obj["kwargs"])
reply = {"status": "ok", "data": data} reply = {"status": "ok", "data": data}
except: except:
reply = {"status": "failed", reply = {"status": "failed",

View File

@ -26,12 +26,9 @@ class ParentActionError(Exception):
pass pass
def make_parent_action(action, argnames, exception=ParentActionError): def make_parent_action(action, exception=ParentActionError):
argnames = argnames.split() def parent_action(*args, **kwargs):
def parent_action(*args): request = {"action": action, "args": args, "kwargs": kwargs}
request = {"action": action}
for argname, arg in zip(argnames, args):
request[argname] = arg
put_object(request) put_object(request)
reply = get_object() reply = get_object()
if "action" in reply: if "action" in reply:
@ -50,7 +47,7 @@ class LogForwarder:
def __init__(self): def __init__(self):
self.buffer = "" self.buffer = ""
to_parent = staticmethod(make_parent_action("log", "message")) to_parent = staticmethod(make_parent_action("log"))
def write(self, data): def write(self, data):
self.buffer += data self.buffer += data
@ -64,18 +61,18 @@ class LogForwarder:
class ParentDeviceDB: class ParentDeviceDB:
get_device_db = make_parent_action("get_device_db", "") get_device_db = make_parent_action("get_device_db")
get = make_parent_action("get_device", "key", KeyError) get = make_parent_action("get_device", KeyError)
class ParentDatasetDB: class ParentDatasetDB:
get = make_parent_action("get_dataset", "key", KeyError) get = make_parent_action("get_dataset", KeyError)
update = make_parent_action("update_dataset", "mod") update = make_parent_action("update_dataset")
class Watchdog: class Watchdog:
_create = make_parent_action("create_watchdog", "t") _create = make_parent_action("create_watchdog")
_delete = make_parent_action("delete_watchdog", "wid") _delete = make_parent_action("delete_watchdog")
def __init__(self, t): def __init__(self, t):
self.t = t self.t = t
@ -91,15 +88,14 @@ set_watchdog_factory(Watchdog)
class Scheduler: class Scheduler:
pause_noexc = staticmethod(make_parent_action("pause", "")) pause_noexc = staticmethod(make_parent_action("pause"))
def pause(self): def pause(self):
if self.pause_noexc(): if self.pause_noexc():
raise TerminationRequested raise TerminationRequested
submit = staticmethod(make_parent_action("scheduler_submit", submit = staticmethod(make_parent_action("scheduler_submit"))
"pipeline_name expid priority due_date flush")) cancel = staticmethod(make_parent_action("scheduler_cancel"))
cancel = staticmethod(make_parent_action("scheduler_cancel", "rid"))
def set_run_info(self, pipeline_name, expid, priority): def set_run_info(self, pipeline_name, expid, priority):
self.pipeline_name = pipeline_name self.pipeline_name = pipeline_name
@ -120,22 +116,21 @@ def get_exp(file, class_name):
return getattr(module, class_name) return getattr(module, class_name)
register_experiment = make_parent_action("register_experiment", register_experiment = make_parent_action("register_experiment")
"class_name name arguments")
class ExamineDeviceMgr: class ExamineDeviceMgr:
get_device_db = make_parent_action("get_device_db", "") get_device_db = make_parent_action("get_device_db")
def get(self, name): def get(name):
return None return None
class DummyDatasetMgr: class DummyDatasetMgr:
def set(self, key, value, broadcast=False, persist=False, save=True): def set(key, value, broadcast=False, persist=False, save=True):
return None return None
def get(self, key): def get(key):
pass pass
@ -213,7 +208,7 @@ def main():
f.close() f.close()
put_object({"action": "completed"}) put_object({"action": "completed"})
elif action == "examine": elif action == "examine":
examine(ExamineDeviceMgr(), DummyDatasetMgr(), obj["file"]) examine(ExamineDeviceMgr, DummyDatasetMgr, obj["file"])
put_object({"action": "completed"}) put_object({"action": "completed"})
elif action == "terminate": elif action == "terminate":
break break