feat: Await initial discovery cycle before server start

This commit is contained in:
courtmanr@gmail.com
2025-04-29 18:18:02 +01:00
parent 6b6de575bc
commit 0b71dbbbdb
+33 -35
View File
@@ -1190,11 +1190,38 @@ function scheduleNextMetric() {
metricTimeoutId = setTimeout(runMetricCycle, METRIC_UPDATE_INTERVAL);
}
// Start the initial cycles
console.log('Starting initial data fetch cycles...');
runDiscoveryCycle(); // Run discovery first
// Metrics will be triggered after discovery or by its own timer if clients connect later
scheduleNextMetric(); // Start scheduling metrics right away
// Start the server
async function startServer() {
await initializeAllPbsClients();
await runDiscoveryCycle(); // << ADDED
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
scheduleNextMetric(); // << ADDED
// Setup hot reload in development mode
if (process.env.NODE_ENV === 'development' && chokidar) {
const publicPath = path.join(__dirname, '../src/public');
console.log(`Watching for changes in ${publicPath}`);
const watcher = chokidar.watch(publicPath, {
ignored: /(^|[\\\/])\./, // ignore dotfiles
persistent: true,
ignoreInitial: true // Don't trigger on initial scan
});
watcher.on('change', (filePath) => {
// console.log(`File changed: ${filePath}. Triggering hot reload.`);
io.emit('hotReload'); // Notify clients to reload
});
watcher.on('error', error => console.error(`Watcher error: ${error}`));
}
});
}
startServer();
// --- PBS Data Fetching Functions ---
@@ -1434,33 +1461,4 @@ async function fetchPbsDatastoreData(pbsClient) {
return datastores;
}
// --- END PBS Data Fetching Functions ---
// Start the server
async function startServer() {
await initializeAllPbsClients();
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
// Setup hot reload in development mode
if (process.env.NODE_ENV === 'development' && chokidar) {
const publicPath = path.join(__dirname, '../src/public');
console.log(`Watching for changes in ${publicPath}`);
const watcher = chokidar.watch(publicPath, {
ignored: /(^|[\\\/])\./, // ignore dotfiles
persistent: true,
ignoreInitial: true // Don't trigger on initial scan
});
watcher.on('change', (filePath) => {
// console.log(`File changed: ${filePath}. Triggering hot reload.`);
io.emit('hotReload'); // Notify clients to reload
});
watcher.on('error', error => console.error(`Watcher error: ${error}`));
}
});
}
startServer();
// --- END PBS Data Fetching Functions ---