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