Repository: dmwm/wmcore
Issue: Problems reading condor logs with PyCondorPlugin

Two parts of the code to fix:
- We have to read the condor logs from the last retry of the job. We should organize by creation date/timestamp rather than by condor job number. 
- Why a condor.x.x.log can be empty? probably some weird case? anyway, the plugin need to handle this. 


Changed files:
src/python/WMCore/BossAir/Plugins/PyCondorPlugin.py

diff --git a/src/python/WMCore/BossAir/Plugins/PyCondorPlugin.py b/src/python/WMCore/BossAir/Plugins/PyCondorPlugin.py
index 4ca521f2c..2494edc46 100644
--- a/src/python/WMCore/BossAir/Plugins/PyCondorPlugin.py
+++ b/src/python/WMCore/BossAir/Plugins/PyCondorPlugin.py
@@ -1176,31 +1176,42 @@ class PyCondorPlugin(BasePlugin):
             return n
 
 
-        jobLogInfo={}
+        ### This should select the latest log file in the cache_dir
+        fmtime=0
+        logFile=None
         for joblog in os.listdir(job['cache_dir']):
             if fnmatch.fnmatch(joblog, 'condor.*.*.log'):
-                logFile=os.path.join(job['cache_dir'],joblog)
-                tmpDict={}
-                try :
-                    logging.debug("Opening condor job log file: %s" % logFile)
-                    logfileobj=open(logFile,"r")
-                except :
-                    logging.debug('Cannot open condor job log file %s'% logFile)
-                else :
-                    cres=condor.read_events(logfileobj,1)
-                    ulog=list(cres)
-                    
-                    _tmpStat = int(ulog[-1]["TriggerEventTypeNumber"])
-                    tmpDict["JobStatus"]=LogToScheddExitCodeMap(_tmpStat)
-                    tmpDict["submitTime"]=int(ulog[-1]["QDate"])
-                    tmpDict["runningTime"]=int(ulog[-1]["JobStartDate"]) if ulog[-1]["JobStartDate"] is not None else 0
-                    tmpDict["stateTime"]=int(ulog[-1]["EnteredCurrentStatus"])
-                    tmpDict["runningCMSSite"]=ulog[-1]["MachineAttrGLIDEIN_CMSSite0"]
-                    tmpDict["WMAgentID"]=int(ulog[-1]["WMAgent_JobID"])
-                    _tmpID = tmpDict["WMAgentID"]
-                    jobLogInfo[_tmpID] = tmpDict
+                _tmplogFile=os.path.join(job['cache_dir'],joblog)
+                _tmpfmtime=int(os.path.getmtime(_tmplogFile))
+                if _tmpfmtime > fmtime:
+                    fmtime = _tmpfmtime
+                    logFile = _tmplogFile
 
-                logging.info("Retrieved %i Info from Condor Job Log file %s" % (len(jobLogInfo), logFile))
+
+        jobLogInfo={}
+        try :
+            logging.debug("Opening condor job log file: %s" % logFile)
+            logfileobj=open(logFile,"r")
+        except :
+            logging.debug('Cannot open condor job log file %s'% logFile)
+        else :
+            tmpDict={}
+            cres=condor.read_events(logfileobj,1)
+            ulog=list(cres)
+            if len(ulog) > 0:
+                _tmpStat = int(ulog[-1]["TriggerEventTypeNumber"])
+                tmpDict["JobStatus"]=LogToScheddExitCodeMap(_tmpStat)
+                tmpDict["submitTime"]=int(ulog[-1]["QDate"])
+                tmpDict["runningTime"]=int(ulog[-1]["JobStartDate"]) if ulog[-1]["JobStartDate"] is not None else 0
+                tmpDict["stateTime"]=int(ulog[-1]["EnteredCurrentStatus"])
+                tmpDict["runningCMSSite"]=ulog[-1]["MachineAttrGLIDEIN_CMSSite0"]
+                tmpDict["WMAgentID"]=int(ulog[-1]["WMAgent_JobID"])
+                _tmpID = tmpDict["WMAgentID"]
+                jobLogInfo[_tmpID] = tmpDict
+            else :
+                logging.debug('%s is EMPTY' % str(logFile))
+
+        logging.info("Retrieved %i Info from Condor Job Log file %s" % (len(jobLogInfo), logFile))
                     
         return jobLogInfo