예제 #1
0
    def setUp(self):
        super().setUp()

        # installs new, empty cache instance for each test
        install_schema_fetcher_cache()
        for cache in caches.all():
            cache.clear()
예제 #2
0
    def _close_django_caches(self, shutdown=False):
        if self.use_django and django_caches:
            if shutdown:
                self.logger.info('Closing all Django caches')

            for cache in django_caches.all():
                cache.close(for_shutdown=shutdown)
예제 #3
0
    def readiness(self, request):
        # Connect to each database and do a generic standard SQL query
        # that doesn't write any data and doesn't depend on any tables
        # being present.
        try:
            requests.get(settings.HSD_URI, timeout=5)
        except Exception as e:
            logger.exception(e)
            return HttpResponseServerError("db: cannot connect to hsd.")

        # Call get_stats() to connect to each memcached instance and get it's stats.
        # This can effectively check if each is online.
        try:
            from django.core.cache import caches
            from django.core.cache.backends.memcached import BaseMemcachedCache
            for cache in caches.all():
                if isinstance(cache, BaseMemcachedCache):
                    stats = cache._cache.get_stats()
                    if len(stats) != len(cache._servers):
                        return HttpResponseServerError("cache: cannot connect to cache.")
        except Exception as e:
            logger.exception(e)
            return HttpResponseServerError("cache: cannot connect to cache.")

        return HttpResponse("OK")
예제 #4
0
    def handle(self, *args, **options):
        self.stdout.write("\nClear caches:\n")
        for cache in caches.all():
            self.stdout.write("\tClear '%s'\n" % cache.__class__.__name__)
            cache.clear()

        self.stdout.write("\ndone.\n")
예제 #5
0
    def readyz(self, request):
        """Returns that the databases and caches are ready"""
        # Connect to each database and do a generic standard SQL query that doesn't
        # write any data and doesn't depend on any tables being present.
        try:
            from django.db import connections
            for name in connections:
                cursor = connections[name].cursor()
                cursor.execute("SELECT 1;")
                row = cursor.fetchone()
                if row is None:
                    return HttpResponseServerError("db: invalid response")
        except Exception as e:
            LOGGER.exception(e)
            return HttpResponseServerError("db: cannot connect to database.")

        # Call get_stats() to connect to each memcached instance and get it's stats.
        # This can effectively check if each is online.
        try:
            from django.core.cache import caches
            from django.core.cache.backends.memcached import BaseMemcachedCache
            for cache in caches.all():
                if isinstance(cache, BaseMemcachedCache):
                    stats = cache._cache.get_stats()
                    if len(stats) != len(cache._servers):
                        return HttpResponseServerError(
                            "cache: cannot connect to cache.")
        except Exception as e:
            LOGGER.exception(e)
            return HttpResponseServerError("cache: cannot connect to cache.")

        return HttpResponse("OK")
예제 #6
0
    def readiness(self, request):
        """
        Metódo para realizar teste de conexão com o banco de dados
        """
        try:
            for name in connections:
                cursor = connections[name].cursor()
                cursor.execute("SELECT 1;")
                row = cursor.fetchone()
                if row is None:
                    return HttpResponseServerError(
                        "Banco de dados: erro de banco de dados.")
        except (Exception) as erro_banco:
            LOGGER.exception(erro_banco)
            return HttpResponseServerError(
                "Banco de dados: não foi possível conectar.")

        # Chama o metódo get_stats() para conectar em todas as instâncias cache do Django
        # e verifica o status.
        # Se todas as instâncias estiverem OK, o Django está em pleno funcionamento
        try:
            for cache in caches.all():
                if isinstance(cache, BaseMemcachedCache):
                    stats = cache._cache.get_stats()
                    if len(stats) != len(cache._servers):
                        return HttpResponseServerError("Cache: erro de cache.")
        except (Exception) as erro_cache:
            LOGGER.exception(erro_cache)
            return HttpResponseServerError(
                "Cache: não foi possível conectar ao cache.")

        # Retorna uma confirmação de OK para o protocolo HTTP
        return HttpResponse("OK")
예제 #7
0
    def readiness(self, request):
        # Connect to each database and do a generic standard SQL query
        # that doesn't write any data and doesn't depend on any tables
        # being present.
        newrelic.agent.ignore_transaction(flag=True)

        try:
            from django.db import connections
            for name in connections:
                cursor = connections[name].cursor()
                cursor.execute('SELECT 1;')
                row = cursor.fetchone()
                if row is None:
                    return HttpResponseServerError('db: invalid response')
        except Exception as e:
            logger.exception(e)
            return HttpResponseServerError('db: cannot connect to database.')

        # Call get_stats() to connect to each memcached instance and get it's stats.
        # This can effectively check if each is online.
        try:
            from django.core.cache import caches
            from django.core.cache.backends.memcached import BaseMemcachedCache
            for cache in caches.all():
                if isinstance(cache, BaseMemcachedCache):
                    stats = cache._cache.get_stats()
                    if len(stats) != len(cache._servers):
                        return HttpResponseServerError(
                            'cache: cannot connect to cache.')
        except Exception as e:
            logger.exception(e)
            return HttpResponseServerError('cache: cannot connect to cache.')

        return HttpResponse('OK')
예제 #8
0
class HealthCheckMiddleware(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        # One-time configuration and initialization.

    def process_request(self, request):
        if request.method == "GET":
            if request.path == "/_status/readiness":
                return self.readiness(request)
            elif request.path == "/_status/healthz":
                return self.healthz(request)

    def __call__(self, request):
        if self.get_response:
            self.process_request(request)
            return self.get_response(request)
        else:
            raise RuntimeError(
                "No get_response callable given. Callable middleware not supported."
            )

    def healthz(self, request):
        """
        Returns that the server is alive.
        """
        return HttpResponse("OK")

    def readiness(self, request):
        # Connect to each database and do a generic standard SQL query
        # that doesn't write any data and doesn't depend on any tables
        # being present.
        try:
            from django.db import connections

            for name in connections:
                cursor = connections[name].cursor()
                cursor.execute("SELECT 1;")
                row = cursor.fetchone()
                if row is None:
                    return HttpResponseServerError("db: invalid response")
        except Exception, e:
            logger.exception(e)
            return HttpResponseServerError("db: cannot connect to database.")

        # Call get_stats() to connect to each memcached instance and get it's stats.
        # This can effectively check if each is online.
        try:
            from django.core.cache import caches
            from django.core.cache.backends.memcached import BaseMemcachedCache

            for cache in caches.all():
                if isinstance(cache, BaseMemcachedCache):
                    stats = cache._cache.get_stats()
                    if len(stats) != len(cache._servers):
                        return HttpResponseServerError(
                            "cache: cannot connect to cache.")
        except Exception, e:
            logger.exception(e)
            return HttpResponseServerError("cache: cannot connect to cache.")
예제 #9
0
def patchInitializedCaches(lib):
    from django.core.cache import caches
    cache_classes = caches.all()
    for cache_class in cache_classes:
        cache_class._lib = lib
        if hasattr(cache_class, '_client'):
            client_kwargs = dict(pickleProtocol=pickle.HIGHEST_PROTOCOL)
            client_kwargs.update(cache_class._options)
            cache_class._client = lib.Client(
                cache_class._servers, **client_kwargs)
예제 #10
0
 def instrument(self):
     from django.core.cache import caches
     cache_classes = caches.all()
     for cache_class in cache_classes:
         cache_class._lib = Library
         if hasattr(cache_class, '_client'):
             client_kwargs = dict(pickleProtocol=pickle.HIGHEST_PROTOCOL)
             client_kwargs.update(cache_class._options)
             cache_class._client = ScoobyMemcacheClient(
                 cache_class._servers, **client_kwargs)
     super(MemcachePlugin, self).instrument()
예제 #11
0
 def enable_instrumentation(self):
     # Monkey patch all open cache connections.  Django maintains cache connections
     # on a per-thread/async task basis, so this will not affect any concurrent
     # requests.  The monkey patch of CacheHander.create_connection() installed in
     # the .ready() method will ensure that any new cache connections that get opened
     # during this request will also be monkey patched.
     for cache in caches.all(initialized_only=True):
         self._monkey_patch_cache(cache)
     # Mark this panel instance as the current one for the active thread/async task
     # context.  This will be used by the CacheHander.create_connection() monkey
     # patch.
     self._context_locals.current_instance = self
예제 #12
0
    def healthz(self, request):
        """Return that the server is healthy."""
        # Connect to each database and do a generic standard SQL query
        # that doesn't write any data and doesn't depend on any tables
        # being present.
        try:
            from django.db import connections

            for name in connections:
                cursor = connections[name].cursor()
                cursor.execute("SELECT 1;")
                row = cursor.fetchone()
                if row is None:
                    return self._response(ko="db: invalid response")
        except Exception as e:
            self.logger.exception(e)
            return self._response(ko="db: cannot connect to database")

        # Do a roundtrip SET and GET on a random key/value.
        # This can effectively check if each is online.
        try:
            from django.core.cache import caches

            caches["default"]
            if not caches.all():
                return self._response(ko="cache: no cache configured")
            for cache in caches.all():
                cache_value = uuid.uuid4().hex
                cache.set(self.cache_key, cache_value)
                result = cache.get(self.cache_key)
                if result != cache_value:
                    return self._response(ko="cache: round trip error")
        except Exception as e:
            self.logger.exception(e)
            return self._response(ko="cache: cannot connect to cache")

        return self._response()
예제 #13
0
def django_cache_add_xdist_key_prefix(request):
    skip_if_no_django()

    xdist_prefix = getattr(request.config, 'workerinput', {}).get('workerid')

    if xdist_prefix:
        # Put a prefix like gw0_, gw1_ etc on xdist processes
        for existing_cache in caches.all():
            existing_cache.key_prefix = xdist_prefix + '_' + existing_cache.key_prefix
            existing_cache.clear()
            logger.info('Set existing cache key prefix to [%s]', existing_cache.key_prefix)

        for name, cache_settings in settings.CACHES.items():
            cache_settings['KEY_PREFIX'] = xdist_prefix + '_' + cache_settings.get('KEY_PREFIX', '')
            logger.info('Set cache key prefix for [%s] cache to [%s]', name, cache_settings['KEY_PREFIX'])
예제 #14
0
def classes(request):
    print(caches.all())
    print("Cache is")
    print(cache.has_key('%sall_classes' % (cache.key_prefix)))
    if cache.get('all_classes') == None:
        print("NOT using cache")
        classes = requests.get(BASE_PATH + CLASSES_PATH).json()
        all_classes = []
        for d3_class in classes['results']:
            print(d3_class['url'])
            class_details = requests.get(BASE_PATH + d3_class['url'])
            all_classes.append(class_details.json())
        cache.set('all_classes', all_classes, 86400)
        print(cache.key_prefix)

    else:
        print("using cache")
        all_classes = cache.get('all_classes')

    return render(request, 'classes.html', {'classes': all_classes})
예제 #15
0
 def _clear_caches(self):
     for cache in caches.all():
         cache.clear()
예제 #16
0
파일: utils.py 프로젝트: nitely/Spirit
def cache_clear():
    cache.clear()  # Default one

    for c in caches.all():
        c.clear()
예제 #17
0
    def handle(self, *args, **options):
        for c in caches.all():
            c.clear()

        exit(0)
예제 #18
0
def django_caches():
    yield caches
    # finalizer
    for cache in caches.all():
        cache.clear()
예제 #19
0
def clear_caches():
    for cache in caches.all():
        cache.clear()
예제 #20
0
 def check_caches(self, key):
     """Check that caches are empty, and add values."""
     for cache in caches.all():
         assert cache.get(key) is None
         cache.set(key, "Not None")
예제 #21
0
 def all(cls):
     return caches.all()
예제 #22
0
def cache_clear():
    cache.clear()  # Default one

    for c in caches.all():
        c.clear()
def clear_caches():
    from django.core.cache import caches

    for cache in caches.all():
        cache.clear()
예제 #24
0
 def disable_instrumentation(self):
     if hasattr(self._context_locals, "current_instance"):
         del self._context_locals.current_instance
     for cache in caches.all(initialized_only=True):
         self._unmonkey_patch_cache(cache)
async def _close_async_caches():
    for cache in caches.all():
        await cache.close_async()
예제 #26
0
def clear_caches(request):
    for existing_cache in caches.all():
        existing_cache.clear()
예제 #27
0
 def check_caches(self, key):
     """Check that caches are empty, and add values."""
     for cache in caches.all():
         self.assertIsNone(cache.get(key))
         cache.set(key, "Not None")