Package: healthcheck-python
A Health Check API Library for Multiprocessing Python AppsThis library adds a health check REST API to your multiprocessing apps. You can add health check function calls to your
functions and library will track the function calls. This library supportsmultiprocessingthreads. You can fetch
a single overall app status by fetchinghttp://<ip>:<port>/healthcheck, a single overall app liveness by fetchinghttp://<ip>:<port>/liveness.Influenced by prometheus client mp exporter. Health check functions will write healthy and liveness results<pid>.jsonfile located in directory defined byPY_HEALTH_MULTIPROC_DIR. If the directory doesn't exist, health checks won't
work.Please clear the directory content before running your app.REST APIEach health check class will be run every 10 seconds by default. You can change this value by
settingPY_HEALTH_RUN_PERIOD.UsageYou can register your functions withadd_check()decorator.
You can set a timeout for your functions withset_timeout()if you process needs to check in regularly.importmultiprocessingasmpimporttimeimporthealthcheck_pythonashpclassP1(mp.Process):def__init__(self,timeout=0):super().__init__()self._stop_bit=mp.Event()self.timeout=timeoutdefclose(self)->None:self._stop_bit.set()defhealthcheck(self):returnTrue,"Healthcheck is OK"defdo_something(self):time.sleep(5)defrun(self):hp.init_check(timeout=self.timeout)hp.add_check(self.healthcheck)hp.live()whilenotself._stop_bit.is_set():hp.healthy()self.do_something()hp.start_http_server(port=8080)p1=P1(timeout=10)p1.start()p2=P1()p2.start()time.sleep(30)p1.close()p2.close()p1.join()p2.join()It can also be used with threading. You need to create separate Healthcheck instances and start instances in your
threads.importmultiprocessingasmpimportthreadingimporttimeimporthealthcheck_pythonashpfromhealthcheck_pythonimportcreate_health_checkHEALTHCHECK_P1=create_health_check("P1",timeout=5)HEALTHCHECK_P2=create_health_check("P2")classP1(threading.Thread):def__init__(self,timeout=0):super().__init__()self._stop_bit=mp.Event()self.timeout=timeoutdefclose(self)->None:self._stop_bit.set()defhealthcheck(self):returnTrue,"Healthcheck is OK"defdo_something(self):time.sleep(5)defrun(self):HEALTHCHECK_P1.add_check(self.healthcheck)HEALTHCHECK_P1.start()HEALTHCHECK_P1.live()whilenotself._stop_bit.is_set():HEALTHCHECK_P1.healthy()self.do_something()classP2(threading.Thread):def__init__(self,timeout=0):super().__init__()self._stop_bit=mp.Event()self.timeout=timeoutdefclose(self)->None:self._stop_bit.set()defhealthcheck(self):returnTrue,"Healthcheck is OK"defdo_something(self):time.sleep(5)defrun(self):HEALTHCHECK_P2.add_check(self.healthcheck)HEALTHCHECK_P2.start()HEALTHCHECK_P2.live()whilenotself._stop_bit.is_set():HEALTHCHECK_P2.healthy()self.do_something()hp.init_check(timeout=0)hp.start_http_server(port=8080)p1=P1(timeout=10)p1.start()p2=P2()p2.start()time.sleep(30)p1.close()p2.close()p1.join()p2.join()$curlhttp://localhost:8080/healthcheck{"hostname":"my_app","status":"success","timestamp":1684406235.474363,"results":[[{"checker":"healthcheck","output":"Healthcheck is OK","passed":true,"timestamp":1684406230.9507005,"response_time":5e-06},{"checker":"P1","output":"","passed":true,"timestamp":1684406230.9507082,"response_time":0}]]}$curlhttp://localhost:8080/liveness{"hostname":"my_app","liveness":true,"timestamp":1684406208.784097}