forked from shaba/openuds
* added storeValue & recoverValue to a deployed service
* Fixed log manager so repeated logs do not get logged by default (avoid duplicate log entries) * Extracted "notifyRestraing" to a method, so it's easier to find/modify it :-)
This commit is contained in:
parent
38ced4fea9
commit
7a9b4ee454
@ -77,7 +77,7 @@ class LogManager(object):
|
||||
LogManager._manager = LogManager()
|
||||
return LogManager._manager
|
||||
|
||||
def __log(self, owner_type, owner_id, level, message, source):
|
||||
def __log(self, owner_type, owner_id, level, message, source, avoidDuplicates):
|
||||
'''
|
||||
Logs a message associated to owner
|
||||
'''
|
||||
@ -87,7 +87,17 @@ class LogManager(object):
|
||||
qs = Log.objects.filter(owner_id = owner_id, owner_type = owner_type)
|
||||
# First, ensure we do not have more than requested logs, and we can put one more log item
|
||||
if qs.count() >= GlobalConfig.MAX_LOGS_PER_ELEMENT.getInt():
|
||||
for i in qs.order_by('-created',)[GlobalConfig.MAX_LOGS_PER_ELEMENT.getInt()-1:]: i.delete()
|
||||
for i in qs.order_by('-created',)[GlobalConfig.MAX_LOGS_PER_ELEMENT.getInt()-1:]:
|
||||
i.delete()
|
||||
|
||||
if avoidDuplicates is True:
|
||||
try:
|
||||
lg = Log.objects.filter(owner_id = owner_id, owner_type = owner_type, level = level, source = source).order_by('-created', '-id')[0]
|
||||
if lg.message == message:
|
||||
# Do not log again, already logged
|
||||
return
|
||||
except: # Do not exists log
|
||||
pass
|
||||
|
||||
# now, we add new log
|
||||
Log.objects.create(owner_type = owner_type, owner_id = owner_id, created = getSqlDatetime(), source = source, level = level, data = message)
|
||||
@ -112,7 +122,7 @@ class LogManager(object):
|
||||
|
||||
|
||||
|
||||
def doLog(self, wichObject, level, message, source):
|
||||
def doLog(self, wichObject, level, message, source, avoidDuplicates = True):
|
||||
'''
|
||||
Do the logging for the requested object.
|
||||
|
||||
@ -123,7 +133,7 @@ class LogManager(object):
|
||||
|
||||
owner_type = transDict.get(type(wichObject), None)
|
||||
if owner_type is not None:
|
||||
self.__log(owner_type, wichObject.id, level, message, source)
|
||||
self.__log(owner_type, wichObject.id, level, message, source, avoidDuplicates)
|
||||
else:
|
||||
logger.debug('Requested doLog for a type of object not covered: {0}'.format(wichObject))
|
||||
|
||||
|
@ -70,9 +70,9 @@ def useLog(type_, serviceUniqueId, serviceIp, username):
|
||||
useLogger.info('|'.join([type_, serviceUniqueId, serviceIp, username]))
|
||||
|
||||
|
||||
def doLog(wichObject, level, message, source = UNKNOWN):
|
||||
def doLog(wichObject, level, message, source = UNKNOWN, avoidDuplicates = True):
|
||||
from uds.core.managers import logManager
|
||||
logManager().doLog(wichObject, level, message, source)
|
||||
logManager().doLog(wichObject, level, message, source, avoidDuplicates)
|
||||
|
||||
|
||||
def getLogs(wichObject, limit = None):
|
||||
|
@ -64,6 +64,12 @@ class ServiceCacheUpdater(Job):
|
||||
def calcProportion(max, actual):
|
||||
return actual * 10000 / max
|
||||
|
||||
@staticmethod
|
||||
def __notifyRestrain(deployedService):
|
||||
log.doLog(deployedService, log.WARN, 'Deployed service is restrained due to errors', log.INTERNAL)
|
||||
logger.info('Deployed service {0} is restrained, will check this later'.format(deployedService.name))
|
||||
|
||||
|
||||
def bestDeployedServiceNeedingCacheUpdate(self):
|
||||
# State filter for cached and inAssigned objects
|
||||
# First we get all deployed services that could need cache generation
|
||||
@ -88,8 +94,7 @@ class ServiceCacheUpdater(Job):
|
||||
continue
|
||||
|
||||
if ds.isRestrained():
|
||||
log.doLog(ds, log.WARN, 'Deployed service is restrained due to errors', log.INTERNAL)
|
||||
logger.info('Deployed service {0} is restrained, will check this later'.format(ds.name))
|
||||
ServiceCacheUpdater.__notifyRestrain(ds)
|
||||
continue
|
||||
|
||||
# Get data related to actual state of cache
|
||||
|
@ -935,6 +935,27 @@ class DeployedService(models.Model):
|
||||
|
||||
return False
|
||||
|
||||
def storeValue(self, name, value):
|
||||
'''
|
||||
Stores a value inside custom storage
|
||||
|
||||
Args:
|
||||
name: Name of the value to store
|
||||
value: Value of the value to store
|
||||
'''
|
||||
self.getEnvironment().storage().put(name, value)
|
||||
|
||||
def recoverValue(self, name):
|
||||
'''
|
||||
Recovers a value from custom storage
|
||||
|
||||
Args:
|
||||
name: Name of values to recover
|
||||
|
||||
Returns:
|
||||
Stored value, None if no value was stored
|
||||
'''
|
||||
return self.getEnvironment().storage().get(name)
|
||||
|
||||
def setState(self, state, save = True):
|
||||
'''
|
||||
|
Loading…
Reference in New Issue
Block a user