2
0
mirror of https://github.com/frappe/frappe_docker.git synced 2024-09-19 18:49:01 +00:00

Merge pull request #146 from revant/fix-python-container-healthcheck

fix: python container healthcheck
This commit is contained in:
Revant Nandgaonkar 2020-03-14 22:48:55 +05:30 committed by GitHub
commit 13b9a5f748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 35 deletions

View File

@ -1,6 +1,13 @@
import socket, os, json, time
from six.moves.urllib.parse import urlparse
COMMON_SITE_CONFIG_FILE = 'common_site_config.json'
REDIS_QUEUE_KEY = 'redis_queue'
REDIS_CACHE_KEY = 'redis_cache'
REDIS_SOCKETIO_KEY = 'redis_socketio'
DB_HOST_KEY = 'db_host'
DB_PORT = 3306
def is_open(ip, port, timeout=30):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
@ -13,9 +20,10 @@ def is_open(ip, port, timeout=30):
finally:
s.close()
def check_host(ip, port, retry=10, delay=3):
def check_host(ip, port, retry=10, delay=3, print_attempt=True):
ipup = False
for i in range(retry):
if print_attempt:
print("Attempt {i} to connect to {ip}:{port}".format(ip=ip,port=port,i=i+1))
if is_open(ip, port):
ipup = True
@ -25,43 +33,87 @@ def check_host(ip, port, retry=10, delay=3):
return ipup
# Check connection to servers
def get_config():
config = None
try:
with open('common_site_config.json') as config_file:
with open(COMMON_SITE_CONFIG_FILE) as config_file:
config = json.load(config_file)
except FileNotFoundError:
raise FileNotFoundError("common_site_config.json missing")
except FileNotFoundError as exception:
print(exception)
exit(1)
except:
raise ValueError("common_site_config.json is not valid")
print(COMMON_SITE_CONFIG_FILE+" is not valid")
exit(1)
return config
# Check mariadb
def check_mariadb(retry=10, delay=3, print_attempt=True):
config = get_config()
check_mariadb = False
check_mariadb = check_host(config.get('db_host', 'mariadb'), 3306)
check_mariadb = check_host(
config.get(DB_HOST_KEY, 'mariadb'),
DB_PORT,
retry,
delay,
print_attempt)
if not check_mariadb:
raise ConnectionError("Connection to mariadb timed out")
print("Connection to MariaDB timed out")
exit(1)
# Check redis queue
def check_redis_queue(retry=10, delay=3, print_attempt=True):
check_redis_queue = False
redis_queue_url = urlparse(config.get("redis_queue","redis://redis:6379")).netloc
config = get_config()
redis_queue_url = urlparse(config.get(REDIS_QUEUE_KEY,"redis://redis-queue:6379")).netloc
redis_queue, redis_queue_port = redis_queue_url.split(":")
check_redis_queue = check_host(redis_queue, redis_queue_port)
check_redis_queue = check_host(
redis_queue,
redis_queue_port,
retry,
delay,
print_attempt)
if not check_redis_queue:
raise ConnectionError("Connection to redis queue timed out")
print("Connection to redis queue timed out")
exit(1)
# Check redis cache
def check_redis_cache(retry=10, delay=3, print_attempt=True):
check_redis_cache = False
redis_cache_url = urlparse(config.get("redis_cache","redis://redis:6379")).netloc
config = get_config()
redis_cache_url = urlparse(config.get(REDIS_CACHE_KEY,"redis://redis-cache:6379")).netloc
redis_cache, redis_cache_port = redis_cache_url.split(":")
check_redis_cache = check_host(redis_cache, redis_cache_port)
check_redis_cache = check_host(
redis_cache,
redis_cache_port,
retry,
delay,
print_attempt)
if not check_redis_cache:
raise ConnectionError("Connection to redis cache timed out")
print("Connection to redis cache timed out")
exit(1)
# Check redis socketio
def check_redis_socketio(retry=10, delay=3, print_attempt=True):
check_redis_socketio = False
redis_socketio_url = urlparse(config.get("redis_socketio","redis://redis:6379")).netloc
config = get_config()
redis_socketio_url = urlparse(config.get(REDIS_SOCKETIO_KEY,"redis://redis-socketio:6379")).netloc
redis_socketio, redis_socketio_port = redis_socketio_url.split(":")
check_redis_socketio = check_host(redis_socketio, redis_socketio_port)
check_redis_socketio = check_host(
redis_socketio,
redis_socketio_port,
retry,
delay,
print_attempt)
if not check_redis_socketio:
raise ConnectionError("Connection to redis socketio timed out")
print("Connection to redis socketio timed out")
exit(1)
def main():
check_mariadb()
check_redis_queue()
check_redis_cache()
check_redis_socketio()
print('Connections OK')
if __name__ == "__main__":
main()

View File

@ -1,4 +1,26 @@
import frappe
from frappe.utils.doctor import doctor
import json
import redis
from rq import Worker
from check_connection import (
check_mariadb,
check_redis_cache,
check_redis_queue,
check_redis_socketio,
)
doctor()
def main():
check_mariadb(retry=1, delay=0, print_attempt=False)
print("MariaDB Connected")
check_redis_cache(retry=1, delay=0, print_attempt=False)
print("Redis Cache Connected")
check_redis_queue(retry=1, delay=0, print_attempt=False)
print("Redis Queue Connected")
check_redis_socketio(retry=1, delay=0, print_attempt=False)
print("Redis SocketIO Connected")
print("Health check successful")
exit(0)
if __name__ == "__main__":
main()