def test_add_check(): checker = HealthCheck() assert len(checker.checks) == 0 def check(): return True, '' checker.add_check(check) assert len(checker.checks) == 1 checker.add_check(check) assert len(checker.checks) == 2
app = Sanic() health_check = HealthCheck(app, no_cache=True) ready_check = ReadyCheck(app) start = time.time() @app.route('/') async def test(request): return response.json({'hello', 'world'}) # Define checks for the health check. def check_health_random(): if random.random() > 0.9: return False, 'the random number is > 0.9' return True, 'the random number is <= 0.9' # Define checks for the ready check. def check_ready(): if time.time() > start + 7: return True, 'ready: seven seconds elapsed' return False, 'not ready: seven seconds have not elapsed yet' if __name__ == '__main__': health_check.add_check(check_health_random) ready_check.add_check(check_ready) app.run(host='0.0.0.0', port=8000)
app = Sanic(__name__) CORS(app) logging.basicConfig(level='INFO', filename="/mnt/log/worker.log", filemode='a', format='%(message)s', datefmt='%Y-%m-%d %H:%M:%S') logging.addLevelName(logging.WARNING, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.WARNING)) logging.addLevelName(logging.ERROR, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.ERROR)) logger = logging.getLogger(__name__) health = HealthCheck(app, "/healthcheck") def worker_status(): return True, 'worker ok' health.add_check(worker_status) @app.route('/load', methods=['POST']) async def load(request): timer = Timer() global_response = update_status = update_models() update_model_detection() elapsed = round(timer.value(), 3) logging.info(json.dumps({'response': global_response, 'elapsed': elapsed})) return sanic_json({"response": global_response, 'success': True, 'elapsed': elapsed}, 200)