/*********** Filename: JobStatus.cpp ***********/ #include #include #include "glite/lb/JobStatus.h" #include "glite/lb/Event.h" #include "glite/wmsutils/jobid/JobId.h" #include "glite/wmsutils/exception/Exception.h" #include "glite/wmsui/api/Job.h" using namespace std; using namespace glite::lb; using namespace glite::wmsutils::jobid; using namespace glite::wmsutils::exception; using namespace glite::wmsui::api; /* jobStatusLoggingOutput.cpp created 12:00 on 26th August 2005 by Sara Collins * based on the edg tutorial code JobStatus.cpp * * Program to check the status of a job and extract the logging info when * it has completed and the output. * * TUTORIAL: use classes detailed in * http://egee-jra1-wm.mi.infn.it/egee-jra1-wm/glite-wms-api-index.shtml * Hints are given at each stage. */ /* Method to print out the attributes of logging Events */ void printAttr(int i, int j, vector< pair < Event::Attr, Event::AttrType > > attrList, Events &log){ switch (attrList[i].second){ case Event::INT_T : cout << log[j].getValInt(attrList[i].first) << endl ; break; case Event::STRING_T : cout << log[j].getValString(attrList[i].first) << endl ; break; case Event::TIMEVAL_T : { timeval t = log[j].getValTime(attrList[i].first); cout << asctime(gmtime(&t.tv_sec)) << endl ; } break; case Event::PORT_T : cout << log[j].getValInt(attrList[i].first) << endl ; break; case Event::LOGSRC_T : cout << log[j].getValInt(attrList[i].first) << endl ; break; case Event::JOBID_T : { if(((JobId)log[j].getValJobId(attrList[i].first)).isSet()) cout << log[j].getValJobId(attrList[i].first).toString() << endl ; } break; case Event::NOTIFID_T : { cout << log[j].getValInt(attrList[i].first) << endl ; } break ; default : /* something is wrong */ break; } } /* Method to print out logging information */ void printLogInfo(Events &log, int level){ struct timezone *tz; const string source_dest[] = {"None","UserInterface", "NetworkServer", "WorkloadManager","Unknown", "JobController", "LogMonitor", "LRMS", "Application"}; const string result[] = {"UNKNOWN","START","OK","REFUSED","FAIL"}; for(unsigned j = 0; j < log.size(); j++){ vector< pair < Event::Attr, Event::AttrType > > attrList = log[j].getAttrs(); cout << "Event : "< 0) { cout << attrName << " == " ; printAttr(i,j,attrList,log); } } } } /* Method for printing out the status information */ void printStatus(JobStatus &stat, int level){ vector< pair < JobStatus::Attr, JobStatus::AttrType > > attrList = stat.getAttrs(); cout << "\nJob Status Information:\n - Current Status = " << stat.name() << "\n ---" << endl<< flush ; if (level>0) for (unsigned i=0; i < attrList.size(); i++ ) { cout << stat.getAttrName(attrList[i].first) << " == " ; switch (attrList[i].second) { case JobStatus::INT_T : cout << stat.getValInt(attrList[i].first) << endl << flush; break; case JobStatus::STRING_T : cout << stat.getValString(attrList[i].first) << endl << flush; break; case JobStatus::TIMEVAL_T : { timeval t = stat.getValTime(attrList[i].first); cout << t.tv_sec << "." << t.tv_usec << " s " << endl << flush; } break; case JobStatus::BOOL_T : cout << stat.getValBool(attrList[i].first) << endl << flush; break; case JobStatus::JOBID_T : { if(((JobId)stat.getValJobId(attrList[i].first)).isSet()) cout << stat.getValJobId(attrList[i].first).toString() << endl << flush; } break; case JobStatus::STSLIST_T : { std::vector< JobStatus > v = stat.getValJobStatusList(attrList[i].first); for(unsigned int i=0; i < v.size(); i++) ; } break ; case JobStatus::TAGLIST_T : { std::vector< std::pair < std::string,std::string > > v = stat.getValTagList(attrList[i].first ) ; for (unsigned int i = 0 ; i < v.size() ; i++ ) cout << v[i].first << " - " << v[i].second << " ; " << flush ; } break; case JobStatus::INTLIST_T : { std::vector< int > v = stat.getValIntList(attrList[i].first); for(unsigned int j=0; j < v.size(); j++) cout << " " << v[j] << flush ; cout << endl << flush; } break; case JobStatus::STRLIST_T : { std::vector< std::string > v = stat.getValStringList(attrList[i].first); for(unsigned int j=0; j < v.size(); j++) cout << v[j] ; cout << endl << flush; } break ; default : /* something is wrong */ break; } } cout << endl << flush; } int main(int argc,char *argv[]) { //check the number of arguments is correct if (argc != 3 || strcmp(argv[1],"--help") == 0){ cout << "Usage : " << argv[0] << " " << endl << flush; return 1; } /* Set the logging information level */ const int level = 0; /* ID of a submitted job */ const string id = argv[1]; /* directory for output */ const string outputDir = argv[2]; try{ /* Create a JobId from the id of a submitted job. */ JobId jid (id); /* Create a Job from the JobId */ glite::wmsui::api::Job job(jid); /* Retrieve the Job status information from the LB (encoded in JobId) * HINT: use Job method and JobStatus */ JobStatus status = job.getStatus(); /* Check the status of the job every 5 seconds until the job * has finished/aborted (indicated by a status code >= 6). * HINT: use Job and JobStatus methods and attributes, and std sleep * */ int state = 0; do { status = job.getStatus(); cout << "\nCurrent job status = " << status.name() << " " << flush; if (state < status.status) { state = status.status; } sleep(5); } while (status.status < 6); /* Output a warning if status code > 6 and output detailed status info * HINT: use JobStatus attribute and printStatus() method defined above. */ if (status.status > 6){ printStatus(status,6); cerr << "Status level suggest job might not have succeeded" << " or has cleared " << endl; } /* Extract logging info. * HINT: use Job method and Events. */ cout << endl << "Obtaining the logging information" << endl; Events logInfo = job.getLogInfo(); /* Print out logging info * HINT: use printLogInfo() method defined above, */ printLogInfo(logInfo,level); /* Get the output of the job using a system call. */ /* Command to retrieve output */ string command = "glite-job-output --dir "; command.append(outputDir); command.append(" "+jid.toString()); /* Execute command and check for errors */ if(system(command.c_str()) == -1){ cout << "Error trying to get output"<