Salome HOME
Update copyrights
[tools/libbatch.git] / src / LSF / Batch_BatchManager_LSF.cxx
1 //  Copyright (C) 2007-2012  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.
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  * BatchManager_LSF.cxx : 
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Thu Nov  6 10:17:22 2003
28  * Projet : Salome 2
29  *
30  */
31
32 extern "C" {
33 #include <lsf/lsf.h>
34 #include <lsf/lsbatch.h>
35 }
36 #include <iostream>
37 #include <fstream>
38 #include <sstream>
39 #include <string>
40 #include "Batch_BatchManager_LSF.hxx"
41
42 using namespace std;
43
44 namespace Batch {
45
46   BatchManager_LSF::BatchManager_LSF(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(parent, host)
47   {
48     // On se connecte au serveur LSF
49     _connect = lsb_init("Salome2 Batch library");
50     if (_connect < 0) { // si erreur
51       char * errmsg = lsb_sysmsg();
52       string msg = "LSF Server on host \"";
53       msg += _hostname;
54       msg += "\" : ";
55       msg += errmsg ? errmsg : "Reason unknown";
56       throw ConnexionFailureException(msg.c_str());
57     }
58   }
59
60   // Destructeur
61   BatchManager_LSF::~BatchManager_LSF()
62   {
63     // Nothing to do
64   }
65
66   // Methode pour le controle des jobs : soumet un job au gestionnaire
67   const JobId BatchManager_LSF::submitJob(const Job & job)
68   {
69     Job_LSF joblsf = job;
70     struct submitReply reply;
71     int ref = lsb_submit(joblsf.getSubmitStruct(),
72                          &reply);
73     if (ref < 0) { // si erreur
74       char * msg = lsb_sysmsg();
75       if (!msg) msg = "unknown";
76       throw APIInternalFailureException(string("LSF submit error. Reason : ") + msg);
77     }
78
79     ostringstream oss;
80     oss << ref;
81     JobId id(this, oss.str());
82     return id;
83   }
84
85   // Ce manager permet de faire de la reprise
86   const Batch::JobId
87   BatchManager_LSF::addJob(const Batch::Job & job, const std::string reference)
88   {
89     return JobId(this, reference);
90   }
91
92   // Methode pour le controle des jobs : retire un job du gestionnaire
93   void BatchManager_LSF::deleteJob(const JobId & jobid)
94   {
95     int ref;
96     istringstream iss(jobid.getReference());
97     iss >> ref;
98     int rc = lsb_deletejob(ref, 0, 0);
99     if (rc < 0) { // si erreur
100       char * msg = lsb_sysmsg();
101       if (!msg) msg = "unknown";
102       throw APIInternalFailureException(string("LSF deljob error. Reason : ") + msg);
103     }
104   }
105    
106   // Methode pour le controle des jobs : suspend un job en file d'attente
107   void BatchManager_LSF::holdJob(const JobId & jobid)
108   {
109    int ref;
110     istringstream iss(jobid.getReference());
111     iss >> ref;
112     int rc = lsb_signaljob(ref, SIGSTOP);
113     if (rc < 0) { // si erreur
114       char * msg = lsb_sysmsg();
115       if (!msg) msg = "unknown";
116       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
117     }
118   }
119
120   // Methode pour le controle des jobs : relache un job suspendu
121   void BatchManager_LSF::releaseJob(const JobId & jobid)
122   {
123     int ref;
124     istringstream iss(jobid.getReference());
125     iss >> ref;
126     int rc = lsb_signaljob(ref, SIGCONT);
127     if (rc < 0) { // si erreur
128       char * msg = lsb_sysmsg();
129       if (!msg) msg = "unknown";
130       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
131     }
132   }
133
134
135   // Methode pour le controle des jobs : modifie un job en file d'attente
136   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
137   {
138     int ref;
139     istringstream iss(jobid.getReference());
140     iss >> ref;
141
142     Job_LSF joblsf = Job(param, env);
143     struct submitReply reply;
144     ref = lsb_modify(joblsf.getSubmitStruct(),
145                      &reply,
146                      ref);
147     if (ref < 0) { // si erreur
148       ostringstream msg_sst;
149       char * msg = lsb_sysmsg();
150       if (!msg) msg = "unknown";
151       msg_sst << msg << endl;
152 //       msg_sst << "BadJobId   = " << (long) reply.badJobId   << endl
153 //            << "BadJobName = " << reply.badJobName << endl
154 //            << "BadReqIndx = " << reply.badReqIndx << endl;
155       throw APIInternalFailureException(string("LSF modify error. Reason : ") + msg_sst.str());
156     }
157   }
158
159   // Methode pour le controle des jobs : modifie un job en file d'attente
160   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param)
161   {
162     alterJob(jobid, param, Environnement());
163   }
164
165   // Methode pour le controle des jobs : modifie un job en file d'attente
166   void BatchManager_LSF::alterJob(const JobId & jobid, const Environnement & env)
167   {
168     alterJob(jobid, Parametre(), env);
169   }
170
171
172
173   // Methode pour le controle des jobs : renvoie l'etat du job
174   JobInfo BatchManager_LSF::queryJob(const JobId & jobid)
175   {
176     int id;
177     istringstream iss(jobid.getReference());
178     iss >> id;
179
180     JobInfo_LSF ji = JobInfo_LSF(id);
181
182     return ji;
183   }
184
185
186
187   // Methode pour le controle des jobs : teste si un job est present en machine
188   bool BatchManager_LSF::isRunning(const JobId & jobid)
189   {
190     int id;
191     istringstream iss(jobid.getReference());
192     iss >> id;
193
194     JobInfo_LSF ji = JobInfo_LSF(id);
195
196     return ji.isRunning();
197   }
198
199
200
201 }