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 removeOrCancel(self, userService: UserService): if userService.isUsable() or State.isRemovable(userService.state): return self.remove(userService) if userService.isPreparing(): return self.cancel(userService) raise OperationException(_('Can\'t remove nor cancel {} cause its state don\'t allow it').format(userService.name))
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)