]> SALOME platform Git repositories - tools/libbatch.git/blob - src/Core/Batch_BatchManager.cxx
Salome HOME
e3fcc58d1abd13ce8971e46660a3d994e9f45413
[tools/libbatch.git] / src / Core / Batch_BatchManager.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.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 #ifdef WIN32
70     WSADATA wsaData;
71     WSAStartup(MAKEWORD(2, 2), &wsaData);  // Initialize Winsock
72 #endif
73
74     // On verifie que le hostname est correct
75     struct hostent* res = gethostbyname(_hostname.c_str());
76
77 #ifdef WIN32
78     WSACleanup();  // Finalize Winsock
79 #endif
80
81     if (!res) { // hostname unknown from network
82       string msg = "hostname \"";
83       msg += _hostname;
84       msg += "\" unknown from the network";
85       throw InvalidArgumentException(msg.c_str());
86     }
87   }
88
89   // Destructeur
90   BatchManager::~BatchManager()
91   {
92     // Nothing to do
93   }
94
95   string BatchManager::__repr__() const
96   {
97     ostringstream oss;
98     oss << "<BatchManager of type '" << (_parent ? _parent->getType() : "unknown (no factory)") << "' connected to server '" << _hostname << "'>";
99     return oss.str();
100   }
101
102   // Recupere le l'identifiant d'un job deja soumis au BatchManager
103 //   const JobId BatchManager::getJobIdByReference(const string & ref)
104 //   {
105 //     return JobId(this, ref);
106 //   }
107   const JobId BatchManager::getJobIdByReference(const char * ref)
108   {
109     return JobId(this, ref);
110   }
111
112 //   // Methode pour le controle des jobs : soumet un job au gestionnaire
113 //   const JobId BatchManager::submitJob(const Job & job)
114 //   {
115 //     static int idx = 0;
116 //     //MEDMEM::STRING sst;
117 //     ostringstream sst;
118 //     sst << "Jobid_" << idx++;
119 //     JobId id(this, sst.str());
120 //     return id;
121 //   }
122
123 //   // Methode pour le controle des jobs : retire un job du gestionnaire
124 //   void BatchManager::deleteJob(const JobId & jobid)
125 //   {
126 //     // Nothing to do
127 //   }
128
129 //   // Methode pour le controle des jobs : suspend un job en file d'attente
130 //   void BatchManager::holdJob(const JobId & jobid)
131 //   {
132 //     // Nothing to do
133 //   }
134
135 //   // Methode pour le controle des jobs : relache un job suspendu
136 //   void BatchManager::releaseJob(const JobId & jobid)
137 //   {
138 //     // Nothing to do
139 //   }
140
141 //   // Methode pour le controle des jobs : modifie un job en file d'attente
142 //   void BatchManager::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
143 //   {
144 //     // Nothing to do
145 //   }
146
147 //   // Methode pour le controle des jobs : modifie un job en file d'attente
148 //   void BatchManager::alterJob(const JobId & jobid, const Parametre & param)
149 //   {
150 //     // Nothing to do
151 //   }
152
153 //   // Methode pour le controle des jobs : modifie un job en file d'attente
154 //   void BatchManager::alterJob(const JobId & jobid, const Environnement & env)
155 //   {
156 //     // Nothing to do
157 //   }
158
159 //   // Methode pour le controle des jobs : renvoie l'etat du job
160 //   JobInfo BatchManager::queryJob(const JobId & jobid)
161 //   {
162 //     return JobInfo();
163 //   }
164
165   //! Wait for the end of a job
166   /*!
167    *  This method is a simple way to wait for a job to end. It will query the job state at
168    *  increasing intervals and return when the job is finished (whether successfully or not) or
169    *  when the timeout is reached. This method is not intended to be generic. In many cases you
170    *  will have to write your own loop to wait for the end of a job.
171    *  \param jobid ID of the job to wait for.
172    *  \param timeout Maximum time to wait in seconds. If -1 (default), wait indefinitely.
173    *  \param initSleepTime Initial time in seconds between two queries for the job state (default is 1).
174    *  \param maxSleepTime Maximum time in seconds between two queries for the job state (default is 600).
175    *  \return The job state as returned by the last query.
176    */
177   string BatchManager::waitForJobEnd(const JobId & jobid, long timeout,
178                                      long initSleepTime, long maxSleepTime)
179   {
180     int time = 0;
181     int sleeptime = initSleepTime;
182     bool testTimeout = (timeout > -1);
183     bool timeoutReached = (testTimeout && time >= timeout);
184     JobInfo jinfo = jobid.queryJob();
185     string state = jinfo.getParametre()[STATE].str();
186     cout << "State is \"" << state << "\"";
187     while (!timeoutReached && state != FINISHED && state != FAILED) {
188       cout << ", sleeping " << sleeptime << "s..." << endl;
189       sleep(sleeptime);
190       time += sleeptime;
191       timeoutReached = (testTimeout && time >= timeout);
192       sleeptime *= 2;
193       if (testTimeout && sleeptime > timeout - time)
194         sleeptime = timeout - time;
195       if (sleeptime > maxSleepTime)
196         sleeptime = maxSleepTime;
197       jinfo = jobid.queryJob();
198       state = jinfo.getParametre()[STATE].str();
199       cout << "State is \"" << state << "\"";
200     }
201     cout << endl;
202     return state;
203   }
204
205 }