Skip to main content

Health Check API for multiprocessing python apps

Project description

A Health Check API Library for Multiprocessing Python Apps

license

This 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 supports multiprocessing threads. You can fetch a single overall app status by fetching http://<ip>:<port>/healthcheck, a single overall app liveness by fetching http://<ip>:<port>/liveness.

Influenced by prometheus client mp exporter. Health check functions will write healthy and liveness results <pid>.json file located in directory defined by PY_HEALTH_MULTIPROC_DIR. If the directory doesn't exist, health checks won't work.

Please clear the directory content before running your app. REST API

Each health check class will be run every 10 seconds by default. You can change this value by setting PY_HEALTH_RUN_PERIOD.

Usage

You can register your functions with add_check() decorator. You can set a timeout for your functions with set_timeout() if you process needs to check in regularly.

import multiprocessing as mp
import time

import healthcheck_python as hp


class P1(mp.Process):
	def __init__(self, timeout=0):
		super().__init__()
		self._stop_bit = mp.Event()
		self.timeout = timeout

	def close(self) -> None:
		self._stop_bit.set()

	def healthcheck(self):
		return True, "Healthcheck is OK"

	def do_something(self):
		time.sleep(5)

	def run(self):
		hp.add_check(self.healthcheck)
		hp.set_timeout(self.timeout)

		hp.live()
		while not self._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()
$ curl http://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}]]}
$ curl http://localhost:8080/liveness
{"hostname": "my_app", "liveness": true, "timestamp": 1684406208.784097}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

healthcheck_python-1.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

healthcheck_python-1.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file healthcheck_python-1.1.0.tar.gz.

File metadata

  • Download URL: healthcheck_python-1.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/6.4.10-200.fc38.x86_64

File hashes

Hashes for healthcheck_python-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6fdc2bc883b52c7d43426271b4d8b91350dee3f1efac37af1deb7cf8e30f5c74
MD5 0c9b2e02a9bca6c1e815b88a76ede9f0
BLAKE2b-256 5c4399fa491a9923865809510203782b06097775716442eeb1cb0fcfe7ff2267

See more details on using hashes here.

File details

Details for the file healthcheck_python-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: healthcheck_python-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/6.4.10-200.fc38.x86_64

File hashes

Hashes for healthcheck_python-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f8e14977f5b6d1dc186b2e27f8292831748259de098da83227eacb0db400736
MD5 6c01eb4693a2579c51161e8fa3f38aa0
BLAKE2b-256 b40b8fc1b675f595f7385e7479ac3de3827d816db200bcc47514d79a0f24daf3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page