Salome HOME
Merging from V4_1_0_maintainance for porting on Win32 Platform
[modules/kernel.git] / src / Batch / Batch_BatchManager_LSF.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21  * BatchManager_LSF.cxx : 
22  *
23  * Auteur : Ivan DUTKA-MALEN - EDF R&D
24  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
25  * Date   : Thu Nov  6 10:17:22 2003
26  * Projet : Salome 2
27  *
28  */
29
30 extern "C" {
31 #include <lsf/lsf.h>
32 #include <lsf/lsbatch.h>
33 }
34 #include <iostream>
35 #include <fstream>
36 #include <sstream>
37 #include <string>
38 #include "Batch_BatchManager_LSF.hxx"
39
40 using namespace std;
41
42 namespace Batch {
43
44   BatchManager_LSF::BatchManager_LSF(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(parent, host)
45   {
46     // On se connecte au serveur LSF
47     _connect = lsb_init("Salome2 Batch library");
48     if (_connect < 0) { // si erreur
49       char * errmsg = lsb_sysmsg();
50       string msg = "LSF Server on host \"";
51       msg += _hostname;
52       msg += "\" : ";
53       msg += errmsg ? errmsg : "Reason unknown";
54       throw ConnexionFailureException(msg.c_str());
55     }
56   }
57
58   // Destructeur
59   BatchManager_LSF::~BatchManager_LSF()
60   {
61     // Nothing to do
62   }
63
64   // Methode pour le controle des jobs : soumet un job au gestionnaire
65   const JobId BatchManager_LSF::submitJob(const Job & job)
66   {
67     Job_LSF joblsf = job;
68     struct submitReply reply;
69     int ref = lsb_submit(joblsf.getSubmitStruct(),
70                          &reply);
71     if (ref < 0) { // si erreur
72       char * msg = lsb_sysmsg();
73       if (!msg) msg = "unknown";
74       throw APIInternalFailureException(string("LSF submit error. Reason : ") + msg);
75     }
76
77     ostringstream oss;
78     oss << ref;
79     JobId id(this, oss.str());
80     return id;
81   }
82
83   // Methode pour le controle des jobs : retire un job du gestionnaire
84   void BatchManager_LSF::deleteJob(const JobId & jobid)
85   {
86     int ref;
87     istringstream iss(jobid.getReference());
88     iss >> ref;
89     int rc = lsb_deletejob(ref, 0, 0);
90     if (rc < 0) { // si erreur
91       char * msg = lsb_sysmsg();
92       if (!msg) msg = "unknown";
93       throw APIInternalFailureException(string("LSF deljob error. Reason : ") + msg);
94     }
95   }
96    
97   // Methode pour le controle des jobs : suspend un job en file d'attente
98   void BatchManager_LSF::holdJob(const JobId & jobid)
99   {
100    int ref;
101     istringstream iss(jobid.getReference());
102     iss >> ref;
103     int rc = lsb_signaljob(ref, SIGSTOP);
104     if (rc < 0) { // si erreur
105       char * msg = lsb_sysmsg();
106       if (!msg) msg = "unknown";
107       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
108     }
109   }
110
111   // Methode pour le controle des jobs : relache un job suspendu
112   void BatchManager_LSF::releaseJob(const JobId & jobid)
113   {
114     int ref;
115     istringstream iss(jobid.getReference());
116     iss >> ref;
117     int rc = lsb_signaljob(ref, SIGCONT);
118     if (rc < 0) { // si erreur
119       char * msg = lsb_sysmsg();
120       if (!msg) msg = "unknown";
121       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
122     }
123   }
124
125
126   // Methode pour le controle des jobs : modifie un job en file d'attente
127   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
128   {
129     int ref;
130     istringstream iss(jobid.getReference());
131     iss >> ref;
132
133     Job_LSF joblsf = Job(param, env);
134     struct submitReply reply;
135     ref = lsb_modify(joblsf.getSubmitStruct(),
136                      &reply,
137                      ref);
138     if (ref < 0) { // si erreur
139       ostringstream msg_sst;
140       char * msg = lsb_sysmsg();
141       if (!msg) msg = "unknown";
142       msg_sst << msg << endl;
143 //       msg_sst << "BadJobId   = " << (long) reply.badJobId   << endl
144 //            << "BadJobName = " << reply.badJobName << endl
145 //            << "BadReqIndx = " << reply.badReqIndx << endl;
146       throw APIInternalFailureException(string("LSF modify error. Reason : ") + msg_sst.str());
147     }
148   }
149
150   // Methode pour le controle des jobs : modifie un job en file d'attente
151   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param)
152   {
153     alterJob(jobid, param, Environnement());
154   }
155
156   // Methode pour le controle des jobs : modifie un job en file d'attente
157   void BatchManager_LSF::alterJob(const JobId & jobid, const Environnement & env)
158   {
159     alterJob(jobid, Parametre(), env);
160   }
161
162
163
164   // Methode pour le controle des jobs : renvoie l'etat du job
165   JobInfo BatchManager_LSF::queryJob(const JobId & jobid)
166   {
167     int id;
168     istringstream iss(jobid.getReference());
169     iss >> id;
170
171     JobInfo_LSF ji = JobInfo_LSF(id);
172
173     return ji;
174   }
175
176
177
178   // Methode pour le controle des jobs : teste si un job est present en machine
179   bool BatchManager_LSF::isRunning(const JobId & jobid)
180   {
181     int id;
182     istringstream iss(jobid.getReference());
183     iss >> id;
184
185     JobInfo_LSF ji = JobInfo_LSF(id);
186
187     return ji.isRunning();
188   }
189
190
191
192 }