def remove(self, userService: UserService) -> UserService: """ Removes a uService element """ with transaction.atomic(): userService = UserService.objects.select_for_update().get( id=userService.id) logger.debug('Removing userService %a', userService) if userService.isUsable() is False and State.isRemovable( userService.state) is False: raise OperationException( _('Can\'t remove a non active element')) userService.setState(State.REMOVING) logger.debug("***** The state now is %s *****", State.toString(userService.state)) userService.setInUse( False ) # For accounting, ensure that it is not in use right now userService.save() userServiceInstance = userService.getInstance() state = userServiceInstance.destroy() # Data will be serialized on makeUnique process UserServiceOpChecker.makeUnique(userService, userServiceInstance, state) return userService
def checkAndUpdateState(userService: UserService, userServiceInstance: UserDeployment, state: str): """ Checks the value returned from invocation to publish or checkPublishingState, updating the servicePoolPub database object Return True if it has to continue checking, False if finished """ try: # Fills up basic data userService.unique_id = userServiceInstance.getUniqueId( ) # Updates uniqueId userService.friendly_name = userServiceInstance.getName( ) # And name, both methods can modify serviceInstance, so we save it later userService.save(update_fields=['unique_id', 'friendly_name']) updater = { State.PREPARING: UpdateFromPreparing, State.REMOVING: UpdateFromRemoving, State.CANCELING: UpdateFromCanceling }.get(userService.state, UpdateFromOther) logger.debug('Updating %s from %s with updater %s and state %s', userService.friendly_name, State.toString(userService.state), updater, state) updater(userService, userServiceInstance).run(state) except Exception as e: logger.exception('Checking service state') log.doLog(userService, log.ERROR, 'Exception: {}'.format(e), log.INTERNAL) userService.setState(State.ERROR) userService.save(update_fields=['data'])
def notifyReadyFromOsManager(self, userService: UserService, data: typing.Any) -> None: try: userServiceInstance = userService.getInstance() logger.debug('Notifying user service ready state') state = userServiceInstance.notifyReadyFromOsManager(data) logger.debug('State: %s', state) if state == State.FINISHED: userService.updateData(userServiceInstance) logger.debug('Service is now ready') elif userService.state in (State.USABLE, State.PREPARING): # We don't want to get active deleting or deleted machines... userService.setState(State.PREPARING) UserServiceOpChecker.makeUnique(userService, userServiceInstance, state) userService.save(update_fields=['os_state']) except Exception as e: logger.exception('Unhandled exception on notyfyReady: %s', e) userService.setState(State.ERROR) return
def moveToLevel(self, cache: UserService, cacheLevel: int) -> None: """ Moves a cache element from one level to another @return: cache element """ cache.refresh_from_db() logger.debug('Moving cache %s to level %s', cache, cacheLevel) cacheInstance = cache.getInstance() state = cacheInstance.moveToCache(cacheLevel) cache.cache_level = cacheLevel cache.save(update_fields=['cache_level']) logger.debug('Service State: %a %s %s', State.toString(state), State.toString(cache.state), State.toString(cache.os_state)) if State.isRuning(state) and cache.isUsable(): cache.setState(State.PREPARING) # Data will be serialized on makeUnique process UserServiceOpChecker.makeUnique(cache, cacheInstance, state)