Salome HOME
082615115933e669ba5da11b3384b5912bf2a110
[tools/libbatch.git] / src / LSF / Batch_BatchManager_LSF.cxx
1 //  Copyright (C) 2007-2010  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   // Methode pour le controle des jobs : retire un job du gestionnaire
86   void BatchManager_LSF::deleteJob(const JobId & jobid)
87   {
88     int ref;
89     istringstream iss(jobid.getReference());
90     iss >> ref;
91     int rc = lsb_deletejob(ref, 0, 0);
92     if (rc < 0) { // si erreur
93       char * msg = lsb_sysmsg();
94       if (!msg) msg = "unknown";
95       throw APIInternalFailureException(string("LSF deljob error. Reason : ") + msg);
96     }
97   }
98    
99   // Methode pour le controle des jobs : suspend un job en file d'attente
100   void BatchManager_LSF::holdJob(const JobId & jobid)
101   {
102    int ref;
103     istringstream iss(jobid.getReference());
104     iss >> ref;
105     int rc = lsb_signaljob(ref, SIGSTOP);
106     if (rc < 0) { // si erreur
107       char * msg = lsb_sysmsg();
108       if (!msg) msg = "unknown";
109       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
110     }
111   }
112
113   // Methode pour le controle des jobs : relache un job suspendu
114   void BatchManager_LSF::releaseJob(const JobId & jobid)
115   {
116     int ref;
117     istringstream iss(jobid.getReference());
118     iss >> ref;
119     int rc = lsb_signaljob(ref, SIGCONT);
120     if (rc < 0) { // si erreur
121       char * msg = lsb_sysmsg();
122       if (!msg) msg = "unknown";
123       throw APIInternalFailureException(string("LSF signaljob error. Reason : ") + msg);
124     }
125   }
126
127
128   // Methode pour le controle des jobs : modifie un job en file d'attente
129   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
130   {
131     int ref;
132     istringstream iss(jobid.getReference());
133     iss >> ref;
134
135     Job_LSF joblsf = Job(param, env);
136     struct submitReply reply;
137     ref = lsb_modify(joblsf.getSubmitStruct(),
138                      &reply,
139                      ref);
140     if (ref < 0) { // si erreur
141       ostringstream msg_sst;
142       char * msg = lsb_sysmsg();
143       if (!msg) msg = "unknown";
144       msg_sst << msg << endl;
145 //       msg_sst << "BadJobId   = " << (long) reply.badJobId   << endl
146 //            << "BadJobName = " << reply.badJobName << endl
147 //            << "BadReqIndx = " << reply.badReqIndx << endl;
148       throw APIInternalFailureException(string("LSF modify error. Reason : ") + msg_sst.str());
149     }
150   }
151
152   // Methode pour le controle des jobs : modifie un job en file d'attente
153   void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param)
154   {
155     alterJob(jobid, param, Environnement());
156   }
157
158   // Methode pour le controle des jobs : modifie un job en file d'attente
159   void BatchManager_LSF::alterJob(const JobId & jobid, const Environnement & env)
160   {
161     alterJob(jobid, Parametre(), env);
162   }
163
164
165
166   // Methode pour le controle des jobs : renvoie l'etat du job
167   JobInfo BatchManager_LSF::queryJob(const JobId & jobid)
168   {
169     int id;
170     istringstream iss(jobid.getReference());
171     iss >> id;
172
173     JobInfo_LSF ji = JobInfo_LSF(id);
174
175     return ji;
176   }
177
178
179
180   // Methode pour le controle des jobs : teste si un job est present en machine
181   bool BatchManager_LSF::isRunning(const JobId & jobid)
182   {
183     int id;
184     istringstream iss(jobid.getReference());
185     iss >> id;
186
187     JobInfo_LSF ji = JobInfo_LSF(id);
188
189     return ji.isRunning();
190   }
191
192
193
194 }