18 from typing
import TextIO
25 from .current_task_run
import setCurrentTaskRun
26 from .._folder_manager
import folder_manager
27 from ..entities
import TaskRun
32 def __init__(self, taskRun: TaskRun, outputStream: TextIO = sys.stdout) ->
None:
33 self._taskRun = taskRun
36 self.__stdoutBackup = sys.stdout
37 self.__stderrBackup = sys.stderr
40 sys.stdout = outputStream
41 sys.stderr = outputStream
43 def _restoreStreams(self) -> None:
44 sys.stdout = self.__stdoutBackup
45 sys.stderr = self.__stderrBackup
47 def onStart(self) -> None:
52 faultHandlerPath = folder_manager.getRunLogsDir(self._taskRun.id) /
"stacktrace.log"
53 faulthandler.register(
55 file = faultHandlerPath.open(
"w"),
59 def onSuccess(self) -> None:
60 logging.getLogger(
"coretexpylib").info(
">> [Coretex] TaskRun finished successfully")
62 self._restoreStreams()
64 def onException(self, exception: BaseException) ->
None:
65 logging.getLogger(
"coretexpylib").critical(
">> [Coretex] TaskRun failed to finish due to an error")
66 logging.getLogger(
"coretexpylib").debug(exception, exc_info =
True)
67 logging.getLogger(
"coretexpylib").critical(str(exception))
69 self._restoreStreams()
71 def onKeyboardInterrupt(self) -> None:
74 def onNetworkConnectionLost(self) -> None:
77 def onCleanUp(self) -> None:
84 from py3nvml
import py3nvml
85 py3nvml.nvmlShutdown()
89 setCurrentTaskRun(
None)