Skip to content

Commit

Permalink
Adding docker healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtlmoon committed Oct 6, 2024
1 parent 1662a94 commit af93495
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
52 changes: 52 additions & 0 deletions backend/docker-health-check.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit af93495

Please sign in to comment.