def getRequest(self, requestType): """ Obtain a request from the database of a :requestType: type :param self: self reference :param str requestType: request type """ self.log.info("getRequest: Attempting to get %s type request." % requestType) try: # Determine the request name to be obtained candidateRequests = [] reqDir = os.path.join(self.root, requestType, "Waiting") self.getIdLock.acquire() if os.path.exists(reqDir): candidateRequests = [ os.path.basename(requestFile) for requestFile in sorted( filter( os.path.isfile, [os.path.join(reqDir, requestName) for requestName in os.listdir(reqDir)] ), key=os.path.getctime, ) ] if not len(candidateRequests) > 0: self.getIdLock.release() self.log.info("getRequest: No request of type %s found." % requestType) return S_OK() # Select a request if requestType not in self.lastRequest: self.lastRequest[requestType] = ("", 0) lastRequest, lastRequestIndex = self.lastRequest[requestType] res = self.__selectRequestCursor(candidateRequests, lastRequest, lastRequestIndex) if not res["OK"]: self.getIdLock.release() errStr = "getRequest: Failed to get request cursor." self.log.error(errStr, res["Message"]) return S_ERROR(errStr) selectedRequestName, selectedRequestIndex = res["Value"] # Obtain the string for the selected request res = self.__getRequestString(selectedRequestName) if not res["OK"]: self.getIdLock.release() errStr = "getRequest: Failed to get request string for %s." % selectedRequestName self.log.error(errStr, res["Message"]) return S_ERROR(errStr) selectedRequestString = res["Value"] # Set the request status to assigned res = self.setRequestStatus(selectedRequestName, "Assigned") if not res["OK"]: self.getIdLock.release() errStr = "getRequest: Failed to set %s status to 'Assigned'." % selectedRequestName self.log.error(errStr, res["Message"]) return S_ERROR(errStr) # Update the request cursor and return the selected request self.lastRequest[requestType] = (selectedRequestName, selectedRequestIndex) self.getIdLock.release() self.log.info("getRequest: Successfully obtained %s request." % selectedRequestName) oRequest = RequestContainer(request=selectedRequestString) jobID = oRequest.getJobID() jobID = jobID["Value"] if jobID["OK"] and jobID["Value"] else 0 try: jobID = int(jobID) except (TypeError, ValueError), error: self.log.error("getRequest: could not get JobID from Request, setting it to 0: %s" % str(error)) jobID = 0 return S_OK({"RequestString": selectedRequestString, "RequestName": selectedRequestName, "JobID": jobID})
def setRequest( self, requestName, requestString ): """ insert request :requestName: to teh db :param str requestName: Requests.RequestName :param str requestString: xml-serialised request """ request = RequestContainer( init = True, request = requestString ) requestTypes = request.getSubRequestTypes()['Value'] failed = False res = self._getRequestID( requestName ) if not res['OK']: # we have a special case here: if request already exists, we override it if it # comes from a DIRAC job. This is identified by having a meaningful JobID in # the request if res['Message'].find( 'Duplicate' ) != -1: # Duplicate request jobID = request.getJobID()['Value'] if jobID == "Unknown": return res try: jobID = int( jobID ) except: return res if jobID > 0: # Remove the existing request result = self._deleteRequest( requestName ) if not result['OK']: message = res['Message'] return S_ERROR( 'Failed to set request: ' + message + ' can not override' ) res = self._getRequestID( requestName ) if not res['OK']: return res else: return res requestID = res['Value'] subRequestIDs = {} res = self.__setRequestAttributes( requestID, request ) if res['OK']: for requestType in requestTypes: res = request.getNumSubRequests( requestType ) numRequests = res['Value'] for ind in range( numRequests ): res = self._getSubRequestID( requestID, requestType ) if res['OK']: subRequestID = res['Value'] res = self.__setSubRequestAttributes( requestID, ind, requestType, subRequestID, request ) if res['OK']: subRequestIDs[subRequestID] = res['Value'] res = self.__setSubRequestFiles( ind, requestType, subRequestID, request ) if res['OK']: res = self.__setSubRequestDatasets( ind, requestType, subRequestID, request ) if not res['OK']: failed = True message = res['Message'] else: failed = True message = res['Message'] else: failed = True message = res['Message'] else: failed = True message = res['Message'] else: failed = True message = res['Message'] for subRequestID, status in subRequestIDs.items(): if not status: status = "Waiting" res = self._setSubRequestAttribute( requestID, subRequestID, 'Status', status ) if not res['OK']: failed = True message = res['Message'] res = self._setRequestAttribute( requestID, 'Status', 'Waiting' ) if not res['OK']: failed = True message = res['Message'] if failed: res = self._deleteRequest( requestName ) return S_ERROR( 'Failed to set request: ' + message ) else: return S_OK( requestID )
def setRequest(self, requestName, requestString): request = RequestContainer(init=True, request=requestString) requestTypes = request.getSubRequestTypes()['Value'] failed = False res = self._getRequestID(requestName) if not res['OK']: # we have a special case here: if request already exists, we override it if it # comes from a DIRAC job. This is identified by having a meaningful JobID in # the request if res['Message'].find('Duplicate') != -1: # Duplicate request jobID = request.getJobID()['Value'] if jobID == "Unknown": return res try: jobID = int(jobID) except: return res if jobID > 0: # Remove the existing request result = self._deleteRequest(requestName) if not result['OK']: message = res['Message'] return S_ERROR('Failed to set request: ' + message + ' can not override') res = self._getRequestID(requestName) if not res['OK']: return res else: return res requestID = res['Value'] subRequestIDs = {} res = self.__setRequestAttributes(requestID, request) if res['OK']: for requestType in requestTypes: res = request.getNumSubRequests(requestType) numRequests = res['Value'] for ind in range(numRequests): res = self._getSubRequestID(requestID, requestType) if res['OK']: subRequestID = res['Value'] res = self.__setSubRequestAttributes( requestID, ind, requestType, subRequestID, request) if res['OK']: subRequestIDs[subRequestID] = res['Value'] res = self.__setSubRequestFiles( ind, requestType, subRequestID, request) if res['OK']: res = self.__setSubRequestDatasets( ind, requestType, subRequestID, request) if not res['OK']: failed = True message = res['Message'] else: failed = True message = res['Message'] else: failed = True message = res['Message'] else: failed = True message = res['Message'] else: failed = True message = res['Message'] for subRequestID, status in subRequestIDs.items(): if not status: status = "Waiting" res = self._setSubRequestAttribute(requestID, subRequestID, 'Status', status) if not res['OK']: failed = True message = res['Message'] res = self._setRequestAttribute(requestID, 'Status', 'Waiting') if not res['OK']: failed = True message = res['Message'] if failed: res = self._deleteRequest(requestName) return S_ERROR('Failed to set request: ' + message) else: return S_OK(requestID)
def getRequest(self, requestType): """ Obtain a request from the database of a certain type """ gLogger.info( "RequestDBFile._getRequest: Attempting to get %s type request." % requestType) try: # Determine the request name to be obtained candidateRequests = [] reqDir = os.path.join(self.root, requestType, "ToDo") self.getIdLock.acquire() if os.path.exists(reqDir): candidateRequests = [ os.path.basename(requestFile) for requestFile in sorted(filter(os.path.isfile, [ os.path.join(reqDir, requestName) for requestName in os.listdir(reqDir) ]), key=os.path.getctime) ] if not len(candidateRequests) > 0: self.getIdLock.release() gLogger.info( "RequestDBFile._getRequest: No request of type %s found." % requestType) return S_OK() # Select a request if not self.lastRequest.has_key(requestType): self.lastRequest[requestType] = ('', 0) lastRequest, lastRequestIndex = self.lastRequest[requestType] res = self.__selectRequestCursor(candidateRequests, lastRequest, lastRequestIndex) if not res['OK']: self.getIdLock.release() errStr = "RequestDBFile._getRequest: Failed to get request cursor." gLogger.error(errStr, res['Message']) return S_ERROR(errStr) selectedRequestName, selectedRequestIndex = res['Value'] # Obtain the string for the selected request res = self.__getRequestString(selectedRequestName) if not res['OK']: self.getIdLock.release() errStr = "RequestDBFile._getRequest: Failed to get request string for %s." % selectedRequestName gLogger.error(errStr, res['Message']) return S_ERROR(errStr) selectedRequestString = res['Value'] # Set the request status to assigned res = self.setRequestStatus(selectedRequestName, 'Assigned') if not res['OK']: self.getIdLock.release() errStr = "RequestDBFile._getRequest: Failed to set %s status to 'Assigned'." % selectedRequestName gLogger.error(errStr, res['Message']) return S_ERROR(errStr) # Update the request cursor and return the selected request self.lastRequest[requestType] = (selectedRequestName, selectedRequestIndex) self.getIdLock.release() gLogger.info( "RequestDBFile._getRequest: Successfully obtained %s request." % selectedRequestName) jobID = 'Unknown' try: oRequest = RequestContainer(request=selectedRequestString) jobID = oRequest.getJobID()['Value'] except: gLogger.exception('Could not get JobID from Request') resDict = { 'RequestString': selectedRequestString, 'RequestName': selectedRequestName, 'JobID': jobID } return S_OK(resDict) except Exception, x: errStr = "RequestDBFile._getRequest: Exception while getting request." gLogger.exception(errStr, requestType, lException=x) return S_ERROR(errStr)
def getRequest(self, requestType): """ Obtain a request from the database of a :requestType: type :param self: self reference :param str requestType: request type """ self.log.info("getRequest: Attempting to get %s type request." % requestType) try: # Determine the request name to be obtained candidateRequests = [] reqDir = os.path.join(self.root, requestType, "Waiting") self.getIdLock.acquire() if os.path.exists(reqDir): candidateRequests = [ os.path.basename(requestFile) for requestFile in sorted(filter(os.path.isfile, [ os.path.join(reqDir, requestName) for requestName in os.listdir(reqDir) ]), key=os.path.getctime) ] if not len(candidateRequests) > 0: self.getIdLock.release() self.log.info("getRequest: No request of type %s found." % requestType) return S_OK() # Select a request if requestType not in self.lastRequest: self.lastRequest[requestType] = ('', 0) lastRequest, lastRequestIndex = self.lastRequest[requestType] res = self.__selectRequestCursor(candidateRequests, lastRequest, lastRequestIndex) if not res['OK']: self.getIdLock.release() errStr = "getRequest: Failed to get request cursor." self.log.error(errStr, res['Message']) return S_ERROR(errStr) selectedRequestName, selectedRequestIndex = res['Value'] # Obtain the string for the selected request res = self.__getRequestString(selectedRequestName) if not res['OK']: self.getIdLock.release() errStr = "getRequest: Failed to get request string for %s." % selectedRequestName self.log.error(errStr, res['Message']) return S_ERROR(errStr) selectedRequestString = res['Value'] # Set the request status to assigned res = self.setRequestStatus(selectedRequestName, 'Assigned') if not res['OK']: self.getIdLock.release() errStr = "getRequest: Failed to set %s status to 'Assigned'." % selectedRequestName self.log.error(errStr, res['Message']) return S_ERROR(errStr) # Update the request cursor and return the selected request self.lastRequest[requestType] = (selectedRequestName, selectedRequestIndex) self.getIdLock.release() self.log.info("getRequest: Successfully obtained %s request." % selectedRequestName) oRequest = RequestContainer(request=selectedRequestString) jobID = oRequest.getJobID() jobID = jobID["Value"] if jobID["OK"] and jobID["Value"] else 0 try: jobID = int(jobID) except (TypeError, ValueError), error: self.log.exception('Could not get JobID from Request: %s' % str(error)) resDict = { 'RequestString': selectedRequestString, 'RequestName': selectedRequestName, 'JobID': jobID } return S_OK(resDict)