Skip to content

Commit

Permalink
Lower watcher warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Ettore Di Giacinto <[email protected]>
  • Loading branch information
mudler committed Jun 21, 2024
1 parent 3ca5e59 commit d2341d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/startup/config_file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 26 additions & 6 deletions core/startup/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d2341d8

Please sign in to comment.