Salome HOME
To avoid compilation pb on RedHat 8.0.
[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 "Batch_BatchManager_LSF.hxx"
38
39 namespace Batch {
40
41   BatchManager_LSF::BatchManager_LSF(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(parent, host)
42   {
43     // On se connecte au serveur LSF
44     _connect = lsb_init("Salome2 Batch library");
45     if (_connect < 0) { // si erreur
46       char * errmsg = lsb_sysmsg();
47       string msg = "LSF Server on host \"";
48       msg += _hostname;
49       msg += "\" : ";
50       msg += errmsg ? errmsg : "Reason unknown";
51       throw ConnexionFailureException(msg.c_str());
52     }
53   }
54
55   // Destructeur
56   BatchManager_LSF::~BatchManager_LSF()
57   {
58     // Nothing to do
59   }
60
61   // Methode pour le controle des jobs : soumet un job au gestionnaire
62   const JobId BatchManager_LSF::submitJob(const Job & job)
63   {
64     Job_LSF joblsf = job;
65     struct submitReply reply;
66     int ref = lsb_submit(joblsf.getSubmitStruct(),
67                          &reply);
68     if (ref < 0) { // si erreur
69       char * msg = lsb_sysmsg();
70       if (!msg) msg = "unknown";
71       throw APIInternalFailureException(string("LSF submit error. Reason : ") + msg);
72     }
73
74     ostringstream oss;
75     oss << ref;
76     JobId id(this, oss.str());
77     return id;
78   }
79
80   // Methode pour le controle des jobs : retire un job du gestionnaire
81   void BatchManager_LSF::deleteJob(const JobId & jobid)
82   {
83     int ref;
84     istringstream iss(jobid.getReference());
85     iss >> ref;
86     int rc = lsb_deletejob(ref, 0, 0);
87     if (rc < 0) { // si erreur
88       char * msg = lsb_sysmsg();
89       if (!msg) msg = "unknown";
90       throw APIInternalFailureException(string("LSF deljob error. Reason : ") + msg);
91     }
92   }
93    
94   // Methode pour le controle des jobs : suspend un job en file d'attente
95   void BatchManager_LSF::holdJob(const JobId & jobid)
96   {
97    int ref;
98     istringstream iss(jobid.getReference());
99     iss >> ref;
100     int rc = lsb_signaljob(ref, SIGSTOP);
101     if (rc < 0) { // si erreur
102       char * msg = lsb_sysmsg();
103       if (!msg) msg = "unknown";
104       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
105     }
106   }
107
108   // Methode pour le controle des jobs : relache un job suspendu
109   void BatchManager_LSF::releaseJob(const JobId & jobid)
110   {
111     int ref;
112     istringstream iss(jobid.getReference());
113     iss >> ref;
114     int rc = lsb_signaljob(ref, SIGCONT);
115     if (rc < 0) { // si erreur
116       char * msg = lsb_sysmsg();
117       if (!msg) msg = "unknown";
118       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
119     }
120   }
121
122
123   // Methode pour le controle des jobs : modifie un job en file d'attente
124   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
125   {
126     int ref;
127     istringstream iss(jobid.getReference());
128     iss >> ref;
129
130     Job_LSF joblsf = Job(param, env);
131     struct submitReply reply;
132     ref = lsb_modify(joblsf.getSubmitStruct(),
133                      &reply,
134                      ref);
135     if (ref < 0) { // si erreur
136       ostringstream msg_sst;
137       char * msg = lsb_sysmsg();
138       if (!msg) msg = "unknown";
139       msg_sst << msg << endl;
140 //       msg_sst << "BadJobId   = " << (long) reply.badJobId   << endl
141 //            << "BadJobName = " << reply.badJobName << endl
142 //            << "BadReqIndx = " << reply.badReqIndx << endl;
143       throw APIInternalFailureException(string("LSF modify error. Reason : ") + msg_sst.str());
144     }
145   }
146
147   // Methode pour le controle des jobs : modifie un job en file d'attente
148   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param)
149   {
150     alterJob(jobid, param, Environnement());
151   }
152
153   // Methode pour le controle des jobs : modifie un job en file d'attente
154   void BatchManager_LSF::alterJob(const JobId & jobid, const Environnement & env)
155   {
156     alterJob(jobid, Parametre(), env);
157   }
158
159
160
161   // Methode pour le controle des jobs : renvoie l'etat du job
162   JobInfo BatchManager_LSF::queryJob(const JobId & jobid)
163   {
164     int id;
165     istringstream iss(jobid.getReference());
166     iss >> id;
167
168     JobInfo_LSF ji = JobInfo_LSF(id);
169
170     return ji;
171   }
172
173
174
175   // Methode pour le controle des jobs : teste si un job est present en machine
176   bool BatchManager_LSF::isRunning(const JobId & jobid)
177   {
178     int id;
179     istringstream iss(jobid.getReference());
180     iss >> id;
181
182     JobInfo_LSF ji = JobInfo_LSF(id);
183
184     return ji.isRunning();
185   }
186
187
188
189 }