Salome HOME
[EDF29576] : destroy containers even if problem arises
authorAnthony Geay <anthony.geay@edf.fr>
Tue, 5 Mar 2024 16:08:51 +0000 (17:08 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Tue, 5 Mar 2024 16:08:51 +0000 (17:08 +0100)
src/yacsloader/driver_internal.py

index 33b9aac17864be856ce88d9b968f83913cd681be..cd45019f335f848cfc85f5858b14a873bcb7206f 100644 (file)
@@ -207,7 +207,7 @@ def prepareExecution(proc, isStop, dumpErrorFile):
   return ex
 
 @SALOMEInitializationNeeded
-def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, CPUMemContainerTimeRes,
+def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, shutdown, CPUMemContainerTimeRes,
                  HTopOfThisProcessFile, HTopTimeRes,
                  HTopOfAllServersFile, HTopOfAllServersTimeRes, DirectoriesToMonitor):
   """
@@ -220,6 +220,7 @@ def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, CPUMemC
   dump (int) : time interval between 2 dump state
   finalDump ( string ) : filename containing final result of graph, if any.
   display (int) :
+  shutdown (int) : shutdown level
   CPUMemContainerTimeRes (int) : time in second between two measures of CPU/Mem in container processes
   HTopOfThisProcessFile (str) : file name (if not empty) containing the result of measure of current process
   HTopTimeRes (int) : time in second between two measures of CPU/Mem of current process
@@ -231,6 +232,19 @@ def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, CPUMemC
   import pilot
   import os
   import contextlib
+
+  class AutoShutdown:
+    def __init__(self, proc, shutdown):
+      self._proc = proc
+      self._shutdown = shutdown
+    def __enter__(self):
+      pass
+    
+    def __exit__(self,exctype, exc, tb):
+      if self._shutdown < 999:
+        self._proc.shutdown(self._shutdown)
+      salome.dsm.shutdownScopes()
+      my_runtime_yacs.fini( False )
   
   def MonitoringDirectories( DirectoriesToMonitor ):
     import SALOME_PyNode
@@ -253,44 +267,39 @@ def executeGraph( executor, xmlfilename, proc, dump, finalDump, display, CPUMemC
       return contextlib.nullcontext()
   #
   if dump != 0:
-      dumpFile = "dumpState_{}".format( os.path.basename(xmlfilename) )
-      lockFile = "{}.lock".format( os.path.splitext( os.path.basename(xmlfilename) )[0] )
-      dump_thread = SALOMERuntime.ThreadDumpState(proc,dump,dumpFile,lockFile)
-      dump_thread.start()
+    dumpFile = "dumpState_{}".format( os.path.basename(xmlfilename) )
+    lockFile = "{}.lock".format( os.path.splitext( os.path.basename(xmlfilename) )[0] )
+    dump_thread = SALOMERuntime.ThreadDumpState(proc,dump,dumpFile,lockFile)
+    dump_thread.start()
   #
   salome.cm.SetDeltaTimeBetweenCPUMemMeasureInMilliSecond( 1000*CPUMemContainerTimeRes )
   #
   ctxManagers = MonitoringDirectories( DirectoriesToMonitor ) + [ MonitoringThisProcess(HTopOfThisProcessFile, HTopTimeRes) ] + [ MonitoringAllKernelServers(HTopOfAllServersFile, HTopOfAllServersTimeRes) ]
-  with contextlib.ExitStack() as stack:
-    for mgr in ctxManagers:
-      stack.enter_context(mgr)
-    executor.RunPy(proc,display,isPyThread=True,fromscratch=True) # same as RunW but releasing GIL
-  if proc.getEffectiveState() != pilot.DONE:
-    raise RuntimeError( proc.getErrorReport() )
+  
+  with AutoShutdown(proc,shutdown):
+    with contextlib.ExitStack() as stack:
+      for mgr in ctxManagers:
+        stack.enter_context(mgr)
+      executor.RunPy(proc,display,isPyThread=True,fromscratch=True) # same as RunW but releasing GIL
+    #
+    if dump_thread:
+        dump_thread.join()
+    #
+    if proc.getEffectiveState() != pilot.DONE:
+      raise RuntimeError( proc.getErrorReport() )
   #
   if display > 0:
       proc.writeDotInFile("titi")
   #
-  if dump_thread:
-      dump_thread.join()
-  #
   if finalDump:
     logging.info(f"Final dump requested : {finalDump}")
     SALOMERuntime.schemaSaveStateUnsafe( proc, finalDump )
 
-@SALOMEInitializationNeeded
-def destroyElementsGeneratedByExecutionOfGraph( proc, shutdown ):
-  """
-  Args:
-  -----
-
-  shutdown (int) : shutdown level
-
-  """
+"""
   if shutdown < 999:
     proc.shutdown(shutdown)
   salome.dsm.shutdownScopes()
-  my_runtime_yacs.fini( False )
+  my_runtime_yacs.fini( False )"""
 
 def EntryFromCoarseEntry( entry ):
   if entry[:2] != "--":
@@ -367,11 +376,9 @@ def mainRun( args, xmlFileName):
   patchGraph( proc, not args[DoNotSqueezeKeyInARGS], args[InitPortKeyInARGS], args[SaveXMLSchemaKeyInARGS], args[LoadStateKeyInARGS], args[ResetKeyInARGS], args[DisplayKeyInARGS])
   executor = prepareExecution( proc, args[StopOnErrorKeyInARGS], args[DumpOnErrorKeyInARGS])
   #
-  executeGraph( executor, xmlFileName, proc, args[DumpKeyInARGS], args[DumpStateKeyInARGS], args[DisplayKeyInARGS], args[CPUTimeResOfContainerKeyInARGS],
+  executeGraph( executor, xmlFileName, proc, args[DumpKeyInARGS], args[DumpStateKeyInARGS], args[DisplayKeyInARGS], args[ShutdownKeyInARGS], args[CPUTimeResOfContainerKeyInARGS],
                args[HTOPFileKeyInARGS], args[HTOPFileTimeResKeyInARGS],
                args[HTOPServerFileKeyInARGS], args[HTOPServerFileTimeResKeyInARGS], [(dirToMonitor,resFile,timeRes) for dirToMonitor,resFile,timeRes in zip(args[MonitoringDirsInARGS],args[MonitoringDirsResInARGS],args[MonitoringDirsTimeResInARGS])] )
-  #
-  destroyElementsGeneratedByExecutionOfGraph( proc, args[ShutdownKeyInARGS] )
 
 def parseArgs():
   """