Golang viper.OnConfigChange(func(e fsnotify.Event) not working as expected

59 views Asked by At

I am working with golang based micro-services, running in a kubernetes cluster. We use viper to read the values of the config variables. In order to avoid restarting of the micro-service, I was trying to add code in accordance with the golang and viper documentation.

For some reason, I see viper.OnConfigChange(func(e fsnotify.Event) is not getting triggered at all. Can someone please help in pointing out what am I missing ?


func run() {
    // some code irrelevant to discussion
    go configFunc()
}

func configFunc() {
    ctx := context.Background()
    viper.SetConfigType("yaml")
    configPath := viper.GetString("config")
    configDir, _ := filepath.Split(configPath)
    viper.AddConfigPath(configDir)
    viper.AddConfigPath(configPath)
    err := viper.ReadInConfig()
    if err != nil {
        //log error
        return
    }

    printf("Initial config is: %v", viper.AllSettings())
    viper.OnConfigChange(func(e fsnotify.Event) {
        printf( "Obtained event: %v", e.Name)
        printf( "Post parsing config is: %v", viper.AllSettings())
    })

    viper.WatchConfig()

}

Please advice what might be missing.

PS. I have already confirmed that configPath is fine. It is /etc/appName/config.yml On changing config, OnConfigChange is not getting invoked.

Thanks

0

There are 0 answers