def check_status(self): timeout = getattr(settings, 'HEALTHCHECK_CELERY_TIMEOUT', 3) try: result = add.apply_async(args=[4, 4], expires=datetime.now() + timedelta(seconds=timeout)) now = datetime.now() while (now + timedelta(seconds=3)) > datetime.now(): print(" checking....") if result.ready(): try: result.forget() except NotImplementedError: pass return True sleep(0.5) except IOError: logger.exception("IOError") raise ServiceUnavailable("IOError") except: logger.exception("Unknown Error") raise ServiceUnavailable("Unknown error") logger.error(u'Celery task did not complete successfully. ' u'Verify celery is running') raise ServiceUnavailable("Unknown error")
def check_status(self): try: # write the file to the storage backend storage = self.get_storage() file_name = self.get_file_name() file_content = self.get_file_content() # save the file file_name = storage.save(file_name, ContentFile(content=file_content)) # read the file and compare f = storage.open(file_name) if not storage.exists(file_name): raise ServiceUnavailable("File does not exist") read_file_contents = f.read() if not read_file_contents.decode("utf-8") == file_content.decode( "utf-8"): raise ServiceUnavailable("File content doesn't match") # delete the file and make sure it is gone storage.delete(file_name) if storage.exists(file_name): raise ServiceUnavailable("File was not deleted") return True except ServiceUnavailable as service_unavailable_exc: raise service_unavailable_exc except Exception as exc: raise ServiceUnavailable("Unknown exception")
def check_status(self): try: cache = get_cache("default") cache.set("health", True) if not cache.get("health"): raise ServiceUnavailable("Default Cache not storing values") return True except InvalidCacheBackendError: raise ServiceUnavailable("Default Cache unavailable")
def check_status(self): try: cache.set('djangohealtcheck_test', 'itworks', 1) if cache.get("djangohealtcheck_test") == "itworks": return True else: raise ServiceUnavailable("Cache key does not match") except CacheKeyWarning: raise ServiceReturnedUnexpectedResult("Cache key warning") except ValueError: raise ServiceReturnedUnexpectedResult("ValueError") except Exception: raise ServiceUnavailable("Unknown exception")
def check_status(self): try: obj = TestModel.objects.create(title="test") obj.title = "newtest" obj.save() obj.delete() return True except IntegrityError: logger.exception("Integrity Error") raise ServiceReturnedUnexpectedResult("Integrity Error") except DatabaseError: logger.exception("Database Error") raise ServiceUnavailable("Database Error") except Exception: raise ServiceUnavailable("Unknown Error")
def check_status(self): try: items = Item.objects.all()[0] if items != None: return True except OperationalError: raise ServiceUnavailable("Database unavailable")
def check_status(self): try: check_geoserver_is_up() return True except AssertionError: logger.exception("Unknown Error") raise ServiceUnavailable("Unknown error")
def check_status(self): try: expected = 'itworks' cache.set('djangohealtcheck_test', expected, 1) got = cache.get("djangohealtcheck_test") if got == expected: return True else: raise ServiceUnavailable( "Cache key does not match. Got %s, expected %s" % (got, expected)) except CacheKeyWarning as e: raise ServiceReturnedUnexpectedResult("Cache key warning: %s" % e) except ValueError as e: raise ServiceReturnedUnexpectedResult("ValueError: %s" % e) except Exception as e: raise ServiceUnavailable("Unknown cache exception: %s" % e)
class StorageHealthCheck(BaseHealthCheckBackend): """ Tests the status of a `StorageBackend`. Can be extended to test any storage backend by subclassing: class MyStorageHealthCheck(StorageHealthCheck): storage = 'some.other.StorageBackend' plugin_dir.register(MyStorageHealthCheck) storage must be either a string pointing to a storage class (e.g 'django.core.files.storage.FileSystemStorage') or a Storage instance. """ storage = None def get_storage(self): if isinstance(self.storage, string_types): return get_storage_class(self.storage)() else: return self.storage def get_file_name(self): return 'health_check_storage_test/test-{}-{}.txt'.format( datetime.datetime.now().strftime('%Y%m%dT%H%M%SZ'), random.randint(10000, 99999)) def get_file_content(self): return b'this is the healthtest file content' def check_status(self): try: # write the file to the storage backend storage = self.get_storage() file_name = self.get_file_name() file_content = self.get_file_content() # save the file file_name = storage.save(file_name, ContentFile(content=file_content)) if not storage.exists(file_name): raise ServiceUnavailable('File does not exist') # read the file and compare f = storage.open(file_name) if not f.read() == file_content: raise ServiceUnavailable('File content doesn\'t match') # delete the file and make sure it is gone storage.delete(file_name) if storage.exists(file_name): raise ServiceUnavailable('File was not deleted') return True except HealthCheckException, e: raise e except Exception: raise ServiceUnavailable('Unknown exception')
def check_status(self): try: obj = TestModel.objects.create(title="test") obj.title = "newtest" obj.save() obj.delete() return True except IntegrityError as e: raise ServiceReturnedUnexpectedResult("Integrity Error: %s" % e) except DatabaseError as e: raise ServiceUnavailable("Database error: %s" % e)
def check_status(self): try: # write the file to the storage backend storage = self.get_storage() file_name = self.get_file_name() file_content = self.get_file_content() # save the file file_name = storage.save(file_name, ContentFile(content=file_content)) if not storage.exists(file_name): raise ServiceUnavailable('File does not exist') # read the file and compare f = storage.open(file_name) if not f.read() == file_content: raise ServiceUnavailable('File content doesn\'t match') # delete the file and make sure it is gone storage.delete(file_name) if storage.exists(file_name): raise ServiceUnavailable('File was not deleted') return True except HealthCheckException, e: raise e
def check_status(self): try: result = add.apply_async(args=[4, 4], expires=datetime.now() + timedelta(seconds=3), connect_timeout=3) now = datetime.now() while (now + timedelta(seconds=3)) > datetime.now(): if result.result == 8: return True sleep(0.5) except IOError: pass raise ServiceUnavailable("Unknown error")
def check_status(self): timeout = getattr(settings, 'HEALTHCHECK_CELERY_TIMEOUT', 3) expires = datetime.now() + timedelta(seconds=timeout) try: result = add.apply_async(args=[4, 4], expires=expires, connect_timeout=timeout) now = datetime.now() while (now + timedelta(seconds=3)) > datetime.now(): if result.result == 8: return True sleep(0.5) except IOError: pass raise ServiceUnavailable("Unknown error")