Commit 148a0972 by Szeberényi Imre

chache log & vnc log fix

parent 215075e2
...@@ -53,13 +53,20 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' ...@@ -53,13 +53,20 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
########## CACHE CONFIGURATION ########## CACHE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
CACHES = { CACHES = {
'default': { 'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache', 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
'LOCATION': '127.0.0.1:11211', 'LOCATION': '127.0.0.1:11211',
} }
} }
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
#CACHES = {
# 'default': {
# 'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
# 'LOCATION': '127.0.0.1:11211',
# }
#}
########## END CACHE CONFIGURATION ########## END CACHE CONFIGURATION
......
...@@ -82,10 +82,9 @@ def activitycontextimpl(act, on_abort=None, on_commit=None): ...@@ -82,10 +82,9 @@ def activitycontextimpl(act, on_abort=None, on_commit=None):
e=str(e.__class__.__name__), e=str(e.__class__.__name__),
error=get_error_msg(e)) error=get_error_msg(e))
raise raise
except: except BaseException as e:
logger.exception("Failed activity %s" % str(act)) logger.exception("Failed activity %s" % str(act))
handler = None handler = None if on_abort is None else lambda a: on_abort(a, e)
# handler = None if on_abort is None else lambda a: on_abort(a, e)
act.finish(succeeded=False, result=result, event_handler=handler) act.finish(succeeded=False, result=result, event_handler=handler)
raise raise
else: else:
...@@ -274,16 +273,18 @@ def compute_cached(method, instance, memcached_seconds, ...@@ -274,16 +273,18 @@ def compute_cached(method, instance, memcached_seconds,
result = method(instance, *args, **kwargs) result = method(instance, *args, **kwargs)
# save to memcache # save to memcache
cache.set(key, result, memcached_seconds) cache.set(key, result, memcached_seconds)
elapsed = time() - start now = time()
elapsed = now - start
cache.set("%s.cached" % key, 2, max(memcached_seconds * 0.5, cache.set("%s.cached" % key, 2, max(memcached_seconds * 0.5,
memcached_seconds * 0.75 - elapsed)) memcached_seconds - elapsed))
logger.debug('Value of <%s>.%s(%s)=<%s> saved to cache (%s elapsed).', setattr(instance, key, {'time': start, 'value': result})
str(instance), method.__name__, str(args), logger.debug('Value of %s<%s>.%s(%s)=<%s> saved to cache (%s elapsed, key: %s).',
str(result), elapsed) str(method.__module__), str(instance), method.__name__, str(args),
str(result), elapsed, key)
return result return result
def method_cache(memcached_seconds=60, instance_seconds=5): # noqa def method_cache(memcached_seconds=60, instance_seconds=30): # noqa
"""Cache return value of decorated method to memcached and memory. """Cache return value of decorated method to memcached and memory.
:param memcached_seconds: Invalidate memcached results after this time. :param memcached_seconds: Invalidate memcached results after this time.
...@@ -307,12 +308,10 @@ def method_cache(memcached_seconds=60, instance_seconds=5): # noqa ...@@ -307,12 +308,10 @@ def method_cache(memcached_seconds=60, instance_seconds=5): # noqa
method_name = method.__name__ method_name = method.__name__
def get_key(instance, *args, **kwargs): def get_key(instance, *args, **kwargs):
print(str(method.__module__) + method_name + concat_str = str(method.__module__) + '<' + str(instance) + '>' + '.' + method_name + str(args) + str(kwargs)
str(instance.id) + key = sha224(concat_str.encode('utf-8')).hexdigest()
str(args) + logger.debug("concat_str: %s key: %s", concat_str, key)
str(kwargs)) return key
concat_str = str(method.__module__) + method_name + str(instance.id) + str(args) + str(kwargs)
return sha224(concat_str.encode('utf-8')).hexdigest()
def x(instance, *args, **kwargs): def x(instance, *args, **kwargs):
invalidate = kwargs.pop('invalidate_cache', False) invalidate = kwargs.pop('invalidate_cache', False)
...@@ -328,26 +327,28 @@ def method_cache(memcached_seconds=60, instance_seconds=5): # noqa ...@@ -328,26 +327,28 @@ def method_cache(memcached_seconds=60, instance_seconds=5): # noqa
if vals['time'] + instance_seconds > now: if vals['time'] + instance_seconds > now:
# has valid on class cache, return that # has valid on class cache, return that
result = vals['value'] result = vals['value']
setattr(instance, key, {'time': now, 'value': result}) # setattr(instance, key, {'time': now, 'value': result})
if result is None: if result is None:
result = cache.get(key) result = cache.get(key)
if invalidate or (result is None): if invalidate or (result is None):
logger.debug("all caches failed, compute now") logger.debug("all caches failed, compute now (key,inv,res,sec: %s,%s,%s,%s)" % (key, invalidate, result, memcached_seconds))
result = compute_cached(method, instance, memcached_seconds, result = compute_cached(method, instance, memcached_seconds,
key, time(), *args, **kwargs) key, time(), *args, **kwargs)
setattr(instance, key, {'time': now, 'value': result}) # setattr(instance, key, {'time': now, 'value': result})
logger.debug("new key,result: %s, %s", key, result)
elif not cache.get("%s.cached" % key): elif not cache.get("%s.cached" % key):
logger.debug("caches expiring, compute async") logger.debug("%s.caches expiring, compute async (%s sec)", key, memcached_seconds)
cache.set("%s.cached" % key, 1, memcached_seconds * 0.5) cache.set("%s.cached" % key, 1, memcached_seconds * 0.5)
logger.debug("%s.caches set %s", key, cache.get("%s.cached" % key))
try: try:
compute_cached.apply_async( compute_cached.apply_async(
queue='localhost.man', kwargs=kwargs, args=[ queue='localhost.man', kwargs=kwargs, args=[
method_name, (instance.__class__, instance.id), method_name, (instance.__class__, instance.id),
memcached_seconds, key, time()] + list(args)) memcached_seconds, key, time()] + list(args))
except: except:
logger.exception("Couldnt compute async %s", method_name) logger.exception("Couldnt compute async %s key %s", method_name, key)
return result return result
......
...@@ -104,6 +104,7 @@ class VmDetailVncTokenView(CheckedDetailView): ...@@ -104,6 +104,7 @@ class VmDetailVncTokenView(CheckedDetailView):
host = str(self.object.node.host.ipv4) host = str(self.object.node.host.ipv4)
value = signing.dumps({'host': host, 'port': port}, value = signing.dumps({'host': host, 'port': port},
key=getenv("PROXY_SECRET", 'asdasd')), key=getenv("PROXY_SECRET", 'asdasd')),
logger.debug('vnc host: %s vnc port: %s value: %s', host, port, value)
return HttpResponse('vnc/?d=%s' % value) return HttpResponse('vnc/?d=%s' % value)
else: else:
raise Http404() raise Http404()
......
...@@ -259,7 +259,7 @@ class Node(OperatedMixin, TimeStampedModel): ...@@ -259,7 +259,7 @@ class Node(OperatedMixin, TimeStampedModel):
""" """
return self.ram_size * self.overcommit return self.ram_size * self.overcommit
@method_cache(30) @method_cache(300)
def get_remote_queue_name(self, queue_id, priority=None): def get_remote_queue_name(self, queue_id, priority=None):
"""Returns the name of the remote celery queue for this node. """Returns the name of the remote celery queue for this node.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment