@@ -70,11 +70,18 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
7070 }
7171 xlog .Info ("Connected to NATS" , "url" , cfg .Distributed .NatsURL )
7272
73+ // Ensure NATS is closed if any subsequent initialization step fails.
74+ success := false
75+ defer func () {
76+ if ! success {
77+ natsClient .Close ()
78+ }
79+ }()
80+
7381 // Initialize object storage
7482 var store storage.ObjectStore
7583 if cfg .Distributed .StorageURL != "" {
7684 if cfg .Distributed .StorageBucket == "" {
77- natsClient .Close ()
7885 return nil , fmt .Errorf ("distributed storage bucket must be set when storage URL is configured" )
7986 }
8087 s3Store , err := storage .NewS3Store (storage.S3Config {
@@ -86,7 +93,6 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
8693 ForcePathStyle : true , // required for MinIO
8794 })
8895 if err != nil {
89- natsClient .Close ()
9096 return nil , fmt .Errorf ("initializing S3 storage: %w" , err )
9197 }
9298 xlog .Info ("Object storage initialized (S3)" , "endpoint" , cfg .Distributed .StorageURL , "bucket" , cfg .Distributed .StorageBucket )
@@ -95,7 +101,6 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
95101 // Fallback to filesystem storage in distributed mode (useful for single-node testing)
96102 fsStore , err := storage .NewFilesystemStore (cfg .DataPath + "/objectstore" )
97103 if err != nil {
98- natsClient .Close ()
99104 return nil , fmt .Errorf ("initializing filesystem storage: %w" , err )
100105 }
101106 xlog .Info ("Object storage initialized (filesystem fallback)" , "path" , cfg .DataPath + "/objectstore" )
@@ -104,13 +109,11 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
104109
105110 // Initialize node registry (requires the auth DB which is PostgreSQL)
106111 if authDB == nil {
107- natsClient .Close ()
108112 return nil , fmt .Errorf ("distributed mode requires auth database to be initialized first" )
109113 }
110114
111115 registry , err := nodes .NewNodeRegistry (authDB )
112116 if err != nil {
113- natsClient .Close ()
114117 return nil , fmt .Errorf ("initializing node registry: %w" , err )
115118 }
116119 xlog .Info ("Node registry initialized" )
@@ -122,12 +125,14 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
122125 if galleriesJSON , err := json .Marshal (cfg .BackendGalleries ); err == nil {
123126 router .SetGalleriesJSON (string (galleriesJSON ))
124127 }
125- healthMon := nodes .NewHealthMonitor (registry , authDB )
128+ healthMon := nodes .NewHealthMonitor (registry , authDB ,
129+ cfg .Distributed .HealthCheckIntervalOrDefault (),
130+ cfg .Distributed .StaleNodeThresholdOrDefault (),
131+ )
126132
127133 // Initialize job store
128134 jobStore , err := jobs .NewJobStore (authDB )
129135 if err != nil {
130- natsClient .Close ()
131136 return nil , fmt .Errorf ("initializing job store: %w" , err )
132137 }
133138 xlog .Info ("Distributed job store initialized" )
@@ -138,7 +143,6 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
138143 // Initialize agent store
139144 agentStore , err := agents .NewAgentStore (authDB )
140145 if err != nil {
141- natsClient .Close ()
142146 return nil , fmt .Errorf ("initializing agent store: %w" , err )
143147 }
144148 xlog .Info ("Distributed agent store initialized" )
@@ -149,15 +153,13 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
149153 // Initialize Phase 4 stores (MCP, Gallery, FineTune, Skills)
150154 distStores , err := distributed .InitStores (authDB )
151155 if err != nil {
152- natsClient .Close ()
153156 return nil , fmt .Errorf ("initializing distributed stores: %w" , err )
154157 }
155158
156159 // Initialize file manager with local cache
157160 cacheDir := cfg .DataPath + "/cache"
158161 fileMgr , err := storage .NewFileManager (store , cacheDir )
159162 if err != nil {
160- natsClient .Close ()
161163 return nil , fmt .Errorf ("initializing file manager: %w" , err )
162164 }
163165 xlog .Info ("File manager initialized" , "cacheDir" , cacheDir )
@@ -185,6 +187,7 @@ func initDistributed(cfg *config.ApplicationConfig, authDB *gorm.DB) (*distribut
185187 // Create ModelRouterAdapter to wire into ModelLoader
186188 modelAdapter := nodes .NewModelRouterAdapter (router )
187189
190+ success = true
188191 return & distributedServices {
189192 nats : natsClient ,
190193 store : store ,
0 commit comments