Salome HOME
Added method waitForJobEnd in BatchManager class. V1_1_0rc1 V5_1_4a1
authorbarate <barate>
Thu, 15 Apr 2010 16:16:52 +0000 (16:16 +0000)
committerbarate <barate>
Thu, 15 Apr 2010 16:16:52 +0000 (16:16 +0000)
src/Core/Batch_BatchManager.cxx
src/Core/Batch_BatchManager.hxx
src/Local/Test/Test_Local_RSH.cxx
src/Local/Test/Test_Local_SH.cxx
src/Local/Test/Test_Local_SSH.cxx
src/PBS/Test/Test_PBS.cxx
src/PBS/Test/Test_ePBS.cxx
src/Python/Test/Test_Python_Local_SH.py

index a7e6d61f243d9a758e342015f8701e72982d2776..8ce6394151d6bf18c77e2da1a209281bbc9f00ab 100644 (file)
 # include <netdb.h>
 #endif
 
-//#include "MEDMEM_STRING.hxx"
 #include "Batch_Job.hxx"
 #include "Batch_JobId.hxx"
 #include "Batch_JobInfo.hxx"
 #include "Batch_InvalidArgumentException.hxx"
 #include "Batch_FactBatchManager.hxx"
 #include "Batch_BatchManager.hxx"
+
+#ifdef WIN32
+#define sleep(seconds) Sleep((seconds)*1000)
+#endif
+
 using namespace std;
 
 namespace Batch {
@@ -157,4 +161,44 @@ namespace Batch {
 //     return JobInfo();
 //   }
 
+  //! Wait for the end of a job
+  /*!
+   *  This method is a simple way to wait for a job to end. It will query the job state at
+   *  increasing intervals and return when the job is finished (whether successfully or not) or
+   *  when the timeout is reached. This method is not intended to be generic. In many cases you
+   *  will have to write your own loop to wait for the end of a job.
+   *  \param jobid ID of the job to wait for.
+   *  \param timeout Maximum time to wait in seconds. If -1 (default), wait indefinitely.
+   *  \param initSleepTime Initial time in seconds between two queries for the job state (default is 1).
+   *  \param maxSleepTime Maximum time in seconds between two queries for the job state (default is 600).
+   *  \return The job state as returned by the last query.
+   */
+  string BatchManager::waitForJobEnd(const JobId & jobid, long timeout,
+                                     long initSleepTime, long maxSleepTime)
+  {
+    int time = 0;
+    int sleeptime = initSleepTime;
+    bool testTimeout = (timeout > -1);
+    bool timeoutReached = (testTimeout && time >= timeout);
+    JobInfo jinfo = jobid.queryJob();
+    string state = jinfo.getParametre()[STATE].str();
+    cout << "State is \"" << state << "\"";
+    while (!timeoutReached && state != FINISHED && state != FAILED) {
+      cout << ", sleeping " << sleeptime << "s..." << endl;
+      sleep(sleeptime);
+      time += sleeptime;
+      timeoutReached = (testTimeout && time >= timeout);
+      sleeptime *= 2;
+      if (testTimeout && sleeptime > timeout - time)
+        sleeptime = timeout - time;
+      if (sleeptime > maxSleepTime)
+        sleeptime = maxSleepTime;
+      jinfo = jobid.queryJob();
+      state = jinfo.getParametre()[STATE].str();
+      cout << "State is \"" << state << "\"";
+    }
+    cout << endl;
+    return state;
+  }
+
 }
index bda29616c016cadabc2e596db84838c3321ef6ea..4c8bc53b174b6bcc2df337e7d48d2ae6858953a7 100644 (file)
@@ -69,6 +69,8 @@ namespace Batch {
     virtual void alterJob(const Batch::JobId & jobid, const Batch::Parametre & param) = 0; // modifie un job en file d'attente
     virtual void alterJob(const Batch::JobId & jobid, const Batch::Environnement & env) = 0; // modifie un job en file d'attente
     virtual Batch::JobInfo queryJob(const Batch::JobId & jobid) = 0; // renvoie l'etat du job
+    virtual std::string waitForJobEnd(const Batch::JobId & jobid, long timeout = -1,
+                                      long initSleepTime = 1, long maxSleepTime = 600);
 
   protected:
     std::string _hostname; // serveur ou tourne le BatchManager
index 700daf57d6927c9549f311147a5a160f92fec388..fe8acb64fd085c47844bf04c8239df92574a9bc2 100644 (file)
 
 #include <SimpleParser.hxx>
 
-#ifdef WIN32
-#include <Windows.h>
-#define sleep(seconds) Sleep((seconds)*1000)
-#define usleep(useconds) Sleep((useconds)/1000)
-#endif
-
 using namespace std;
 using namespace Batch;
 
@@ -100,13 +94,7 @@ int main(int argc, char** argv)
     cout << jobid.__repr__() << endl;
 
     // Wait for the end of the job
-    string state = "Unknown";
-    for (int i=0 ; i<timeout*10 && state != FINISHED && state != FAILED ; i++) {
-      usleep(100000);
-      Versatile paramState = jobid.queryJob().getParametre()[STATE];
-      state = (paramState.size() > 0) ? paramState.str() : "Unknown";
-      cout << "Job state is: " << state << endl;
-    }
+    string state = bm->waitForJobEnd(jobid, timeout);
 
     if (state != FINISHED && state != FAILED) {
       cerr << "Error: Job not finished after timeout" << endl;
index bcac2bfa984e7072ad538c6f762d405f8a1c524f..22e70760fcdbac11e816ac897b9f76f45581b7c4 100644 (file)
 #include <Test_Local_config.h>
 #include <SimpleParser.hxx>
 
-#ifdef WIN32
-#include <Windows.h>
-#define sleep(seconds) Sleep((seconds)*1000)
-#define usleep(useconds) Sleep((useconds)/1000)
-#endif
-
 using namespace std;
 using namespace Batch;
 
@@ -100,13 +94,7 @@ int main(int argc, char** argv)
     cout << jobid.__repr__() << endl;
 
     // Wait for the end of the job
-    string state = "Unknown";
-    for (int i=0 ; i<timeout*10 && state != FINISHED && state != FAILED ; i++) {
-      usleep(100000);
-      Versatile paramState = jobid.queryJob().getParametre()[STATE];
-      state = (paramState.size() > 0) ? paramState.str() : "Unknown";
-      cout << "Job state is: " << state << endl;
-    }
+    string state = bm->waitForJobEnd(jobid, timeout);
 
     if (state != FINISHED && state != FAILED) {
       cerr << "Error: Job not finished after timeout" << endl;
index 9db89170df071dd36c09069886d58475e17213d4..358a44b9b922b3cd66d268fd16ce67534bad9f26 100644 (file)
 
 #include <SimpleParser.hxx>
 
-#ifdef WIN32
-#include <Windows.h>
-#define sleep(seconds) Sleep((seconds)*1000)
-#define usleep(useconds) Sleep((useconds)/1000)
-#endif
-
 using namespace std;
 using namespace Batch;
 
@@ -103,13 +97,7 @@ int main(int argc, char** argv)
     cout << jobid.__repr__() << endl;
 
     // Wait for the end of the job
-    string state = "Unknown";
-    for (int i=0 ; i<timeout*10 && state != FINISHED && state != FAILED ; i++) {
-      usleep(100000);
-      Versatile paramState = jobid.queryJob().getParametre()[STATE];
-      state = (paramState.size() > 0) ? paramState.str() : "Unknown";
-      cout << "Job state is: " << state << endl;
-    }
+    string state = bm->waitForJobEnd(jobid, timeout);
 
     if (state != FINISHED && state != FAILED) {
       cerr << "Error: Job not finished after timeout" << endl;
index 3af87f4c5dbe10f065d632e54c8f7399fc5fc14d..cb4b41a3414496bd560b69a1acfe274e06ab8fc2 100644 (file)
 
 #include <SimpleParser.hxx>
 
-#ifdef WIN32
-#include <Windows.h>
-#include <direct.h>
-#define sleep(seconds) Sleep((seconds)*1000)
-#define usleep(useconds) Sleep((useconds)/1000)
-#endif
-
 using namespace std;
 using namespace Batch;
 
-const int MAX_SLEEP_TIME = 600;
-
 int main(int argc, char** argv)
 {
   cout << "*******************************************************************************************" << endl;
@@ -110,28 +101,7 @@ int main(int argc, char** argv)
     cout << jobid.__repr__() << endl;
 
     // Wait for the end of the job
-    int time = 0;
-    int sleeptime = 1;
-    bool testTimeout = (timeout > -1);
-    bool timeoutReached = (testTimeout && time >= timeout);
-    JobInfo jinfo = jobid.queryJob();
-    string state = jinfo.getParametre()[STATE].str();
-    cout << "State is \"" << state << "\"";
-    while (!timeoutReached && state != FINISHED && state != FAILED) {
-      cout << ", sleeping " << sleeptime << "s..." << endl;
-      sleep(sleeptime);
-      time += sleeptime;
-      timeoutReached = (testTimeout && time >= timeout);
-      sleeptime *= 2;
-      if (testTimeout && sleeptime > timeout - time)
-        sleeptime = timeout - time;
-      if (sleeptime > MAX_SLEEP_TIME)
-        sleeptime = MAX_SLEEP_TIME;
-      jinfo = jobid.queryJob();
-      state = jinfo.getParametre()[STATE].str();
-      cout << "State is \"" << state << "\"";
-    }
-    cout << endl;
+    string state = bm->waitForJobEnd(jobid, timeout);
 
     if (state == FINISHED || state == FAILED) {
       cout << "Job " << jobid.__repr__() << " is done" << endl;
index 1f7863298b998781e9819a9237b8f72d61af7f6c..8f9a9ed256703381771f69077b0ab42af299eeaa 100644 (file)
 
 #include <SimpleParser.hxx>
 
-#ifdef WIN32
-#include <Windows.h>
-#define sleep(seconds) Sleep((seconds)*1000)
-#define usleep(useconds) Sleep((useconds)/1000)
-#endif
-
 using namespace std;
 using namespace Batch;
 
-const int MAX_SLEEP_TIME = 600;
-
 void print_usage()
 {
   cout << "usage: Test_ePBS PROTOCOL" << endl;
@@ -129,28 +121,7 @@ int main(int argc, char** argv)
     cout << jobid.__repr__() << endl;
 
     // Wait for the end of the job
-    int time = 0;
-    int sleeptime = 1;
-    bool testTimeout = (timeout > -1);
-    bool timeoutReached = (testTimeout && time >= timeout);
-    JobInfo jinfo = jobid.queryJob();
-    string state = jinfo.getParametre()[STATE].str();
-    cout << "State is \"" << state << "\"";
-    while (!timeoutReached && state != FINISHED && state != FAILED) {
-      cout << ", sleeping " << sleeptime << "s..." << endl;
-      sleep(sleeptime);
-      time += sleeptime;
-      timeoutReached = (testTimeout && time >= timeout);
-      sleeptime *= 2;
-      if (testTimeout && sleeptime > timeout - time)
-        sleeptime = timeout - time;
-      if (sleeptime > MAX_SLEEP_TIME)
-        sleeptime = MAX_SLEEP_TIME;
-      jinfo = jobid.queryJob();
-      state = jinfo.getParametre()[STATE].str();
-      cout << "State is \"" << state << "\"";
-    }
-    cout << endl;
+    string state = bm->waitForJobEnd(jobid, timeout);
 
     if (state == FINISHED || state == FAILED) {
       cout << "Job " << jobid.__repr__() << " is done" << endl;
index 11194e40763a8297ea73679ba4a585d8d550fc4f..25fc2b3376d3a571a761c4f6c14d9bbf71eaf3ff 100644 (file)
@@ -69,17 +69,7 @@ def work():
     jobid.queryJob()
 
     # Wait for the end of the job
-    state = 'Unknown'
-    i=0
-    while state != FINISHED and state != FAILED and i<config.TEST_LOCAL_SH_TIMEOUT*10:
-        time.sleep(0.1)
-        i+=1
-        jinfo = jobid.queryJob()
-        try:
-            state = jinfo.getParametre()[STATE]
-        except KeyError:
-            pass
-        print "State is", state
+    state = bm.waitForJobEnd(jobid, config.TEST_LOCAL_SH_TIMEOUT);
 
     if state != FINISHED and state != FAILED:
         print "Error: Job not finished after timeout"