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'
########## CACHE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
'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
......
......@@ -82,10 +82,9 @@ def activitycontextimpl(act, on_abort=None, on_commit=None):
e=str(e.__class__.__name__),
error=get_error_msg(e))
raise
except:
except BaseException as e:
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)
raise
else:
......@@ -274,16 +273,18 @@ def compute_cached(method, instance, memcached_seconds,
result = method(instance, *args, **kwargs)
# save to memcache
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,
memcached_seconds * 0.75 - elapsed))
logger.debug('Value of <%s>.%s(%s)=<%s> saved to cache (%s elapsed).',
str(instance), method.__name__, str(args),
str(result), elapsed)
memcached_seconds - elapsed))
setattr(instance, key, {'time': start, 'value': result})
logger.debug('Value of %s<%s>.%s(%s)=<%s> saved to cache (%s elapsed, key: %s).',
str(method.__module__), str(instance), method.__name__, str(args),
str(result), elapsed, key)
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.
:param memcached_seconds: Invalidate memcached results after this time.
......@@ -307,12 +308,10 @@ def method_cache(memcached_seconds=60, instance_seconds=5): # noqa
method_name = method.__name__
def get_key(instance, *args, **kwargs):
print(str(method.__module__) + method_name +
str(instance.id) +
str(args) +
str(kwargs))
concat_str = str(method.__module__) + method_name + str(instance.id) + str(args) + str(kwargs)
return sha224(concat_str.encode('utf-8')).hexdigest()
concat_str = str(method.__module__) + '<' + str(instance) + '>' + '.' + method_name + str(args) + str(kwargs)
key = sha224(concat_str.encode('utf-8')).hexdigest()
logger.debug("concat_str: %s key: %s", concat_str, key)
return key
def x(instance, *args, **kwargs):
invalidate = kwargs.pop('invalidate_cache', False)
......@@ -328,26 +327,28 @@ def method_cache(memcached_seconds=60, instance_seconds=5): # noqa
if vals['time'] + instance_seconds > now:
# has valid on class cache, return that
result = vals['value']
setattr(instance, key, {'time': now, 'value': result})
# setattr(instance, key, {'time': now, 'value': result})
if result is None:
result = cache.get(key)
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,
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):
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)
logger.debug("%s.caches set %s", key, cache.get("%s.cached" % key))
try:
compute_cached.apply_async(
queue='localhost.man', kwargs=kwargs, args=[
method_name, (instance.__class__, instance.id),
memcached_seconds, key, time()] + list(args))
except:
logger.exception("Couldnt compute async %s", method_name)
logger.exception("Couldnt compute async %s key %s", method_name, key)
return result
......
......@@ -104,6 +104,7 @@ class VmDetailVncTokenView(CheckedDetailView):
host = str(self.object.node.host.ipv4)
value = signing.dumps({'host': host, 'port': port},
key=getenv("PROXY_SECRET", 'asdasd')),
logger.debug('vnc host: %s vnc port: %s value: %s', host, port, value)
return HttpResponse('vnc/?d=%s' % value)
else:
raise Http404()
......
......@@ -259,7 +259,7 @@ class Node(OperatedMixin, TimeStampedModel):
"""
return self.ram_size * self.overcommit
@method_cache(30)
@method_cache(300)
def get_remote_queue_name(self, queue_id, priority=None):
"""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