Salome HOME
5e8c2cfa7f9f0387796a8187e3c7ce560de3ebf9
[modules/kernel.git] / src / Launcher / launcher_proxy.py
1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2017-2024  CEA, EDF
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 """ Easier access to SalomeLauncher"""
22
23 import os
24 import salome
25 import time
26
27 JOB_FILE_NAME = "jobDump.xml"
28
29 class Job:
30   """
31   This class makes an easier access to SalomeLauncher.
32   It adds an automatic save of the job's parameters after the launch. The save
33   is done into the result directory of the job. It is then possible to reload
34   the job from that file. It also provides a waiting loop for the job to finish.
35   This proxy also hides the calls to the naming service in order to get the
36   instance of SalomeLauncher.
37   """
38
39   @staticmethod
40   def launch(job_params):
41     """ Create, submit and dump a new job to result_dir."""
42     myjob = Job()
43     launcher = salome.naming_service.Resolve('/SalomeLauncher')
44     myjob.launcher = launcher
45     result_dir = job_params.result_directory # local path where to copy out_files
46
47     myjob.job_id = launcher.createJob(job_params) #SALOME id of the job
48     launcher.launchJob(myjob.job_id) # copy files, run pre_command, submit job
49     myjob.saveJob(result_dir)
50     return myjob
51
52   @staticmethod
53   def reloadJob(result_dir):
54     """ Create a new job from a job dumped in result_dir."""
55     myjob = Job()
56     launcher = salome.naming_service.Resolve('/SalomeLauncher')
57     myjob.launcher = launcher
58     myjob.job_id = -1
59     try:
60       job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
61       job_string = ""
62       with open(job_file_path, "r") as f:
63         job_string = f.read()
64       myjob.job_id = launcher.restoreJob(job_string)
65     except Exception:
66       myjob = None
67     return myjob
68
69   def saveJob(self, result_dir):
70     job_string = self.launcher.dumpJob(self.job_id)
71     job_file_path = os.path.join(result_dir, JOB_FILE_NAME)
72     if not os.path.exists(result_dir):
73       os.makedirs(result_dir)
74     with open(job_file_path, "w") as f:
75       f.write(job_string)
76
77   def wait(self, sleep_delay=10):
78     """ Wait for the end of the job """
79     launcher = self.launcher
80     job_id = self.job_id
81     jobState = launcher.getJobState(job_id)
82     while jobState != "FINISHED" and jobState != "FAILED" :
83       time.sleep(sleep_delay)
84       jobState = launcher.getJobState(job_id)
85       print ("Job %d state: %s" % (job_id,jobState))
86
87   def verify(self):
88     """ Get the return code of the solver if the job is finished.
89         If the job is not finished (submission error or another state),
90         return an empty string.
91     """
92     launcher = self.launcher
93     job_id = self.job_id
94     jobState = launcher.getJobState(job_id)
95     exit_code = ""
96     if jobState != "FINISHED" :
97       print ("Job has not finished correctly.")
98       print ("Job %d state: %s" % (job_id,jobState))
99     else :
100       job_params = launcher.getJobParameters(job_id)
101       temp_dir = job_params.result_directory
102       temp_log_dir = os.path.join(temp_dir, "logs")
103       if(launcher.getJobWorkFile(job_id, "logs/exit_code.log", temp_log_dir)):
104         exit_code_file = os.path.join(temp_log_dir, "exit_code.log")
105         if os.path.isfile(exit_code_file):
106           with open(exit_code_file) as myfile:
107             exit_code = myfile.read()
108     return exit_code.strip()
109
110   def getResults(self):
111     """ Copy the result files from remote working_directory
112         to the local result_directory."""
113     self.launcher.getJobResults(self.job_id, "")
114
115   def relaunch(self, script=""):
116     job_params = self.launcher.getJobParameters(self.job_id)
117     if script:
118       job_params.job_file = script
119     job_params.pre_command = ""
120     self.job_id = self.launcher.createJob(job_params)
121     self.launcher.launchJob(self.job_id)
122     self.saveJob(job_params.result_directory)