def handle(self, *args, **options): """Command Handle""" if len(options['command'] ) == 0 or options['command'][0] not in self.available: raise CommandError( 'Command Does not exist! Please use one of the following: python manage.py health [%s]' % ", ".join(self.available)) command = options['command'][0] if command == "check": health = Health() status = Health.OK errors = [] errors.extend(health.check_db()) errors.extend(health.check_io()) errors.extend(health.check_workers()) if len(errors) > 0: status = Health.NOT_OK if status == Health.OK: print(Health.OK) else: raise Exception("%(status)s: %(errors)s" % { "status": Health.NOT_OK, "errors": ", ".join(errors) })
def get(self, request): self.__health = Health() self.__helpers = Helpers() self.__logger = self.__helpers.get_logger(__name__) status = Health.OK errors = [] errors.extend(self.__health.check_db()) errors.extend(self.__health.check_io()) errors.extend(self.__health.check_workers()) if len(errors) > 0: status = Health.NOT_OK self.__logger.error( _("Health Check Result: %(status)s %(errors)s") % { "status": status, "errors": self.__helpers.json_dumps(errors) }) else: self.__logger.info( _("Health Check Result: %(status)s %(errors)s") % { "status": status, "errors": self.__helpers.json_dumps(errors) }) return JsonResponse({ "status": status, "messages": [] }, status=200 if status == Health.OK else 503)
class HealthCheck(View): __response = None __correlation_id = None __health = None __helpers = None __logger = None def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__response = Response() self.__health = Health() self.__helpers = Helpers() self.__logger = self.__helpers.get_logger(__name__) status = Health.OK errors = [] errors.extend(self.__health.check_db()) errors.extend(self.__health.check_io()) errors.extend(self.__health.check_workers()) if len(errors) > 0: status = Health.NOT_OK self.__logger.error( _("Health Check Result: %(status)s %(errors)s {'correlationId':'%(correlationId)s'}" ) % { "status": status, "errors": self.__helpers.json_dumps(errors), "correlationId": self.__correlation_id }) else: self.__logger.debug( _("Health Check Result: %(status)s %(errors)s {'correlationId':'%(correlationId)s'}" ) % { "status": status, "errors": self.__helpers.json_dumps(errors), "correlationId": self.__correlation_id }) return JsonResponse(self.__response.send({"status": status}, self.__correlation_id), status=200 if status == Health.OK else 503)