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