Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / LSF / JobInfo_LSF.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * JobInfo_LSF.cxx :  emulation of LSF client
24  *
25  * Auteur : Bernard SECHER - CEA DEN
26  * Mail   : mailto:bernard.secher@cea.fr
27  * Date   : Thu Apr 24 10:17:22 2008
28  * Projet : PAL Salome 
29  *
30  */
31
32 #include <cstdio>
33 #include <sstream>
34
35 #include <Constants.hxx>
36 #include "JobInfo_LSF.hxx"
37 #include "Log.hxx"
38
39 using namespace std;
40
41 namespace Batch {
42
43   // Constructeurs
44   JobInfo_LSF::JobInfo_LSF(int id, const std::string & queryOutput) : JobInfo()
45   {
46     // Fill ID parameter
47     ostringstream oss;
48     oss << id;
49     _param[ID] = oss.str();
50
51     // read query output
52     string line;
53     istringstream fp(queryOutput);
54     getline(fp, line);
55
56     // On some batch managers, the job is deleted soon after it is finished,
57     // so we have to consider that an unknown job (empty file) is a finished
58     // one, even if it is not always true.
59     if (fp.eof()) {
60       _param[STATE] = FINISHED;
61     } else {
62       string sjobid, username, status;
63       fp >> sjobid;
64       fp >> username;
65       fp >> status;
66
67       if (status == "PEND") {         // Pending
68         _param[STATE] = QUEUED;
69       } else if (status == "PSUSP") { // Suspended while pending
70         _param[STATE] = PAUSED;
71       } else if (status == "RUN") {   // Running
72         _param[STATE] = RUNNING;
73       } else if (status == "USUSP") { // Suspended while running
74         _param[STATE] = PAUSED;
75       } else if (status == "SSUSP") { // Suspended by LSF
76         _param[STATE] = PAUSED;
77       } else if (status == "DONE") {  // Finished successfully
78         _param[STATE] = FINISHED;
79       } else if (status == "EXIT") {  // Finished in error
80         _param[STATE] = FAILED;
81       } else if (status == "UNKWN") { // Lost contact
82         _param[STATE] = FAILED;
83       } else if (status == "ZOMBI") { // Zombie
84         _param[STATE] = FAILED;
85       } else {
86         LOG("Unknown job state code: " << status);
87       }
88
89       if( status.find("RUN") != string::npos)
90         _running = true;
91     }
92   }
93
94   // Teste si un job est present en machine
95   bool JobInfo_LSF::isRunning() const
96   {
97     return _running;
98   }
99
100
101   // Destructeur
102   JobInfo_LSF::~JobInfo_LSF()
103   {
104     // Nothing to do
105   }
106
107   // Convertit une date HH:MM:SS en secondes
108   long JobInfo_LSF::HMStoLong(const string & s)
109   {
110     long hour, min, sec;
111
112     sscanf( s.c_str(), "%ld:%ld:%ld", &hour, &min, &sec);
113     return ( ( ( hour * 60L ) + min ) * 60L ) + sec;
114   }
115
116   // Methode pour l'interfacage avec Python (SWIG) : affichage en Python
117   string JobInfo_LSF::__str__() const
118   {
119     ostringstream sst;
120     sst << "<JobInfo_LSF (" << this << ") :" << endl;
121     sst << " ID = " <<_param[ID] << endl;
122     sst << " STATE = " <<_param[STATE] << endl;
123
124     return sst.str();
125   }
126
127
128 }