From d2341d8076262e78c2f23339e8df9aff7124eea6 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Wed, 19 Jun 2024 22:04:46 +0200 Subject: [PATCH] Lower watcher warnings Signed-off-by: Ettore Di Giacinto --- core/startup/config_file_watcher.go | 2 +- core/startup/startup.go | 32 +++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/startup/config_file_watcher.go b/core/startup/config_file_watcher.go index 6a2bdca13153..fac663f61fa0 100644 --- a/core/startup/config_file_watcher.go +++ b/core/startup/config_file_watcher.go @@ -116,7 +116,7 @@ func (c *configFileHandler) Watch() error { // Add a path. err = c.watcher.Add(c.appConfig.DynamicConfigsDir) if err != nil { - return fmt.Errorf("unable to establish watch on the LocalAI Configuration Directory: %+v", err) + return fmt.Errorf("unable to create a watcher on the configuration directory: %+v", err) } return nil diff --git a/core/startup/startup.go b/core/startup/startup.go index 0a135a62ec72..74f55cb8854a 100644 --- a/core/startup/startup.go +++ b/core/startup/startup.go @@ -142,17 +142,37 @@ func Startup(opts ...config.AppOption) (*config.BackendConfigLoader, *model.Mode } // Watch the configuration directory - // If the directory does not exist, we don't watch it - configHandler := newConfigFileHandler(options) - err = configHandler.Watch() - if err != nil { - log.Error().Err(err).Msg("error establishing configuration directory watcher") - } + startWatcher(options) log.Info().Msg("core/startup process completed!") return cl, ml, options, nil } +func startWatcher(options *config.ApplicationConfig) { + if options.DynamicConfigsDir == "" { + // No need to start the watcher if the directory is not set + return + } + + if _, err := os.Stat(options.DynamicConfigsDir); err != nil { + if os.IsNotExist(err) { + // We try to create the directory if it does not exist and was specified + if err := os.MkdirAll(options.DynamicConfigsDir, 0700); err != nil { + log.Error().Err(err).Msg("failed creating DynamicConfigsDir") + } + } else { + // something else happened, we log the error and don't start the watcher + log.Error().Err(err).Msg("failed to read DynamicConfigsDir, watcher will not be started") + return + } + } + + configHandler := newConfigFileHandler(options) + if err := configHandler.Watch(); err != nil { + log.Error().Err(err).Msg("failed creating watcher") + } +} + // In Lieu of a proper DI framework, this function wires up the Application manually. // This is in core/startup rather than core/state.go to keep package references clean! func createApplication(appConfig *config.ApplicationConfig) *core.Application {