1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
checktheroads-hyperglass/hyperglass/gunicorn_config.py.example

72 lines
2.1 KiB
Plaintext
Raw Normal View History

"""
https://github.com/checktheroads/hyperglass
Guncorn configuration
"""
import os
import shutil
2019-05-11 23:22:27 -07:00
import multiprocessing
from logzero import logger
2019-05-11 23:22:27 -07:00
command = "/usr/local/bin/gunicorn"
2019-05-28 14:14:42 -07:00
pythonpath = "/opt/hyperglass"
2019-05-11 23:22:27 -07:00
bind = "[::1]:8001"
2019-05-28 14:14:42 -07:00
preload = True
workers = multiprocessing.cpu_count() * 2
2019-05-11 23:22:27 -07:00
user = "www-data"
2019-05-11 23:28:13 -07:00
timeout = 60
2019-05-28 14:14:42 -07:00
keepalive = 10
# Prometheus Multiprocessing directory, set as environment variable
2019-06-15 12:42:28 -07:00
prometheus_multiproc_dir = "/tmp/hyperglass_prometheus"
def on_starting(server): # pylint: disable=unused-argument
"""Pre-startup Gunicorn Tasks"""
2019-06-15 12:42:28 -07:00
# Renders Jinja2 -> Sass, compiles Sass -> CSS prior to worker load
try:
import hyperglass.render
hyperglass.render.css()
print(1)
except ImportError as error_exception:
logger.error(f"Exception occurred:\n{error_exception}")
2019-06-15 12:42:28 -07:00
# Verify Redis is running
try:
import hyperglass.configuration
import redis
config = hyperglass.configuration.params()
redis_config = {
"host": config["general"]["redis_host"],
"port": config["general"]["redis_port"],
"charset": "utf-8",
"decode_responses": True,
"db": config["features"]["cache"]["redis_id"],
}
r_cache = redis.Redis(**redis_config)
if r_cache.set("testkey", "testvalue", ex=1):
logger.debug("Redis is working properly")
except (redis.exceptions.ConnectionError):
logger.error("Redis is not running")
raise EnvironmentError("Redis is not running")
2019-06-11 22:28:30 -07:00
# Prometheus multiprocessing directory
2019-06-15 12:42:28 -07:00
if os.path.exists(prometheus_multiproc_dir):
shutil.rmtree(prometheus_multiproc_dir)
else:
os.mkdir(prometheus_multiproc_dir)
os.environ["prometheus_multiproc_dir"] = prometheus_multiproc_dir
def worker_exit(server, worker): # pylint: disable=unused-argument
"""Prometheus multiprocessing WSGI support"""
from prometheus_client import multiprocess
multiprocess.mark_process_dead(worker.pid)
2019-05-28 14:14:42 -07:00
def on_exit(server):
"""Pre-shutdown Gunicorn Tasks"""
2019-06-15 12:42:28 -07:00
if os.path.exists(prometheus_multiproc_dir):
shutil.rmtree(prometheus_multiproc_dir)