Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit 334c6d3

Browse files
author
danecreekphotography
authored
Add ongoing health message via MQTT (#424)
* Add repeating MQTT health message Fixes #423 * Update changelog * Add missing await statement
1 parent e5a5b3c commit 334c6d3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
- System now sends an MQTT message every 60 seconds to report its status as online. Resolves [issue 423](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/423).
6+
37
## 5.7.0
48

59
- Add support for `customEndpoint` on a trigger to support custom Deepstack models. Resolves [issue 416](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/416).

src/main.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ import IConfiguration from "./types/IConfiguration";
2222

2323
import npmPackageInfo from "../package.json";
2424

25+
// Health message is sent via MQTT every 60 seconds
26+
const healthWaitTime = 60 * 1000;
2527
// If startup fails restart is reattempted 5 times every 30 seconds.
2628
const restartAttemptWaitTime = 30 * 1000;
2729
const maxRestartAttempts = 5;
2830

2931
// The list of settings file watchers, used to stop them on hot reloading of settings.
3032
const watchers: chokidar.FSWatcher[] = [];
3133

34+
let healthTimer: NodeJS.Timeout;
3235
let restartAttemptCount = 0;
3336
let restartTimer: NodeJS.Timeout;
3437
let settingsConfiguration: IConfiguration;
@@ -119,7 +122,7 @@ async function startup(): Promise<void> {
119122
TriggerManager.startWatching();
120123

121124
// Notify it's up and running
122-
await MqttManager.publishServerState("online");
125+
await sendHealthMessage();
123126

124127
// Start watching for config file changes
125128
startWatching();
@@ -140,6 +143,7 @@ async function startup(): Promise<void> {
140143
);
141144

142145
// Notify it's not up and running
146+
clearTimeout(healthTimer);
143147
await MqttManager.publishServerState("offline", e.message);
144148

145149
// Shutdown the web server plus other things that may have spun up successfully.
@@ -165,6 +169,19 @@ async function startup(): Promise<void> {
165169
}
166170
}
167171

172+
/**
173+
* Sends a health message via MQTT every n seconds to indicate the server is online.
174+
*/
175+
async function sendHealthMessage(): Promise<void> {
176+
if (!MqttManager.isEnabled) {
177+
return;
178+
}
179+
180+
await MqttManager.publishServerState("online");
181+
182+
healthTimer = setTimeout(sendHealthMessage, healthWaitTime);
183+
}
184+
168185
/**
169186
* Shuts down all registered file system watchers and the web server
170187
*/

0 commit comments

Comments
 (0)