Skip to content

Commit

Permalink
HEAT-226 Storing latest info, version and health as individual keys (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrlee authored Mar 13, 2024
1 parent 94ae415 commit acd04fe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SERVICE_CATALOGUE_API_ENDPOINT=
SERVICE_CATALOGUE_API_KEY=
REDIS_ENDPOINT=localhost
REDIS_PORT=6379
REDIS_TLS_ENABLED=false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# dotenv environment variables file
.env*
!.env.example

.idea
.vscode
**/Chart.lock
__pycache__/
.python
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ info:[component name]:[environment name]
health:[component name]:[environment name]
version:[component name]:[environment name]
```

# How to run locally

Ensure python is installed: `brew install python`
Create local env: `python3 -m venv .python`
Ensure all dependencies are installed: `.python/bin/pip install -r requirements.txt`
Copy and create .env file: `cp .env.example .env`
Start redis by running `docker compose up -d`.

Run: `export $(cat .env) && .python/bin/python health_ping.py`
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3.1'
services:

redis:
image: 'redis/redis-stack:7.2.0-v9'
networks:
- hmpps
container_name: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- '6379:6379'
healthcheck:
test: [ "CMD-SHELL", "redis-cli ping | grep PONG" ]
interval: 5s
timeout: 3s
retries: 5

networks:
hmpps:
15 changes: 14 additions & 1 deletion health_ping.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
'''Health ping - fetches all /health and /info endpoints and stores the results in Redis'''
from datetime import datetime
import os
import threading
import logging
Expand Down Expand Up @@ -34,7 +35,7 @@ def update_sc_component(c_id, data):

def update_app_version(app_version, c_name, e_name):
version_key = f'version:{c_name}:{e_name}'
version_data={'v': app_version}
version_data={'v': app_version, 'dateAdded': datetime.now().isoformat()}
try:
# Get last entry to version stream
last_entry_version = redis.xrevrange(version_key, max='+', min='-', count=1)
Expand All @@ -43,11 +44,13 @@ def update_app_version(app_version, c_name, e_name):
else:
# Must be first time entry.
redis.xadd(version_key, version_data, maxlen=200, approximate=False)
redis.json().set('latest:versions', f'$.{version_key}', version_data)
log.debug(f"First version entry = {version_key}:{version_data}")
return

if last_version != app_version:
redis.xadd(version_key, version_data, maxlen=200, approximate=False)
redis.json().set('latest:versions', f'$.{version_key}', stream_data)
log.info(f'Updating redis with new version. {version_key} = {version_data}')
except Exception as e:
log.error(e)
Expand All @@ -58,6 +61,8 @@ def process_env(c_name, e_name, endpoint, endpoint_type, component):
stream_key = f'{endpoint_type}:{c_name}:{e_name}'
stream_data = {}
stream_data.update({'url': endpoint})
stream_data.update({'dateAdded': datetime.now().isoformat()})

try:
# Override default User-Agent other gets blocked by mod security.
headers = {'User-Agent': 'hmpps-health-ping'}
Expand Down Expand Up @@ -141,6 +146,7 @@ def process_env(c_name, e_name, endpoint, endpoint_type, component):

try:
redis.xadd(stream_key, stream_data, maxlen=redis_max_stream_length, approximate=False)
redis.json().set(f'latest:{endpoint_type}', f'$.{stream_key}', stream_data)
log.debug(f"{stream_key}: {stream_data}")
except Exception as e:
log.error(f"Unable to add data to redis stream. {e}")
Expand Down Expand Up @@ -183,6 +189,13 @@ def startHttpServer():
redis = redis.Redis(**redis_connect_args)
redis.ping()
log.info("Successfully connected to redis.")
# Create root objects for latest if they don't exist
if not redis.exists('latest:health'):
redis.json().set(f'latest:health', '$', {})
if not redis.exists('latest:info'):
redis.json().set('latest:info', '$', {})
if not redis.exists('latest:versions'):
redis.json().set('latest:versions', '$', {})
except Exception as e:
log.critical("Unable to connect to redis.")
raise SystemExit(e)
Expand Down

0 comments on commit acd04fe

Please sign in to comment.