wait for DB init even to emit before starting LLM services

This commit is contained in:
perf3ct 2025-03-16 18:21:43 +00:00
parent 697d348286
commit c315b32c99
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
5 changed files with 59 additions and 7 deletions

View File

@ -15,6 +15,8 @@ import error_handlers from "./routes/error_handlers.js";
import { startScheduledCleanup } from "./services/erase.js"; import { startScheduledCleanup } from "./services/erase.js";
import sql_init from "./services/sql_init.js"; import sql_init from "./services/sql_init.js";
import { t } from "i18next"; import { t } from "i18next";
import eventService from "./services/events.js";
import log from "./services/log.js";
await import("./services/handlers.js"); await import("./services/handlers.js");
await import("./becca/becca_loader.js"); await import("./becca/becca_loader.js");
@ -26,13 +28,41 @@ const scriptDir = dirname(fileURLToPath(import.meta.url));
// Initialize DB // Initialize DB
sql_init.initializeDb(); sql_init.initializeDb();
// Initialize embedding providers // Listen for database initialization event
const { initializeEmbeddings } = await import("./services/llm/embeddings/init.js"); eventService.subscribe(eventService.DB_INITIALIZED, async () => {
await initializeEmbeddings(); try {
log.info("Database initialized, setting up LLM features");
// Initialize the index service for LLM functionality // Initialize embedding providers
const { default: indexService } = await import("./services/llm/index_service.js"); const { initializeEmbeddings } = await import("./services/llm/embeddings/init.js");
await indexService.initialize().catch(e => console.error("Failed to initialize index service:", e)); await initializeEmbeddings();
// Initialize the index service for LLM functionality
const { default: indexService } = await import("./services/llm/index_service.js");
await indexService.initialize().catch(e => console.error("Failed to initialize index service:", e));
log.info("LLM features initialized successfully");
} catch (error) {
console.error("Error initializing LLM features:", error);
}
});
// Initialize LLM features only if database is already initialized
if (sql_init.isDbInitialized()) {
try {
// Initialize embedding providers
const { initializeEmbeddings } = await import("./services/llm/embeddings/init.js");
await initializeEmbeddings();
// Initialize the index service for LLM functionality
const { default: indexService } = await import("./services/llm/index_service.js");
await indexService.initialize().catch(e => console.error("Failed to initialize index service:", e));
} catch (error) {
console.error("Error initializing LLM features:", error);
}
} else {
console.log("Database not initialized yet. LLM features will be initialized after setup.");
}
// view engine setup // view engine setup
app.set("views", path.join(scriptDir, "views")); app.set("views", path.join(scriptDir, "views"));

View File

@ -10,6 +10,7 @@ const ENTITY_CHANGE_SYNCED = "ENTITY_CHANGE_SYNCED";
const ENTITY_DELETE_SYNCED = "ENTITY_DELETE_SYNCED"; const ENTITY_DELETE_SYNCED = "ENTITY_DELETE_SYNCED";
const CHILD_NOTE_CREATED = "CHILD_NOTE_CREATED"; const CHILD_NOTE_CREATED = "CHILD_NOTE_CREATED";
const NOTE_CONTENT_CHANGE = "NOTE_CONTENT_CHANGED"; const NOTE_CONTENT_CHANGE = "NOTE_CONTENT_CHANGED";
const DB_INITIALIZED = "DB_INITIALIZED";
type EventType = string | string[]; type EventType = string | string[];
type EventListener = (data: any) => void; type EventListener = (data: any) => void;
@ -72,5 +73,6 @@ export default {
ENTITY_CHANGE_SYNCED, ENTITY_CHANGE_SYNCED,
ENTITY_DELETE_SYNCED, ENTITY_DELETE_SYNCED,
CHILD_NOTE_CREATED, CHILD_NOTE_CREATED,
NOTE_CONTENT_CHANGE NOTE_CONTENT_CHANGE,
DB_INITIALIZED
}; };

View File

@ -2,6 +2,7 @@ import log from "../../log.js";
import options from "../../options.js"; import options from "../../options.js";
import { initEmbeddings } from "./index.js"; import { initEmbeddings } from "./index.js";
import providerManager from "./providers.js"; import providerManager from "./providers.js";
import sqlInit from "../../sql_init.js";
/** /**
* Initialize the embedding system * Initialize the embedding system
@ -10,6 +11,12 @@ export async function initializeEmbeddings() {
try { try {
log.info("Initializing embedding system..."); log.info("Initializing embedding system...");
// Check if the database is initialized before proceeding
if (!sqlInit.isDbInitialized()) {
log.info("Skipping embedding system initialization as database is not initialized yet.");
return;
}
// Initialize default embedding providers // Initialize default embedding providers
await providerManager.initializeDefaultProviders(); await providerManager.initializeDefaultProviders();

View File

@ -19,6 +19,7 @@ import eventService from "../events.js";
import type { NoteEmbeddingContext } from "./embeddings/embeddings_interface.js"; import type { NoteEmbeddingContext } from "./embeddings/embeddings_interface.js";
import type { OptionDefinitions } from "../options_interface.js"; import type { OptionDefinitions } from "../options_interface.js";
import sql from "../sql.js"; import sql from "../sql.js";
import sqlInit from "../sql_init.js";
class IndexService { class IndexService {
private initialized = false; private initialized = false;
@ -45,6 +46,12 @@ class IndexService {
if (this.initialized) return; if (this.initialized) return;
try { try {
// Check if database is initialized before proceeding
if (!sqlInit.isDbInitialized()) {
log.info("Index service: Database not initialized yet, skipping initialization");
return;
}
const aiEnabled = await options.getOptionBool('aiEnabled'); const aiEnabled = await options.getOptionBool('aiEnabled');
if (!aiEnabled) { if (!aiEnabled) {
log.info("Index service: AI features disabled, skipping initialization"); log.info("Index service: AI features disabled, skipping initialization");

View File

@ -17,6 +17,7 @@ import zipImportService from "./import/zip.js";
import becca_loader from "../becca/becca_loader.js"; import becca_loader from "../becca/becca_loader.js";
import password from "./encryption/password.js"; import password from "./encryption/password.js";
import backup from "./backup.js"; import backup from "./backup.js";
import eventService from "./events.js";
const dbReady = deferred<void>(); const dbReady = deferred<void>();
@ -153,6 +154,11 @@ function setDbAsInitialized() {
optionService.setOption("initialized", "true"); optionService.setOption("initialized", "true");
initDbConnection(); initDbConnection();
// Emit an event to notify that the database is now initialized
eventService.emit(eventService.DB_INITIALIZED);
log.info("Database initialization completed, emitted DB_INITIALIZED event");
} }
} }