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