From 0b71dbbbdbbd46772f34e9e6465971ef56e53f6c Mon Sep 17 00:00:00 2001 From: "courtmanr@gmail.com" Date: Tue, 29 Apr 2025 18:18:02 +0100 Subject: [PATCH] feat: Await initial discovery cycle before server start --- server/index.js | 68 ++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/server/index.js b/server/index.js index 9f99a8faf..f8f147831 100644 --- a/server/index.js +++ b/server/index.js @@ -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(); \ No newline at end of file +// --- END PBS Data Fetching Functions --- \ No newline at end of file