@@ -36,38 +36,42 @@ const databasePlugin: FastifyPluginAsync = async (fastify) => {
3636 // Don't fail the startup, but log the error
3737 }
3838
39- // Backfill litellm_key_alias for existing API keys
40- try {
41- const { backfillLiteLLMKeyAlias } = await import ( '../lib/database-migrations' ) ;
42- const { LiteLLMService } = await import ( '../services/litellm.service' ) ;
43- const liteLLMService = new LiteLLMService ( fastify ) ;
44- await backfillLiteLLMKeyAlias ( fastify . dbUtils , liteLLMService ) ;
45- } catch ( backfillError ) {
46- fastify . log . error ( backfillError as Error , 'Failed to backfill litellm_key_alias' ) ;
47- // Don't fail the startup, but log the error
48- }
49-
50- // Skip initial model sync in test environment
51- // Tests can explicitly sync models if needed for their scenarios
52- if ( process . env . NODE_ENV !== 'test' ) {
53- // Sync models on startup
39+ // Start LiteLLM-dependent operations in background - don't block server startup
40+ setImmediate ( async ( ) => {
41+ // Background task 1: Backfill litellm_key_alias for existing API keys
5442 try {
55- const { ModelSyncService } = await import ( '../services/model-sync.service' ) ;
56- const modelSyncService = new ModelSyncService ( fastify ) ;
57- const syncResult = await modelSyncService . syncModels ( {
58- forceUpdate : false ,
59- markUnavailable : true ,
60- } ) ;
43+ fastify . log . info ( 'Starting background backfill of litellm_key_alias' ) ;
44+ const { backfillLiteLLMKeyAlias } = await import ( '../lib/database-migrations' ) ;
45+ const { LiteLLMService } = await import ( '../services/litellm.service' ) ;
46+ const liteLLMService = new LiteLLMService ( fastify ) ;
47+ await backfillLiteLLMKeyAlias ( fastify . dbUtils , liteLLMService ) ;
48+ fastify . log . info ( 'Background backfill completed successfully' ) ;
49+ } catch ( backfillError ) {
50+ fastify . log . error ( backfillError as Error , 'Background backfill failed' ) ;
51+ }
52+
53+ // Background task 2: Sync models from LiteLLM
54+ // Skip in test environment
55+ if ( process . env . NODE_ENV !== 'test' ) {
56+ try {
57+ fastify . log . info ( 'Starting background model sync from LiteLLM' ) ;
58+ const { ModelSyncService } = await import ( '../services/model-sync.service' ) ;
59+ const modelSyncService = new ModelSyncService ( fastify ) ;
60+ const syncResult = await modelSyncService . syncModels ( {
61+ forceUpdate : false ,
62+ markUnavailable : true ,
63+ } ) ;
6164
62- fastify . log . info ( {
63- msg : 'Initial model sync completed' ,
64- ...syncResult ,
65- } ) ;
66- } catch ( syncError ) {
67- fastify . log . error ( { error : syncError } , 'Failed to sync models on startup' ) ;
68- // Don't fail startup - continue with cached models
65+ fastify . log . info ( {
66+ msg : 'Background model sync completed' ,
67+ ...syncResult ,
68+ } ) ;
69+ } catch ( syncError ) {
70+ fastify . log . error ( { error : syncError } , 'Background model sync failed' ) ;
71+ // Continue - services will use cached models if available
72+ }
6973 }
70- }
74+ } ) ;
7175
7276 // Initialize LiteLLM integration service for auto-sync
7377 const litellmIntegrationService = new LiteLLMIntegrationService ( fastify ) ;
0 commit comments