def checkPassive(): reload(config) #so we can throttle the times based on our needs. pool = getPool() if pool == None: return resourceList = pool.resourceList #its not a list but a dict, oops passiveCnt = 0 errorCnt = 0 for resource in resourceList.keys(): if resourceList[resource].useFlag == USED and resourceList[ resource].resourceUseType == PASSIVE: passiveCnt += 1 resourceLog.logDebug('Trying... %s: %s' % (resourceList[resource].resourceName, resourceList[resource].useTime)) resourceLog.logDebug('Passive Timeout set to: %s' % config.passiveWarn) if getAge(resourceList[resource].useTime, MIN) >= config.passiveWarn: errorCnt += 1 resourceLog.logCrit("%s has been checked out a long time" % (resourceList[resource].resourceName)) resourceLog.logInfo( "%s Errors found out of %s checked out passive resources" % (errorCnt, passiveCnt)) return
def getResource(self, buildTag, buildId, resourceType, extraText): #We don't need to init in this function, do it in the constructor. #change all the locks to use the class lock #this locking may not work, we might have to use the pyro built in locking mechanism, but i dont' trust it, we'll see #Remove This, fix the queue crap if MODULETEST: print "I'm waiting for lock for init for %s"%(resourceType) self.lock.acquire() if MODULETEST: print "I have lock for init for %s"%(resourceType) try: #if any of this stuff dies we must release the lock resourceQueue.addEntry(buildTag, buildId, resourceType) finally: if MODULETEST: print "Init Complete, releasing lock for %s"%(resourceType) self.lock.release() #This part stays more or less the same, what we are going to remove is the loading/unloading of various data structures #loop until something returns while(1): if MODULETEST: print "Attemping to check out %s, waiting for lock..."%(resourceType) self.lock.acquire() #arg is 1 for blocking, 0 for nonblocking #lock exceptions will propagate to the calling module, other exceptions will require cleanup #so we must release the lock try: if MODULETEST: print "Lock obtained proceeding to search for %s"%(resourceType) if resourceQueue.checkEntry(buildId, resourceType): #Is it our turn to go? resource = self.activePool.tagResource(resourceType, buildTag, buildId, extraText) if resource == "error": #print "Error: requested resource %s doesn't exist"%(resourceType) resourceQueue.removeEntry(buildId, resourceType) resourceLog.logErr('Error Checking out: %s'%resourceType) self.lock.release() return resource #This will return the string "error" if we decide to look for it in the calling script elif resource: #Is the resource Available? resourceQueue.removeEntry(buildId, resourceType) if MODULETEST: print "I have %s, leaving..."%(resourceType) resourceLog.logInfo('Checking out: %s'%resource) self.lock.release() return resource else: #No Resource if MODULETEST: print "Resource %s not available, going to sleep..."%(resourceType) self.lock.release() time.sleep(random.randint(1,10)) #once the paperwork is done, sleep for a random number of seconds else: #Not our turn if MODULETEST: print "Wasn't our turn in the Queue, releasing lock for %s"%(resourceType) self.lock.release() time.sleep(random.randint(1,10)) except: self.lock.release() traceback.print_exc() raise ResourceError, "Fatal Resource Manager Error"
def releaseResource(self, resourceName): if MODULETEST: print "awaiting lock to release %s"%(resourceName) self.lock.acquire() try: self.activePool.returnResource(resourceName) resourceLog.logInfo('Checking in: %s'%resourceName) if MODULETEST: print "resource %s returning, releasing lock"%(resourceName) finally: self.lock.release() return
def __init__(self): Pyro.core.ObjBase.__init__(self) resourceLog.logInfo('Initializing resource manager....') self.lock = threading.Lock() #one lock, one love #We load the resource pool into memory using pickle, as before self.activePool = ResourcePool() self.activePool.initResourcePool() resourceQueue.initQueue() #setup the datastructures for use priorityList.priorityListInit() self.controlThrd = threading.Thread(target=resourceControl.controller) self.controlThrd.start() resourceLog.logInfo('Init Complete!')
def controller(): while 1: reload( config ) #we'll reload each iteration so we can change things when we feel like without restarting the server time.sleep(config.watchInterval) cmd = checkMsg() if cmd == QUIT: return else: resourceLog.logInfo("Executing task list") exec_tasklist() resourceLog.logInfo("Task list complete!")
def manualCheckIn(): while (1): os.system('clear') print "Return a Resource" resource = raw_input("Resource to return:") print "Returning %s..." % (resource) resourceManager.manualCheckIn(resource) resourceLog.logInfo('Manual Checkin: %s' % resource) another = raw_input("Would you like another? (y/N)") if another not in ( 'y', 'Y'): #if they don't type a yes we bail back to the main menu os.system('clear') return
def manualCheckOut(): while (1): os.system('clear') print "This function isn't intended to be use for long as ultimately" print "all build scripts should incorporate resource checkouts into their scripts" searchMode = None while (searchMode not in ('n', 'N', 't', 'T')): searchMode = raw_input("Node Lookup Method([N]ame/[T]ype):") if searchMode not in ('n', 'N', 't', 'T'): print "Invalid Selection, please enter N or T" if searchMode in ('N', 'n'): searchMode = "name" else: searchMode = "type" requestedResource = raw_input( "Please Enter the Resource You would like to check out:") notes = raw_input("Notes associated with this checkout:") resource = resourceManager.manualCheckOut(requestedResource, notes, searchMode) if (resource == None): print "Unable to check out: %s" % (requestedResource) else: if resource == 'error': print "Error: There is a problem with this resource, please see the system administrator" else: print "\n\nGranted: %s\n\n" % (resource) print "Please remember to check this resource back in when you are done with it!!!!!!!" resourceLog.logInfo('Manual Checkout: %s' % resource) another = raw_input("Would you like another? (y/N)") if another not in ( 'y', 'Y'): #if they don't type a yes we bail back to the main menu os.system('clear') return