From c44b314d56b6be8e47ff2797658a604e827df7c6 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Wed, 13 Jun 2018 15:31:54 +0200 Subject: [PATCH] Add launcher_proxy with a reload job feature. --- src/Launcher/CMakeLists.txt | 6 ++ src/Launcher/launcher_proxy.py | 104 +++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100755 src/Launcher/launcher_proxy.py diff --git a/src/Launcher/CMakeLists.txt b/src/Launcher/CMakeLists.txt index def84e2ae..2f661c8e6 100755 --- a/src/Launcher/CMakeLists.txt +++ b/src/Launcher/CMakeLists.txt @@ -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 index 000000000..7750c5c6f --- /dev/null +++ b/src/Launcher/launcher_proxy.py @@ -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) -- 2.39.2