From fef98f30c96fc0663032e7bc5c7e1e6fe848790d Mon Sep 17 00:00:00 2001 From: ahrenholz Date: Thu, 3 Apr 2014 21:58:05 +0000 Subject: [PATCH] when executing Python scripts from the GUI, run in background thread and wait for them to enter the RUNTIME state --- daemon/sbin/core-daemon | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/daemon/sbin/core-daemon b/daemon/sbin/core-daemon index 7b716725..af8adb44 100755 --- a/daemon/sbin/core-daemon +++ b/daemon/sbin/core-daemon @@ -936,12 +936,12 @@ class CoreRequestHandler(SocketServer.BaseRequestHandler): if os.path.splitext(filename)[1].lower() == '.xml': session = server.getsession(useexisting=False) opensessionxml(session, filename, start=True) - # TODO: Script may not return; run in separate thread here. - # Wait for some configurable timeout period, then check - # for new session below. Wait for session to enter - # the runtime state, then send back the register message. else: - execfile(filename, {'server': server}) + t = threading.Thread(target = execfile, + args=(filename, {'server': server})) + t.daemon = True + t.start() + time.sleep(0.25) # allow time for session creation if msg.flags & coreapi.CORE_API_STR_FLAG: new_session_ids = set(server.getsessionids()) new_sid = new_session_ids.difference(old_session_ids) @@ -951,6 +951,18 @@ class CoreRequestHandler(SocketServer.BaseRequestHandler): except KeyError: self.info("executed '%s' with unknown session ID" % ex) return replies + self.info("checking session %d for RUNTIME state" % sid) + session = self.server.getsession(sessionid=sid, useexisting=True) + retries = 10 + # wait for session to enter RUNTIME state, to prevent GUI from + # connecting while nodes are still being instantiated + while session.getstate() != coreapi.CORE_EVENT_RUNTIME_STATE: + self.info("waiting for session %d to enter RUNTIME state" % sid) + time.sleep(1) + retries -= 1 + if retries <= 0: + self.info("session %d did not enter RUNTIME state" % sid) + return replies tlvdata = coreapi.CoreRegTlv.pack( \ coreapi.CORE_TLV_REG_EXECSRV, ex) tlvdata += coreapi.CoreRegTlv.pack( \