Exemple #1
0
  def test02Props( self ):
    """ props """
    # # valid values
    req = Request()

    req.RequestID = 1
    self.assertEqual( req.RequestID, 1 )

    req.RequestName = "test"
    self.assertEqual( req.RequestName, "test" )

    req.JobID = 1
    self.assertEqual( req.JobID, 1 )

    req.CreationTime = "1970-01-01 00:00:00"
    self.assertEqual( req.CreationTime, datetime.datetime( 1970, 1, 1, 0, 0, 0 ) )
    req.CreationTime = datetime.datetime( 1970, 1, 1, 0, 0, 0 )
    self.assertEqual( req.CreationTime, datetime.datetime( 1970, 1, 1, 0, 0, 0 ) )

    req.SubmitTime = "1970-01-01 00:00:00"
    self.assertEqual( req.SubmitTime, datetime.datetime( 1970, 1, 1, 0, 0, 0 ) )
    req.SubmitTime = datetime.datetime( 1970, 1, 1, 0, 0, 0 )
    self.assertEqual( req.SubmitTime, datetime.datetime( 1970, 1, 1, 0, 0, 0 ) )

    req.LastUpdate = "1970-01-01 00:00:00"
    self.assertEqual( req.LastUpdate, datetime.datetime( 1970, 1, 1, 0, 0, 0 ) )
    req.LastUpdate = datetime.datetime( 1970, 1, 1, 0, 0, 0 )
    self.assertEqual( req.LastUpdate, datetime.datetime( 1970, 1, 1, 0, 0, 0 ) )

    req.Error = ""
Exemple #2
0
def test_Props():
    """props"""
    # # valid values
    req = Request()

    req.RequestID = 1
    assert req.RequestID == 1

    req.RequestName = "test"
    assert req.RequestName == "test"

    req.JobID = 1
    assert req.JobID == 1

    req.CreationTime = "1970-01-01 00:00:00"
    assert req.CreationTime == datetime.datetime(1970, 1, 1, 0, 0, 0)
    req.CreationTime = datetime.datetime(1970, 1, 1, 0, 0, 0)
    assert req.CreationTime == datetime.datetime(1970, 1, 1, 0, 0, 0)

    req.SubmitTime = "1970-01-01 00:00:00"
    assert req.SubmitTime == datetime.datetime(1970, 1, 1, 0, 0, 0)
    req.SubmitTime = datetime.datetime(1970, 1, 1, 0, 0, 0)
    assert req.SubmitTime == datetime.datetime(1970, 1, 1, 0, 0, 0)

    req.LastUpdate = "1970-01-01 00:00:00"
    assert req.LastUpdate == datetime.datetime(1970, 1, 1, 0, 0, 0)
    req.LastUpdate = datetime.datetime(1970, 1, 1, 0, 0, 0)
    assert req.LastUpdate == datetime.datetime(1970, 1, 1, 0, 0, 0)

    req.Error = ""
Exemple #3
0
    def test02Props(self):
        """ props """
        # # valid values
        req = Request()

        req.RequestID = 1
        self.assertEqual(req.RequestID, 1)

        req.RequestName = "test"
        self.assertEqual(req.RequestName, "test")

        req.JobID = 1
        self.assertEqual(req.JobID, 1)
        req.JobID = "1"
        self.assertEqual(req.JobID, 1)

        req.CreationTime = "1970-01-01 00:00:00"
        self.assertEqual(req.CreationTime,
                         datetime.datetime(1970, 1, 1, 0, 0, 0))
        req.CreationTime = datetime.datetime(1970, 1, 1, 0, 0, 0)
        self.assertEqual(req.CreationTime,
                         datetime.datetime(1970, 1, 1, 0, 0, 0))

        req.SubmitTime = "1970-01-01 00:00:00"
        self.assertEqual(req.SubmitTime,
                         datetime.datetime(1970, 1, 1, 0, 0, 0))
        req.SubmitTime = datetime.datetime(1970, 1, 1, 0, 0, 0)
        self.assertEqual(req.SubmitTime,
                         datetime.datetime(1970, 1, 1, 0, 0, 0))

        req.LastUpdate = "1970-01-01 00:00:00"
        self.assertEqual(req.LastUpdate,
                         datetime.datetime(1970, 1, 1, 0, 0, 0))
        req.LastUpdate = datetime.datetime(1970, 1, 1, 0, 0, 0)
        self.assertEqual(req.LastUpdate,
                         datetime.datetime(1970, 1, 1, 0, 0, 0))

        req.Error = ""
Exemple #4
0
    def getRequest(self, requestName='', assigned=True):
        """ read request for execution

    :param str requestName: request's name (default None)
    """
        requestID = None
        log = self.log.getSubLogger(
            'getRequest' if assigned else 'peekRequest')
        if requestName:
            log.verbose("selecting request '%s'%s" %
                        (requestName, ' (Assigned)' if assigned else ''))
            reqIDQuery = "SELECT `RequestID`, `Status` FROM `Request` WHERE `RequestName` = '%s';" % str(
                requestName)
            reqID = self._transaction(reqIDQuery)
            if not reqID["OK"]:
                log.error(reqID["Message"])
                return reqID
            reqID = reqID["Value"].get(reqIDQuery, [])
            if reqID:
                reqID = reqID[0]
            else:
                reqID = {}
            requestID = reqID.get("RequestID")
            status = reqID.get("Status")
            if not all((requestID, status)):
                return S_ERROR("getRequest: Request '%s' does not exist" %
                               requestName)
            if requestID and status and status == "Assigned" and assigned:
                return S_ERROR(
                    "getRequest: status of request '%s' is 'Assigned', request cannot be selected"
                    % requestName)
        else:
            reqIDsQuery = "SELECT `RequestID` FROM `Request` WHERE `Status` = 'Waiting' ORDER BY `LastUpdate` ASC LIMIT 100;"
            reqAscIDs = self._transaction(reqIDsQuery)
            if not reqAscIDs['OK']:
                log.error(reqAscIDs["Message"])
                return reqAscIDs
            reqIDs = set([
                reqID['RequestID'] for reqID in reqAscIDs["Value"][reqIDsQuery]
            ])
            reqIDsQuery = "SELECT `RequestID` FROM `Request` WHERE `Status` = 'Waiting' ORDER BY `LastUpdate` DESC LIMIT 50;"
            reqDescIDs = self._transaction(reqIDsQuery)
            if not reqDescIDs['OK']:
                log.error(reqDescIDs["Message"])
                return reqDescIDs
            reqIDs |= set([
                reqID['RequestID']
                for reqID in reqDescIDs["Value"][reqIDsQuery]
            ])
            if not reqIDs:
                return S_OK()
            reqIDs = list(reqIDs)
            random.shuffle(reqIDs)
            requestID = reqIDs[0]

        selectQuery = [
            "SELECT * FROM `Request` WHERE `RequestID` = %s;" % requestID,
            "SELECT * FROM `Operation` WHERE `RequestID` = %s;" % requestID
        ]
        selectReq = self._transaction(selectQuery)
        if not selectReq["OK"]:
            log.error(selectReq["Message"])
            return S_ERROR(selectReq["Message"])
        selectReq = selectReq["Value"]

        request = Request(selectReq[selectQuery[0]][0])
        origLastUpdate = request.LastUpdate
        if not requestName:
            log.verbose(
                "selected request '%s'%s" %
                (request.RequestName, ' (Assigned)' if assigned else ''))
        for records in sorted(selectReq[selectQuery[1]],
                              key=lambda k: k["Order"]):
            # # order is ro, remove
            del records["Order"]
            operation = Operation(records)
            getFilesQuery = "SELECT * FROM `File` WHERE `OperationID` = %s;" % operation.OperationID
            getFiles = self._transaction(getFilesQuery)
            if not getFiles["OK"]:
                log.error(getFiles["Message"])
                return getFiles
            getFiles = getFiles["Value"][getFilesQuery]
            for getFile in getFiles:
                getFileDict = dict([(key, value)
                                    for key, value in getFile.items()
                                    if value != None])
                operation.addFile(File(getFileDict))
            request.addOperation(operation)

        request.LastUpdate = origLastUpdate

        if assigned:
            setAssigned = self._transaction(
                "UPDATE `Request` SET `Status` = 'Assigned', `LastUpdate`=UTC_TIMESTAMP() WHERE RequestID = %s;"
                % requestID)
            if not setAssigned["OK"]:
                log.error("%s" % setAssigned["Message"])
                return setAssigned

        return S_OK(request)
Exemple #5
0
  def getRequest( self, requestName = '', assigned = True ):
    """ read request for execution

    :param str requestName: request's name (default None)
    """
    requestID = None
    log = self.log.getSubLogger( 'getRequest' if assigned else 'peekRequest' )
    if requestName:
      log.verbose( "selecting request '%s'%s" % ( requestName, ' (Assigned)' if assigned else '' ) )
      reqIDQuery = "SELECT `RequestID`, `Status` FROM `Request` WHERE `RequestName` = '%s';" % str( requestName )
      reqID = self._transaction( reqIDQuery )
      if not reqID["OK"]:
        log.error( reqID["Message"] )
        return reqID
      reqID = reqID["Value"].get( reqIDQuery, [] )
      if reqID:
        reqID = reqID[0]
      else:
        reqID = {}
      requestID = reqID.get( "RequestID" )
      status = reqID.get( "Status" )
      if not all( ( requestID, status ) ):
        return S_ERROR( "getRequest: Request '%s' does not exist" % requestName )
      if requestID and status and status == "Assigned" and assigned:
        return S_ERROR( "getRequest: status of request '%s' is 'Assigned', request cannot be selected" % requestName )
    else:
      reqIDsQuery = "SELECT `RequestID` FROM `Request` WHERE `Status` = 'Waiting' ORDER BY `LastUpdate` ASC LIMIT 100;"
      reqAscIDs = self._transaction( reqIDsQuery )
      if not reqAscIDs['OK']:
        log.error( reqAscIDs["Message"] )
        return reqAscIDs
      reqIDs = set( [reqID['RequestID'] for reqID in reqAscIDs["Value"][reqIDsQuery]] )
      reqIDsQuery = "SELECT `RequestID` FROM `Request` WHERE `Status` = 'Waiting' ORDER BY `LastUpdate` DESC LIMIT 50;"
      reqDescIDs = self._transaction( reqIDsQuery )
      if not reqDescIDs['OK']:
        log.error( reqDescIDs["Message"] )
        return reqDescIDs
      reqIDs |= set( [reqID['RequestID'] for reqID in reqDescIDs["Value"][reqIDsQuery]] )
      if not reqIDs:
        return S_OK()
      reqIDs = list( reqIDs )
      random.shuffle( reqIDs )
      requestID = reqIDs[0]

    selectQuery = [ "SELECT * FROM `Request` WHERE `RequestID` = %s;" % requestID,
                    "SELECT * FROM `Operation` WHERE `RequestID` = %s;" % requestID ]
    selectReq = self._transaction( selectQuery )
    if not selectReq["OK"]:
      log.error( selectReq["Message"] )
      return S_ERROR( selectReq["Message"] )
    selectReq = selectReq["Value"]

    request = Request( selectReq[selectQuery[0]][0] )
    origLastUpdate = request.LastUpdate
    if not requestName:
      log.verbose( "selected request '%s'%s" % ( request.RequestName, ' (Assigned)' if assigned else '' ) )
    for records in sorted( selectReq[selectQuery[1]], key = lambda k: k["Order"] ):
      # # order is ro, remove
      del records["Order"]
      operation = Operation( records )
      getFilesQuery = "SELECT * FROM `File` WHERE `OperationID` = %s;" % operation.OperationID
      getFiles = self._transaction( getFilesQuery )
      if not getFiles["OK"]:
        log.error( getFiles["Message"] )
        return getFiles
      getFiles = getFiles["Value"][getFilesQuery]
      for getFile in getFiles:
        getFileDict = dict( [ ( key, value ) for key, value in getFile.items() if value != None ] )
        operation.addFile( File( getFileDict ) )
      request.addOperation( operation )

    request.LastUpdate = origLastUpdate

    if assigned:
      setAssigned = self._transaction( "UPDATE `Request` SET `Status` = 'Assigned', `LastUpdate`=UTC_TIMESTAMP() WHERE RequestID = %s;" % requestID )
      if not setAssigned["OK"]:
        log.error( "%s" % setAssigned["Message"] )
        return setAssigned

    return S_OK( request )