fix: add check to wait for neo4j

This commit is contained in:
Harshith Mullapudi 2025-10-28 23:44:06 +05:30
parent 6c37b41ca4
commit c5407be54d

View File

@ -1,6 +1,6 @@
import { logger } from "~/services/logger.service";
import { fetchAndSaveStdioIntegrations } from "~/trigger/utils/mcp";
import { initNeo4jSchemaOnce } from "~/lib/neo4j.server";
import { initNeo4jSchemaOnce, verifyConnectivity } from "~/lib/neo4j.server";
import { env } from "~/env.server";
import { initWorkers, shutdownWorkers } from "~/bullmq/start-workers";
import { trackConfig } from "~/services/telemetry.server";
@ -8,6 +8,31 @@ import { trackConfig } from "~/services/telemetry.server";
// Global flag to ensure startup only runs once per server process
let startupInitialized = false;
/**
* Wait for Neo4j to be ready before initializing schema
*/
async function waitForNeo4j(maxRetries = 30, retryDelay = 2000) {
logger.info("Waiting for Neo4j to be ready...");
for (let i = 0; i < maxRetries; i++) {
try {
const connected = await verifyConnectivity();
if (connected) {
logger.info("✓ Neo4j is ready!");
return true;
}
} catch (error) {
// Connection failed, will retry
}
logger.info(`Neo4j not ready, retrying... (${i + 1}/${maxRetries})`);
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
logger.error("Failed to connect to Neo4j after maximum retries");
throw new Error("Failed to connect to Neo4j after maximum retries");
}
/**
* Initialize all startup services once per server process
* Safe to call multiple times - will only run initialization once
@ -80,6 +105,9 @@ export async function initializeStartupServices() {
try {
logger.info("Starting application initialization...");
// Wait for Neo4j to be ready
await waitForNeo4j();
// Initialize Neo4j schema
await initNeo4jSchemaOnce();
logger.info("Neo4j schema initialization completed");