Skip to content

Commit

Permalink
Split up code into seperate functions
Browse files Browse the repository at this point in the history
CURA-10717
  • Loading branch information
nallath committed Jul 7, 2023
1 parent b9b80d3 commit e6e922d
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions UM/Backend/Backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,40 @@ def startEngine(self) -> None:
self._createSocket()
return

if not self._backend_log_max_lines:
self._backend_log = []

# Double check that the old process is indeed killed.
if self._process is not None:
try:
self._process.terminate()
except PermissionError:
Logger.log("e", "Unable to kill running engine. Access is denied.")
return
Logger.log("d", "Engine process is killed. Received return code %s", self._process.wait())
self._flushBackendLog()
self._ensureOldProcessIsTerminated()

self._process = self._runEngineProcess(command)
if self._process is None: # Failed to start engine.
return
Logger.log("i", "Started engine process: %s", self.getEngineCommand()[0])

self._beginThreads()

def _beginThreads(self) -> None:
self._backendLog(bytes("Calling engine with: %s\n" % self.getEngineCommand(), "utf-8"))
t = threading.Thread(target = self._storeOutputToLogThread, args = (self._process.stdout,), name = "EngineOutputThread")
t = threading.Thread(target=self._storeOutputToLogThread, args=(self._process.stdout,),
name="EngineOutputThread")
t.daemon = True
t.start()
t = threading.Thread(target = self._storeStderrToLogThread, args = (self._process.stderr,), name = "EngineErrorThread")
t = threading.Thread(target=self._storeStderrToLogThread, args=(self._process.stderr,),
name="EngineErrorThread")
t.daemon = True
t.start()

def _ensureOldProcessIsTerminated(self) -> None:
if self._process is not None:
try:
self._process.terminate()
except PermissionError:
Logger.log("e", "Unable to kill running engine. Access is denied.")
return
Logger.log("d", "Engine process is killed. Received return code %s", self._process.wait())

def _flushBackendLog(self) -> None:
if not self._backend_log_max_lines:
self._backend_log = []

def close(self) -> None:
if self._socket:
while self._socket.getState() == Arcus.SocketState.Opening:
Expand Down Expand Up @@ -235,6 +245,17 @@ def _onSocketError(self, error: Arcus.ErrorCode) -> None:

self._createSocket()

def _cleanupExistingSocket(self) -> None:
self._socket.stateChanged.disconnect(self._onSocketStateChanged)
self._socket.messageReceived.disconnect(self._onMessageReceived)
self._socket.error.disconnect(self._onSocketError)
# Hack for (at least) Linux. If the socket is connecting, the close will deadlock.
while self._socket.getState() == Arcus.SocketState.Opening:
sleep(0.1)
# If the error occurred due to parsing, both connections believe that connection is okay.
# So we need to force a close.
self._socket.close()

def _createSocket(self, protocol_file: Optional[str] = None) -> None:
"""Creates a socket and attaches listeners."""

Expand All @@ -244,15 +265,7 @@ def _createSocket(self, protocol_file: Optional[str] = None) -> None:

if self._socket:
Logger.log("d", "Previous socket existed. Closing that first.") # temp debug logging
self._socket.stateChanged.disconnect(self._onSocketStateChanged)
self._socket.messageReceived.disconnect(self._onMessageReceived)
self._socket.error.disconnect(self._onSocketError)
# Hack for (at least) Linux. If the socket is connecting, the close will deadlock.
while self._socket.getState() == Arcus.SocketState.Opening:
sleep(0.1)
# If the error occurred due to parsing, both connections believe that connection is okay.
# So we need to force a close.
self._socket.close()
self._cleanupExistingSocket()

self._socket = SignalSocket()
self._socket.stateChanged.connect(self._onSocketStateChanged)
Expand Down

0 comments on commit e6e922d

Please sign in to comment.