2 # -*- coding: utf-8 -*-
3 """ Easier access to SalomeLauncher"""
9 JOB_FILE_NAME = "jobDump.xml"
13 This class makes an easier access to SalomeLauncher.
14 It adds an automatic save of the job's parameters after the launch. The save
15 is done into the result directory of the job. It is then possible to reload
16 the job from that file. It also provides a waiting loop for the job to finish.
17 This proxy also hides the calls to the naming service in order to get the
18 instance of SalomeLauncher.
22 def launch(job_params):
23 """ Create, submit and dump a new job to result_dir."""
25 launcher = salome.naming_service.Resolve('/SalomeLauncher')
26 myjob.launcher = launcher
27 result_dir = job_params.result_directory # local path where to copy out_files
29 myjob.job_id = launcher.createJob(job_params) #SALOME id of the job
30 launcher.launchJob(myjob.job_id) # copy files, run pre_command, submit job
31 myjob.saveJob(result_dir)
35 def reloadJob(result_dir):
36 """ Create a new job from a job dumped in result_dir."""
38 launcher = salome.naming_service.Resolve('/SalomeLauncher')
39 myjob.launcher = launcher
42 job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
44 with open(job_file_path, "r") as f:
46 myjob.job_id = launcher.restoreJob(job_string)
51 def saveJob(self, result_dir):
52 job_string = self.launcher.dumpJob(self.job_id)
53 job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
54 if not os.path.exists(result_dir):
55 os.makedirs(result_dir)
56 with open(job_file_path, "w") as f:
59 def wait(self, sleep_delay=10):
60 """ Wait for the end of the job """
61 launcher = self.launcher
63 jobState = launcher.getJobState(job_id)
64 while jobState != "FINISHED" and jobState != "FAILED" :
65 time.sleep(sleep_delay)
66 jobState = launcher.getJobState(job_id)
67 print ("Job %d state: %s" % (job_id,jobState))
70 """ Get the return code of the solver if the job is finished.
71 If the job is not finished (submission error or another state),
72 return an empty string.
74 launcher = self.launcher
76 jobState = launcher.getJobState(job_id)
78 if jobState != "FINISHED" :
79 print ("Job has not finished correctly.")
80 print ("Job %d state: %s" % (job_id,jobState))
82 job_params = launcher.getJobParameters(job_id)
83 temp_dir = job_params.result_directory
84 temp_log_dir = os.path.join(temp_dir, "logs")
85 if(launcher.getJobWorkFile(job_id, "logs/exit_code.log", temp_log_dir)):
86 exit_code_file = os.path.join(temp_log_dir, "exit_code.log")
87 if os.path.isfile(exit_code_file):
88 with open(exit_code_file) as myfile:
89 exit_code = myfile.read()
90 return exit_code.strip()
93 """ Copy the result files from remote working_directory
94 to the local result_directory."""
95 self.launcher.getJobResults(self.job_id, "")
97 def relaunch(self, script=""):
98 job_params = self.launcher.getJobParameters(self.job_id)
100 job_params.job_file = script
101 job_params.pre_command = ""
102 self.job_id = self.launcher.createJob(job_params)
103 self.launcher.launchJob(self.job_id)
104 self.saveJob(job_params.result_directory)