Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / src / Batch / Batch_BatchManager_PBS.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_PBS.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 <pbs_error.h>
32 #include <pbs_ifl.h>
33 }
34 #include <iostream>
35 #include <fstream>
36 #include <sstream>
37 //#include "MEDMEM_STRING.hxx"
38 #include "Batch_BatchManager_PBS.hxx"
39 using namespace std;
40
41 namespace Batch {
42
43   // Recupere le nom du serveur par defaut
44 //   string BatchManager_PBS::getDefaultServer() {
45 //     string server_name = "localhost";
46
47 //     const char * server_name_path = "@openpbsspooldir@/server_name";
48 //     ifstream server_name_file(server_name_path);
49 //     if (server_name_file) {
50 //       server_name_file >> server_name;
51 //       server_name_file.close();
52 //     }
53
54 //     return server_name;
55 //   }
56
57   // Constructeur
58 //   BatchManager_PBS::BatchManager_PBS() throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(BatchManager_PBS::getDefaultServer())
59 //   {
60 //     // On se connecte au serveur PBS
61 //     _connect = pbs_connect(const_cast< char * >(_hostname.c_str()));
62 //     if (_connect < 0) { // si erreur
63 //       char * errmsg = pbs_geterrmsg(_connect);
64 //       string msg = "PBS Server on host \"";
65 //       msg += _hostname;
66 //       msg += "\" : ";
67 //       msg += errmsg ? errmsg : "Reason unknown";
68 //       throw ConnexionFailureException(msg.c_str());
69 //     }
70 //   }
71
72   // Constructeur
73 //   BatchManager_PBS::BatchManager_PBS(string host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(host)
74 //   {
75 //     // On se connecte au serveur PBS
76 //     _connect = pbs_connect(const_cast< char * >(_hostname.c_str()));
77 //     if (_connect < 0) { // si erreur
78 //       char * errmsg = pbs_geterrmsg(_connect);
79 //       string msg = "PBS Server on host \"";
80 //       msg += _hostname;
81 //       msg += "\" : ";
82 //       msg += errmsg ? errmsg : "Reason unknown";
83 //       throw ConnexionFailureException(msg.c_str());
84 //     }
85 //   }
86   BatchManager_PBS::BatchManager_PBS(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager(parent, host)
87   {
88     // On se connecte au serveur PBS
89     _connect = pbs_connect(const_cast< char * >(_hostname.c_str()));
90     if (_connect < 0) { // si erreur
91       char * errmsg = pbs_geterrmsg(_connect);
92       string msg = "PBS Server on host \"";
93       msg += _hostname;
94       msg += "\" : ";
95       msg += errmsg ? errmsg : "Reason unknown";
96       throw ConnexionFailureException(msg.c_str());
97     }
98   }
99
100   // Destructeur
101   BatchManager_PBS::~BatchManager_PBS()
102   {
103     // On se deconnecte du serveur PBS
104     int rc = pbs_disconnect(_connect);
105     if (rc < 0) { // si erreur
106       string msg = "PBS Server on host \"";
107       msg += _hostname;
108       msg += "\" : ";
109       msg += pbs_geterrmsg(_connect);
110       throw ConnexionFailureException(msg.c_str());
111     }
112   }
113
114   // Methode pour le controle des jobs : soumet un job au gestionnaire
115   const JobId BatchManager_PBS::submitJob(const Job & job)
116   {
117     Job_PBS jobpbs = job;
118     char * ref = pbs_submit(_connect,
119                             jobpbs.getAttributesOP(),
120                             jobpbs.getScript(),
121                             jobpbs.getDestination(),
122                             NULL);
123     if (!ref) { // si erreur
124       char * msg = pbs_geterrmsg(_connect);
125       if (!msg) msg = "unknown";
126       throw APIInternalFailureException(string("PBS submit error. Reason : ") + msg);
127     }
128
129     JobId id(this, string(ref));
130     free(ref);
131     return id;
132   }
133
134   // Methode pour le controle des jobs : retire un job du gestionnaire
135   void BatchManager_PBS::deleteJob(const JobId & jobid)
136   {
137     char * ref = const_cast< char * >(jobid.getReference().c_str());
138     int rc = pbs_deljob(_connect, ref, 0);
139     if (rc) { // si erreur
140       char * msg = pbs_geterrmsg(_connect);
141       if (!msg) msg = "unknown";
142       throw APIInternalFailureException(string("PBS deljob error. Reason : ") + msg);
143     }
144   }
145    
146   // Methode pour le controle des jobs : suspend un job en file d'attente
147   void BatchManager_PBS::holdJob(const JobId & jobid)
148   {
149     char * ref = const_cast< char * >(jobid.getReference().c_str());
150     int rc = pbs_holdjob(_connect, ref, USER_HOLD, 0);
151     if (rc) { // si erreur
152       char * msg = pbs_geterrmsg(_connect);
153       if (!msg) msg = "unknown";
154       throw APIInternalFailureException(string("PBS holdjob error. Reason : ") + msg);
155     }
156   }
157
158   // Methode pour le controle des jobs : relache un job suspendu
159   void BatchManager_PBS::releaseJob(const JobId & jobid)
160   {
161     char * ref = const_cast< char * >(jobid.getReference().c_str());
162     int rc = pbs_rlsjob(_connect, ref, USER_HOLD, 0);
163     if (rc) { // si erreur
164       char * msg = pbs_geterrmsg(_connect);
165       if (!msg) msg = "unknown";
166       throw APIInternalFailureException(string("PBS rlsjob error. Reason : ") + msg);
167     }
168   }
169
170
171   // Methode pour le controle des jobs : modifie un job en file d'attente
172   void BatchManager_PBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
173   {
174     Job job(param, env);
175     Job_PBS jobpbs(job);
176
177     char * ref = const_cast< char * >(jobid.getReference().c_str());
178     int rc = pbs_alterjob(_connect,
179                           ref,
180                           jobpbs.getAttributes(),
181                           NULL);
182     if (rc) { // si erreur
183       char * msg = pbs_geterrmsg(_connect);
184       if (!msg) msg = "unknown";
185       throw APIInternalFailureException(string("PBS alterjob error. Reason : ") + msg);
186     }
187                 
188   }
189
190   // Methode pour le controle des jobs : modifie un job en file d'attente
191   void BatchManager_PBS::alterJob(const JobId & jobid, const Parametre & param)
192   {
193     alterJob(jobid, param, Environnement());
194   }
195
196   // Methode pour le controle des jobs : modifie un job en file d'attente
197   void BatchManager_PBS::alterJob(const JobId & jobid, const Environnement & env)
198   {
199     alterJob(jobid, Parametre(), env);
200   }
201
202
203
204   // Methode pour le controle des jobs : renvoie l'etat du job
205   JobInfo BatchManager_PBS::queryJob(const JobId & jobid)
206   {
207     char * id = const_cast< char * >(jobid.getReference().c_str());
208     JobInfo_PBS ji = JobInfo_PBS(pbs_statjob(_connect, id, 0, 0), true);
209     return ji;
210   }
211
212
213
214 }