From b203d2743acd4b7e302359d88aae8599dab042ef Mon Sep 17 00:00:00 2001 From: barate Date: Thu, 15 Apr 2010 16:16:52 +0000 Subject: [PATCH] Added method waitForJobEnd in BatchManager class. --- src/Core/Batch_BatchManager.cxx | 46 ++++++++++++++++++++++++- src/Core/Batch_BatchManager.hxx | 2 ++ src/Local/Test/Test_Local_RSH.cxx | 14 +------- src/Local/Test/Test_Local_SH.cxx | 14 +------- src/Local/Test/Test_Local_SSH.cxx | 14 +------- src/PBS/Test/Test_PBS.cxx | 32 +---------------- src/PBS/Test/Test_ePBS.cxx | 31 +---------------- src/Python/Test/Test_Python_Local_SH.py | 12 +------ 8 files changed, 53 insertions(+), 112 deletions(-) diff --git a/src/Core/Batch_BatchManager.cxx b/src/Core/Batch_BatchManager.cxx index a7e6d61..8ce6394 100644 --- a/src/Core/Batch_BatchManager.cxx +++ b/src/Core/Batch_BatchManager.cxx @@ -37,13 +37,17 @@ # include #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; + } + } diff --git a/src/Core/Batch_BatchManager.hxx b/src/Core/Batch_BatchManager.hxx index bda2961..4c8bc53 100644 --- a/src/Core/Batch_BatchManager.hxx +++ b/src/Core/Batch_BatchManager.hxx @@ -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 diff --git a/src/Local/Test/Test_Local_RSH.cxx b/src/Local/Test/Test_Local_RSH.cxx index 700daf5..fe8acb6 100644 --- a/src/Local/Test/Test_Local_RSH.cxx +++ b/src/Local/Test/Test_Local_RSH.cxx @@ -37,12 +37,6 @@ #include -#ifdef WIN32 -#include -#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 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; diff --git a/src/Local/Test/Test_Local_SH.cxx b/src/Local/Test/Test_Local_SH.cxx index bcac2bf..22e7076 100644 --- a/src/Local/Test/Test_Local_SH.cxx +++ b/src/Local/Test/Test_Local_SH.cxx @@ -38,12 +38,6 @@ #include #include -#ifdef WIN32 -#include -#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 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; diff --git a/src/Local/Test/Test_Local_SSH.cxx b/src/Local/Test/Test_Local_SSH.cxx index 9db8917..358a44b 100644 --- a/src/Local/Test/Test_Local_SSH.cxx +++ b/src/Local/Test/Test_Local_SSH.cxx @@ -38,12 +38,6 @@ #include -#ifdef WIN32 -#include -#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 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; diff --git a/src/PBS/Test/Test_PBS.cxx b/src/PBS/Test/Test_PBS.cxx index 3af87f4..cb4b41a 100644 --- a/src/PBS/Test/Test_PBS.cxx +++ b/src/PBS/Test/Test_PBS.cxx @@ -37,18 +37,9 @@ #include -#ifdef WIN32 -#include -#include -#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; diff --git a/src/PBS/Test/Test_ePBS.cxx b/src/PBS/Test/Test_ePBS.cxx index 1f78632..8f9a9ed 100644 --- a/src/PBS/Test/Test_ePBS.cxx +++ b/src/PBS/Test/Test_ePBS.cxx @@ -40,17 +40,9 @@ #include -#ifdef WIN32 -#include -#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; diff --git a/src/Python/Test/Test_Python_Local_SH.py b/src/Python/Test/Test_Python_Local_SH.py index 11194e4..25fc2b3 100644 --- a/src/Python/Test/Test_Python_Local_SH.py +++ b/src/Python/Test/Test_Python_Local_SH.py @@ -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