# 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 {
// 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;
+ }
+
}
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
#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;
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;
#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;
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;
#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;
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;
#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;
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;
#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;
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;
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"