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

Commit 398758d

Browse files
author
danecreekphotography
authored
Print out count of images in /aiinput on startup (#331)
* Print out count of images in /aiinput on startup Fixes #330 * Print out count of images in /aiinput on startup Fixes #330
1 parent d56f18d commit 398758d

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Log messages are printed on startup to help confirm the image folder was mounted correctly with Docker.
6+
Resolves [issue 330](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/330).
57
- Web server now shuts down properly when reloading settings. Resolves [issue 323](https://github.com/danecreekphotography/node-deepstackai-trigger/issues/323).
68
- Startup is now re-attempted if there are any failures during launch. Each re-attempt is 30 seconds
79
apart and five re-attempts will happen before things are assumed to just be completely broken.

src/TriggerManager.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import * as log from "./Log";
77
import * as MQTTManager from "./handlers/mqttManager/MqttManager";
88

99
import ITriggerConfigJson from "./types/ITriggerConfigJson";
10+
import * as fs from "fs";
1011
import MqttHandlerConfig from "./handlers/mqttManager/MqttHandlerConfig";
12+
import path from "path";
1113
import PushbulletConfig from "./handlers/pushbulletManager/PushbulletConfig";
1214
import PushoverConfig from "./handlers/pushoverManager/PushoverConfig";
1315
import Rect from "./Rect";
@@ -252,3 +254,32 @@ export function resetOverallStatistics(): ITriggerStatistics {
252254
MQTTManager.publishStatisticsMessage(triggeredCount, analyzedFilesCount);
253255
return getOverallStatistics();
254256
}
257+
258+
/**
259+
* Checks the watch folder on each trigger to see if there are images in it. If
260+
* not throws a warning.
261+
* @returns True if all the watch locations are valid, false otherwise.
262+
*/
263+
export function verifyTriggerWatchLocations(): boolean {
264+
const invalidWatchLocations = triggers?.filter(trigger => {
265+
const watchFolder = path.dirname(trigger.watchPattern);
266+
267+
let files: string[];
268+
269+
try {
270+
files = fs.readdirSync(watchFolder);
271+
} catch (e) {
272+
log.warn(
273+
"Trigger manager",
274+
`Unable to read contents of watch folder ${watchFolder} for trigger ${trigger.name}. Check and make sure the image folder is mounted properly. ${e}`,
275+
);
276+
return true;
277+
}
278+
279+
log.verbose("Trigger manager", `There are ${files.length} images waiting in ${watchFolder} for ${trigger.name}.`);
280+
return false;
281+
});
282+
283+
// If no invalid watch locations were found then we're good to go and return true
284+
return invalidWatchLocations.length == 0;
285+
}

src/main.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as WebServer from "./WebServer";
2222
import npmPackageInfo from "../package.json";
2323

2424
// If startup fails restart is reattempted 5 times every 30 seconds.
25-
const restartAttemptWaitTime = 5 * 1000;
25+
const restartAttemptWaitTime = 30 * 1000;
2626
const maxRestartAttempts = 5;
2727

2828
// The list of settings file watchers, used to stop them on hot reloading of settings.
@@ -83,6 +83,9 @@ async function startup(): Promise<void> {
8383

8484
// Load the trigger configuration.
8585
triggersFilePath = TriggerManager.loadConfiguration(["/run/secrets/triggers", "/config/triggers.json"]);
86+
if (!TriggerManager.verifyTriggerWatchLocations()) {
87+
throw Error(`Unable to access one or more watch locations.`);
88+
}
8689

8790
// Initialize the other handler managers. MQTT got done earlier
8891
// since it does double-duty and sends overall status messages for the system.
@@ -120,6 +123,9 @@ async function startup(): Promise<void> {
120123
// Notify it's not up and running
121124
await MqttManager.publishServerState("offline", e.message);
122125

126+
// Shutdown the web server plus other things that may have spun up successfully.
127+
await shutdown();
128+
123129
restartAttemptCount++;
124130

125131
// Try starting again in a little bit.
@@ -174,6 +180,11 @@ async function hotLoadTriggers(path: string) {
174180
await TriggerManager.stopWatching();
175181

176182
TriggerManager.loadConfiguration([path]);
183+
if (!TriggerManager.verifyTriggerWatchLocations()) {
184+
log.warn("Main", `Unable to access one or more watch locations.`);
185+
return;
186+
}
187+
177188
TriggerManager.startWatching();
178189
}
179190

0 commit comments

Comments
 (0)