]> SALOME platform Git repositories - modules/kernel.git/blob - src/Launcher/launcher_proxy.py
Salome HOME
Add launcher_proxy with a reload job feature.
[modules/kernel.git] / src / Launcher / launcher_proxy.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """ Easier access to SalomeLauncher"""
4
5 import os
6 import salome
7 import time
8
9 JOB_FILE_NAME = "jobDump.xml"
10
11 class Job(object):
12   """
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.
19   """
20
21   @staticmethod
22   def launch(job_params):
23     """ Create, submit and dump a new job to result_dir."""
24     myjob = Job()
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
28
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)
32     return myjob
33
34   @staticmethod
35   def reloadJob(result_dir):
36     """ Create a new job from a job dumped in result_dir."""
37     myjob = Job()
38     launcher = salome.naming_service.Resolve('/SalomeLauncher')
39     myjob.launcher = launcher
40     myjob.job_id = -1
41     try:
42       job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
43       job_string = ""
44       with open(job_file_path, "r") as f:
45         job_string = f.read()
46       myjob.job_id = launcher.restoreJob(job_string)
47     except:
48       myjob = None
49     return myjob
50
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:
57       f.write(job_string)
58
59   def wait(self, sleep_delay=10):
60     """ Wait for the end of the job """
61     launcher = self.launcher
62     job_id = self.job_id
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))
68
69   def verify(self):
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.
73     """
74     launcher = self.launcher
75     job_id = self.job_id
76     jobState = launcher.getJobState(job_id)
77     exit_code = ""
78     if jobState != "FINISHED" :
79       print ("Job has not finished correctly.")
80       print ("Job %d state: %s" % (job_id,jobState))
81     else :
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()
91
92   def getResults(self):
93     """ Copy the result files from remote working_directory
94         to the local result_directory."""
95     self.launcher.getJobResults(self.job_id, "")
96
97   def relaunch(self, script=""):
98     job_params = self.launcher.getJobParameters(self.job_id)
99     if script:
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)