def export_setJobStatusBulk( self, jobID, statusDict ):
    """ Set various status fields for job specified by its JobId.
        Set only the last status in the JobDB, updating all the status
        logging information in the JobLoggingDB. The statusDict has datetime
        as a key and status information dictionary as values
    """

    status = ""
    minor = ""
    application = ""
    appCounter = ""
    endDate = ''
    startDate = ''
    startFlag = ''
    jobID = int( jobID )

    result = jobDB.getJobAttributes( jobID, ['Status'] )
    if not result['OK']:
      return result

    if not result['Value']:
      # if there is no matching Job it returns an empty dictionary
      return S_ERROR( 'No Matching Job' )

    new_status = result['Value']['Status']
    if new_status == "Stalled":
      status = 'Running'

    # Get the latest WN time stamps of status updates
    result = logDB.getWMSTimeStamps( int( jobID ) )
    if not result['OK']:
      return result
    lastTime = max( [float( t ) for s, t in result['Value'].items() if s != 'LastTime'] )
    from DIRAC import Time
    lastTime = Time.toString( Time.fromEpoch( lastTime ) )

    # Get the last status values
    dates = sorted( statusDict )
    # We should only update the status if its time stamp is more recent than the last update
    for date in [date for date in dates if date >= lastTime]:
      sDict = statusDict[date]
      if sDict['Status']:
        status = sDict['Status']
        if status in JOB_FINAL_STATES:
          endDate = date
        if status == "Running":
          startFlag = 'Running'
      if sDict['MinorStatus']:
        minor = sDict['MinorStatus']
        if minor == "Application" and startFlag == 'Running':
          startDate = date
      if sDict['ApplicationStatus']:
        application = sDict['ApplicationStatus']
      counter = sDict.get( 'ApplicationCounter' )
      if counter:
        appCounter = counter
    attrNames = []
    attrValues = []
    if status:
      attrNames.append( 'Status' )
      attrValues.append( status )
    if minor:
      attrNames.append( 'MinorStatus' )
      attrValues.append( minor )
    if application:
      attrNames.append( 'ApplicationStatus' )
      attrValues.append( application )
    if appCounter:
      attrNames.append( 'ApplicationCounter' )
      attrValues.append( appCounter )
    result = jobDB.setJobAttributes( jobID, attrNames, attrValues, update = True )
    if not result['OK']:
      return result

    if endDate:
      result = jobDB.setEndExecTime( jobID, endDate )
    if startDate:
      result = jobDB.setStartExecTime( jobID, startDate )

    # Update the JobLoggingDB records
    for date in dates:
      sDict = statusDict[date]
      status = sDict['Status']
      if not status:
        status = 'idem'
      minor = sDict['MinorStatus']
      if not minor:
        minor = 'idem'
      application = sDict['ApplicationStatus']
      if not application:
        application = 'idem'
      else:
        status = "Running"
        minor = "Application"
      source = sDict['Source']
      result = logDB.addLoggingRecord( jobID, status, minor, application, date, source )
      if not result['OK']:
        return result

    return S_OK()
Example #2
0
    def export_setJobStatusBulk(self, jobID, statusDict):
        """ Set various status fields for job specified by its JobId.
        Set only the last status in the JobDB, updating all the status
        logging information in the JobLoggingDB. The statusDict has datetime
        as a key and status information dictionary as values
    """

        status = ""
        minor = ""
        application = ""
        appCounter = ""
        endDate = ''
        startDate = ''
        startFlag = ''
        jobID = int(jobID)

        result = jobDB.getJobAttributes(jobID, ['Status'])
        if not result['OK']:
            return result

        if not result['Value']:
            # if there is no matching Job it returns an empty dictionary
            return S_ERROR('No Matching Job')

        new_status = result['Value']['Status']
        if new_status == "Stalled":
            status = 'Running'

        # Get the latest WN time stamps of status updates
        result = logDB.getWMSTimeStamps(int(jobID))
        if not result['OK']:
            return result
        lastTime = max(
            [float(t) for s, t in result['Value'].items() if s != 'LastTime'])
        from DIRAC import Time
        lastTime = Time.toString(Time.fromEpoch(lastTime))

        # Get the last status values
        dates = sorted(statusDict)
        # We should only update the status if its time stamp is more recent than the last update
        for date in [date for date in dates if date >= lastTime]:
            sDict = statusDict[date]
            if sDict['Status']:
                status = sDict['Status']
                if status in JOB_FINAL_STATES:
                    endDate = date
                if status == "Running":
                    startFlag = 'Running'
            if sDict['MinorStatus']:
                minor = sDict['MinorStatus']
                if minor == "Application" and startFlag == 'Running':
                    startDate = date
            if sDict['ApplicationStatus']:
                application = sDict['ApplicationStatus']
            counter = sDict.get('ApplicationCounter')
            if counter:
                appCounter = counter
        attrNames = []
        attrValues = []
        if status:
            attrNames.append('Status')
            attrValues.append(status)
        if minor:
            attrNames.append('MinorStatus')
            attrValues.append(minor)
        if application:
            attrNames.append('ApplicationStatus')
            attrValues.append(application)
        if appCounter:
            attrNames.append('ApplicationCounter')
            attrValues.append(appCounter)
        result = jobDB.setJobAttributes(jobID,
                                        attrNames,
                                        attrValues,
                                        update=True)
        if not result['OK']:
            return result

        if endDate:
            result = jobDB.setEndExecTime(jobID, endDate)
        if startDate:
            result = jobDB.setStartExecTime(jobID, startDate)

        # Update the JobLoggingDB records
        for date in dates:
            sDict = statusDict[date]
            status = sDict['Status']
            if not status:
                status = 'idem'
            minor = sDict['MinorStatus']
            if not minor:
                minor = 'idem'
            application = sDict['ApplicationStatus']
            if not application:
                application = 'idem'
            else:
                status = "Running"
                minor = "Application"
            source = sDict['Source']
            result = logDB.addLoggingRecord(jobID, status, minor, application,
                                            date, source)
            if not result['OK']:
                return result

        return S_OK()