]> SALOME platform Git repositories - tools/libbatch.git/blob - src/Core/Batch_BatchManager.cxx
Salome HOME
Errors in directory creation with Vishnu are not fatal
[tools/libbatch.git] / src / Core / Batch_BatchManager.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.cxx :
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Date   : Septembre 2003
27  * Projet : SALOME 2
28  *
29  */
30
31 #include <iostream>
32 #include <sstream>
33 #include <string>
34 #ifdef WIN32
35 # include<winsock2.h>
36 #else
37 # include <netdb.h>
38 #endif
39
40 #include "Batch_Constants.hxx"
41 #include "Batch_Job.hxx"
42 #include "Batch_JobId.hxx"
43 #include "Batch_JobInfo.hxx"
44 #include "Batch_InvalidArgumentException.hxx"
45 #include "Batch_FactBatchManager.hxx"
46 #include "Batch_BatchManager.hxx"
47
48 #ifdef WIN32
49 #define sleep(seconds) Sleep((seconds)*1000)
50 #endif
51
52 using namespace std;
53
54 namespace Batch {
55
56   // Constructeur
57 //   BatchManager::BatchManager(string host) throw(InvalidArgumentException) : _hostname(host), jobid_map()
58 //   {
59 //     // On verifie que le hostname est correct
60 //     if (!gethostbyname(_hostname.c_str())) { // hostname unknown from network
61 //       string msg = "hostname \"";
62 //       msg += _hostname;
63 //       msg += "\" unknown from the network";
64 //       throw InvalidArgumentException(msg.c_str());
65 //     }
66 //   }
67   BatchManager::BatchManager(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException) : _hostname(host), jobid_map(), _parent(parent)
68   {
69     /*
70 #ifdef WIN32
71     WSADATA wsaData;
72     WSAStartup(MAKEWORD(2, 2), &wsaData);  // Initialize Winsock
73 #endif
74
75     // On verifie que le hostname est correct
76     struct hostent* res = gethostbyname(_hostname.c_str());
77
78 #ifdef WIN32
79     WSACleanup();  // Finalize Winsock
80 #endif
81
82     if (!res) { // hostname unknown from network
83       string msg = "hostname \"";
84       msg += _hostname;
85       msg += "\" unknown from the network";
86       throw InvalidArgumentException(msg.c_str());
87     }
88     */
89   }
90
91   // Destructeur
92   BatchManager::~BatchManager()
93   {
94     // Nothing to do
95   }
96
97   string BatchManager::__repr__() const
98   {
99     ostringstream oss;
100     oss << "<BatchManager of type '" << (_parent ? _parent->getType() : "unknown (no factory)") << "' connected to server '" << _hostname << "'>";
101     return oss.str();
102   }
103
104   // Recupere le l'identifiant d'un job deja soumis au BatchManager
105 //   const JobId BatchManager::getJobIdByReference(const string & ref)
106 //   {
107 //     return JobId(this, ref);
108 //   }
109   const JobId BatchManager::getJobIdByReference(const char * ref)
110   {
111     return JobId(this, ref);
112   }
113
114 //   // Methode pour le controle des jobs : soumet un job au gestionnaire
115 //   const JobId BatchManager::submitJob(const Job & job)
116 //   {
117 //     static int idx = 0;
118 //     //MEDMEM::STRING sst;
119 //     ostringstream sst;
120 //     sst << "Jobid_" << idx++;
121 //     JobId id(this, sst.str());
122 //     return id;
123 //   }
124
125 //   // Methode pour le controle des jobs : retire un job du gestionnaire
126 //   void BatchManager::deleteJob(const JobId & jobid)
127 //   {
128 //     // Nothing to do
129 //   }
130
131 //   // Methode pour le controle des jobs : suspend un job en file d'attente
132 //   void BatchManager::holdJob(const JobId & jobid)
133 //   {
134 //     // Nothing to do
135 //   }
136
137 //   // Methode pour le controle des jobs : relache un job suspendu
138 //   void BatchManager::releaseJob(const JobId & jobid)
139 //   {
140 //     // Nothing to do
141 //   }
142
143 //   // Methode pour le controle des jobs : modifie un job en file d'attente
144 //   void BatchManager::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
145 //   {
146 //     // Nothing to do
147 //   }
148
149 //   // Methode pour le controle des jobs : modifie un job en file d'attente
150 //   void BatchManager::alterJob(const JobId & jobid, const Parametre & param)
151 //   {
152 //     // Nothing to do
153 //   }
154
155 //   // Methode pour le controle des jobs : modifie un job en file d'attente
156 //   void BatchManager::alterJob(const JobId & jobid, const Environnement & env)
157 //   {
158 //     // Nothing to do
159 //   }
160
161 //   // Methode pour le controle des jobs : renvoie l'etat du job
162 //   JobInfo BatchManager::queryJob(const JobId & jobid)
163 //   {
164 //     return JobInfo();
165 //   }
166
167   //! Wait for the end of a job
168   /*!
169    *  This method is a simple way to wait for a job to end. It will query the job state at
170    *  increasing intervals and return when the job is finished (whether successfully or not) or
171    *  when the timeout is reached. This method is not intended to be generic. In many cases you
172    *  will have to write your own loop to wait for the end of a job.
173    *  \param jobid ID of the job to wait for.
174    *  \param timeout Maximum time to wait in seconds. If -1 (default), wait indefinitely.
175    *  \param initSleepTime Initial time in seconds between two queries for the job state (default is 1).
176    *  \param maxSleepTime Maximum time in seconds between two queries for the job state (default is 600).
177    *  \return The job state as returned by the last query.
178    */
179   string BatchManager::waitForJobEnd(const JobId & jobid, long timeout,
180                                      long initSleepTime, long maxSleepTime)
181   {
182     int time = 0;
183     int sleeptime = initSleepTime;
184     bool testTimeout = (timeout > -1);
185     bool timeoutReached = (testTimeout && time >= timeout);
186     JobInfo jinfo = jobid.queryJob();
187     string state = jinfo.getParametre()[STATE].str();
188     cout << "State is \"" << state << "\"";
189     while (!timeoutReached && state != FINISHED && state != FAILED) {
190       cout << ", sleeping " << sleeptime << "s..." << endl;
191       sleep(sleeptime);
192       time += sleeptime;
193       timeoutReached = (testTimeout && time >= timeout);
194       sleeptime *= 2;
195       if (testTimeout && sleeptime > timeout - time)
196         sleeptime = timeout - time;
197       if (sleeptime > maxSleepTime)
198         sleeptime = maxSleepTime;
199       jinfo = jobid.queryJob();
200       state = jinfo.getParametre()[STATE].str();
201       cout << "State is \"" << state << "\"";
202     }
203     cout << endl;
204     return state;
205   }
206
207 }