diff --git a/README.md b/README.md index eda41b5..5873cfa 100644 --- a/README.md +++ b/README.md @@ -104,8 +104,23 @@ On a `Intel(R) Xeon(R) E-2288G CPU @ 3.70GHz` (16 core), it will sustain 150 con Most of the CPU load seems to occur when starting a browser, maybe in the future 1 browser could processes multiple requests. +### Docker healthcheck + +Add this to your `docker-compose.yml`, it will check port 3000 answers and that the `/stats` endpoint on port 8080 responds + +``` + healthcheck: + test: + CMD-SHELL: python3 /usr/src/app/docker-health-check.py --host http://localhost + interval: 30s + timeout: 5s + retries: 3 + start_period: 10s +``` + ### Future ideas - Some super cool "on the wire" hacks to add custom functionality to CDP, like issuing single commands to download files (PDF) to location https://github.com/dgtlmoon/changedetection.io/issues/2019 + Have fun! diff --git a/backend/docker-health-check.py b/backend/docker-health-check.py new file mode 100755 index 0000000..b99f7ea --- /dev/null +++ b/backend/docker-health-check.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +import sys +import socket +import urllib.request +import argparse +from urllib.parse import urlparse + +def main(): + parser = argparse.ArgumentParser(description='Health check script') + parser.add_argument( + '--host', + default='http://localhost', + help='Hostname or URL to check (default: http://localhost)' + ) + args = parser.parse_args() + + # Extract hostname and scheme from the provided host argument + host_input = args.host + + # Parse the URL to extract components + parsed_url = urlparse(host_input) + + # If scheme is missing, assume 'http' and parse again + if not parsed_url.scheme: + host_input = f'http://{host_input}' + parsed_url = urlparse(host_input) + + hostname = parsed_url.hostname or 'localhost' + scheme = parsed_url.scheme or 'http' + netloc = parsed_url.netloc or 'localhost' + + # Reconstruct the base URL without any path, params, query, or fragment + base_url = f'{scheme}://{netloc}' + + try: + # Strip 'http://' or 'https://' for socket connection + # Check connection to port 3000 + s = socket.create_connection((hostname, 3000), timeout=5) + s.close() + + # Check HTTP status code at /stats on port 8080 + stats_url = f'{base_url}:8080/stats' + response = urllib.request.urlopen(stats_url, timeout=5) + sys.exit(0 if response.status == 200 else 1) + except Exception as e: + # Optionally, print the exception for debugging + print(f'Health check failed: {e}', file=sys.stderr) + sys.exit(1) + +if __name__ == '__main__': + main() \ No newline at end of file