Skip to content

Commit 3fcbbac

Browse files
committed
Implement health checks
Implements service status tracking Adds Docker health checks Includes gitignore updates
1 parent 212a522 commit 3fcbbac

6 files changed

Lines changed: 75 additions & 8 deletions

File tree

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/config.json
2-
*.egginfo
2+
*.egg-info
33
build
44
dist
5-
.pytest_cache
5+
.pytest_cache
6+
__pycache__

Dockerfile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ ENV OVOS_CONFIG_BASE_FOLDER=neon
77
ENV OVOS_CONFIG_FILENAME=diana.yaml
88
ENV OVOS_DEFAULT_CONFIG=/opt/neon/diana.yaml
99
ENV XDG_CONFIG_HOME=/config
10+
ENV HEALTHCHECK_PORT=8000
11+
1012
COPY docker_overlay/ /
1113

1214
RUN apt-get update && \
1315
apt-get install -y \
1416
gcc \
17+
curl \
18+
jq \
1519
python3 \
1620
python3-dev \
1721
&& pip install wheel
1822

19-
ADD . /neon_api_proxy
23+
COPY . /neon_api_proxy
2024
WORKDIR /neon_api_proxy
21-
RUN pip install .
25+
RUN pip install --no-cache-dir .
2226

23-
CMD ["neon_api_proxy"]
27+
HEALTHCHECK CMD "/opt/neon/healthcheck.sh"
28+
CMD ["neon_api_proxy"]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
# NEON AI (TM) SOFTWARE, Software Development Kit & Application Development System
3+
# All trademark and other rights reserved by their respective owners
4+
# Copyright 2008-2025 Neongecko.com Inc.
5+
# BSD-3
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# 1. Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# 2. Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# 3. Neither the name of the copyright holder nor the names of its
14+
# contributors may be used to endorse or promote products derived from this
15+
# software without specific prior written permission.
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23+
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
28+
port="${HEALTHCHECK_PORT:-8080}"
29+
# Perform the health check using curl
30+
resp_content=$(curl -s http://localhost:${port}/status)
31+
status=$(echo "${resp_content}" | jq -r '.status')
32+
if [ "${status}" == "Ready" ]; then
33+
exit 0 # Success
34+
else
35+
echo "Health check failed with response: ${resp_content}" >&2
36+
exit 1 # Failure
37+
fi

neon_api_proxy/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2727
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

29+
from os import environ
2930
from ovos_utils import wait_for_exit_signal
3031
from ovos_utils.log import init_service_logger
32+
from neon_utils.process_utils import start_health_check_server
3133

3234
from neon_api_proxy.api_connector import NeonAPIMQConnector
3335
from neon_api_proxy.controller import NeonAPIProxyController
@@ -43,6 +45,11 @@ def run_mq_handler():
4345
connector = NeonAPIMQConnector(config=None,
4446
service_name='neon_api_connector',
4547
proxy=proxy)
48+
if status_port := environ.get("HEALTHCHECK_PORT"):
49+
start_health_check_server(
50+
connector.status,
51+
int(status_port),
52+
connector.check_health)
4653
connector.run()
4754
wait_for_exit_signal()
4855

neon_api_proxy/api_connector.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from typing import Optional
3232
from ovos_utils.log import LOG
33+
from ovos_utils.process_utils import ProcessStatus, ProcessState
3334
from neon_mq_connector.utils.network_utils import b64_to_dict, dict_to_b64
3435
from neon_mq_connector.connector import MQConnector
3536

@@ -48,10 +49,17 @@ def __init__(self, config: Optional[dict], service_name: str,
4849
:param service_name: name of the service instance
4950
"""
5051
super().__init__(config, service_name)
51-
52+
self.status = ProcessStatus(self.service_name)
53+
self.status.set_alive()
5254
self.vhost = '/neon_api'
5355
self.proxy = proxy
5456

57+
def check_health(self) -> bool:
58+
if not MQConnector.check_health(self):
59+
self.status.set_error("MQConnector health check failed")
60+
return False
61+
return self.status == ProcessState.READY
62+
5563
def handle_api_input(self,
5664
channel: pika.channel.Channel,
5765
method: pika.spec.Basic.Deliver,
@@ -126,10 +134,19 @@ def extract_agent_tokens(msg_data: dict) -> dict:
126134

127135
def handle_error(self, thread, exception):
128136
LOG.error(f"{exception} occurred in {thread}")
129-
LOG.info(f"Restarting Consumers")
137+
LOG.info("Restarting Consumers")
130138
self.stop()
131139
self.run()
132140

141+
def stop(self):
142+
self.status.set_stopping()
143+
MQConnector.stop(self)
144+
145+
def run(self):
146+
MQConnector.run(self)
147+
LOG.info("API Connector is running")
148+
self.status.set_ready()
149+
133150
def pre_run(self, **kwargs):
134151
self.register_consumer("neon_api_consumer", self.vhost,
135152
'neon_api_input', self.handle_api_input,

requirements/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
requests-cache~=0.6,>=0.6.4
22
requests~=2.20
3-
neon-utils[network,sentry]~=1.0
3+
neon-utils[network,sentry]~=1.0, >=1.12.2a2
44
ovos-utils>=0.0.31,<0.2.0
55
ovos-config~=0.1
66
neon-mq-connector~=0.8,>=0.8.1a3

0 commit comments

Comments
 (0)