]> SALOME platform Git repositories - modules/yacs.git/commitdiff
Salome HOME
Add launcher_proxy with a reload job feature.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 13 Jun 2018 13:31:54 +0000 (15:31 +0200)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 13 Jun 2018 13:31:54 +0000 (15:31 +0200)
src/Launcher/CMakeLists.txt
src/Launcher/launcher_proxy.py [new file with mode: 0755]

index def84e2ae5287bd88f5e321972ca66c6ffdece2f..2f661c8e6b101e72f1614f3b48135605f34a0844 100755 (executable)
@@ -130,5 +130,11 @@ SET(COMMON_HEADERS_HXX
   SALOME_Launcher_Parser.hxx
   SALOME_Launcher_defs.hxx
 )
+
+SET(LAUNCHER_PYTHON_SCRIPTS
+  launcher_proxy.py
+)
+
 INSTALL(FILES ${COMMON_HEADERS_HXX} DESTINATION ${SALOME_INSTALL_HEADERS})
 INSTALL(FILES testLauncher.xml DESTINATION ${SALOME_KERNEL_INSTALL_RES_DATA})
+SALOME_INSTALL_SCRIPTS("${LAUNCHER_PYTHON_SCRIPTS}" ${SALOME_INSTALL_PYTHON})
diff --git a/src/Launcher/launcher_proxy.py b/src/Launcher/launcher_proxy.py
new file mode 100755 (executable)
index 0000000..7750c5c
--- /dev/null
@@ -0,0 +1,104 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+""" Easier access to SalomeLauncher"""
+
+import os
+import salome
+import time
+
+JOB_FILE_NAME = "jobDump.xml"
+
+class Job(object):
+  """
+  This class makes an easier access to SalomeLauncher.
+  It adds an automatic save of the job's parameters after the launch. The save
+  is done into the result directory of the job. It is then possible to reload
+  the job from that file. It also provides a waiting loop for the job to finish.
+  This proxy also hides the calls to the naming service in order to get the
+  instance of SalomeLauncher.
+  """
+
+  @staticmethod
+  def launch(job_params):
+    """ Create, submit and dump a new job to result_dir."""
+    myjob = Job()
+    launcher = salome.naming_service.Resolve('/SalomeLauncher')
+    myjob.launcher = launcher
+    result_dir = job_params.result_directory # local path where to copy out_files
+
+    myjob.job_id = launcher.createJob(job_params) #SALOME id of the job
+    launcher.launchJob(myjob.job_id) # copy files, run pre_command, submit job
+    myjob.saveJob(result_dir)
+    return myjob
+
+  @staticmethod
+  def reloadJob(result_dir):
+    """ Create a new job from a job dumped in result_dir."""
+    myjob = Job()
+    launcher = salome.naming_service.Resolve('/SalomeLauncher')
+    myjob.launcher = launcher
+    myjob.job_id = -1
+    try:
+      job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
+      job_string = ""
+      with open(job_file_path, "r") as f:
+        job_string = f.read()
+      myjob.job_id = launcher.restoreJob(job_string)
+    except:
+      myjob = None
+    return myjob
+
+  def saveJob(self, result_dir):
+    job_string = self.launcher.dumpJob(self.job_id)
+    job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
+    if not os.path.exists(result_dir):
+      os.makedirs(result_dir)
+    with open(job_file_path, "w") as f:
+      f.write(job_string)
+
+  def wait(self, sleep_delay=10):
+    """ Wait for the end of the job """
+    launcher = self.launcher
+    job_id = self.job_id
+    jobState = launcher.getJobState(job_id)
+    while jobState != "FINISHED" and jobState != "FAILED" :
+      time.sleep(sleep_delay)
+      jobState = launcher.getJobState(job_id)
+      print ("Job %d state: %s" % (job_id,jobState))
+
+  def verify(self):
+    """ Get the return code of the solver if the job is finished.
+        If the job is not finished (submission error or another state),
+        return an empty string.
+    """
+    launcher = self.launcher
+    job_id = self.job_id
+    jobState = launcher.getJobState(job_id)
+    exit_code = ""
+    if jobState != "FINISHED" :
+      print ("Job has not finished correctly.")
+      print ("Job %d state: %s" % (job_id,jobState))
+    else :
+      job_params = launcher.getJobParameters(job_id)
+      temp_dir = job_params.result_directory
+      temp_log_dir = os.path.join(temp_dir, "logs")
+      if(launcher.getJobWorkFile(job_id, "logs/exit_code.log", temp_log_dir)):
+        exit_code_file = os.path.join(temp_log_dir, "exit_code.log")
+        if os.path.isfile(exit_code_file):
+          with open(exit_code_file) as myfile:
+            exit_code = myfile.read()
+    return exit_code.strip()
+
+  def getResults(self):
+    """ Copy the result files from remote working_directory
+        to the local result_directory."""
+    self.launcher.getJobResults(self.job_id, "")
+
+  def relaunch(self, script=""):
+    job_params = self.launcher.getJobParameters(self.job_id)
+    if script:
+      job_params.job_file = script
+    job_params.pre_command = ""
+    self.job_id = self.launcher.createJob(job_params)
+    self.launcher.launchJob(self.job_id)
+    self.saveJob(job_params.result_directory)