Skip to content

Commit 08100a1

Browse files
Fix mqtt startup (#54)
* Fix MQTT startup * Increase version number * Update readme
1 parent 86ac220 commit 08100a1

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

docs/content/deployment/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ If an error happens during processing the request, that error is published to th
9595
9696
Start the docker container as explained in the HTTP API section.
9797
98-
To connect the MCP to a tool like Claude Desktop, follow their [guide](https://modelcontextprotocol.io/docs/develop/connect-local-servers). The following line has to be added to the `claude_desktop_config.json`:
98+
To connect the MCP to a tool like Claude Desktop, follow their [guide](https://modelcontextprotocol.io/docs/develop/connect-local-servers). The following `tirex` line has to be added to the `claude_desktop_config.json` under `mcpServers`:
9999
```json
100100
{
101101
"mcpServers": {

inference/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This docker container runs the TiRex model and provides the following APIs to interact with the model:
44
- **HTTP API**
55
- **MQTT**
6+
- **MCP**
67

78
## Using the docker container
89

@@ -73,7 +74,7 @@ If an error happens during processing the request, that error is published to th
7374
7475
Start the docker container as explained in the HTTP API section.
7576
76-
To connect the MCP to a tool like Claude Desktop, follow their [guide](https://modelcontextprotocol.io/docs/develop/connect-local-servers). The following line has to be added to the `claude_desktop_config.json`:
77+
To connect the MCP to a tool like Claude Desktop, follow their [guide](https://modelcontextprotocol.io/docs/develop/connect-local-servers). The following `tirex` line has to be added to the `claude_desktop_config.json` under `mcpServers`:
7778
```json
7879
{
7980
"mcpServers": {

inference/app/mqtt_server.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This software may be used and distributed according to the terms of the NXAI Community License Agreement.
33

44
import json
5+
import time
56

67
import paho.mqtt.client as mqtt
78
import requests
@@ -22,6 +23,7 @@ def __init__(self, config: Settings):
2223
self.client.on_message = self.on_message
2324
self.client.on_connect = self.on_connect
2425
self.client.on_disconnect = self.on_disconnect
26+
self.http_url = f"http://{self.config.http_host}:{self.config.http_port}"
2527

2628
def on_message(self, client, userdata, msg):
2729
try:
@@ -39,7 +41,7 @@ def on_message(self, client, userdata, msg):
3941

4042
def predict(self, context, prediction_length):
4143
response = requests.post(
42-
f"http://{self.config.http_host}:{self.config.http_port}/forecast/quantiles",
44+
f"{self.http_url}/forecast/quantiles",
4345
json={"context": context, "prediction_length": prediction_length},
4446
)
4547

@@ -52,6 +54,8 @@ def predict(self, context, prediction_length):
5254

5355
def connect(self, keepalive=60):
5456
try:
57+
print(f"MQTT is waiting for the HTTP server at {self.http_url} to load the model and go online")
58+
self.wait_for_api()
5559
print(f"Connecting to MQTT broker at {self.config.mqtt_broker_host}:{self.config.mqtt_broker_port}")
5660
self.client.connect(self.config.mqtt_broker_host, self.config.mqtt_broker_port, keepalive)
5761
self.client.loop_forever()
@@ -73,3 +77,14 @@ def on_connect(self, client, userdata, connect_flags, reason_code, properties):
7377
def on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties):
7478
if reason_code != 0:
7579
print(f"Unexpected disconnection from MQTT broker with code: {reason_code}")
80+
81+
def wait_for_api(self, timeout=300):
82+
for _ in range(timeout):
83+
try:
84+
response = requests.get(f"{self.http_url}/health")
85+
if response.status_code == 200:
86+
return
87+
except:
88+
pass
89+
time.sleep(1)
90+
raise TimeoutError(f"MQTT can't connect to {self.http_url} in {timeout} seconds!")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "tirex-ts"
3-
version = "1.1.1"
3+
version = "1.2.0"
44
authors = [
55
{ name="Andreas Auer", email="andreas.auer@nx-ai.com" },
66
{ name="Martin Loretz", email="martin.loretz@nx-ai.com" },

0 commit comments

Comments
 (0)