From 8a3b3af614545b53afb5d8ff07e4492515112eb4 Mon Sep 17 00:00:00 2001 From: Rui Fu Date: Mon, 22 Jul 2019 00:12:32 +0800 Subject: [PATCH] [go function] fix: go function should parse conf content first (#4746) ### Motivation `pulsar-function-go/conf` package apply `instance-conf-path` with default value `HOME_PATH+github.com/apache/pulsar/pulsar-function-go/conf/conf.yaml`, once function deployed, the running node may not have the yaml conf file exist, then go function will panic with `not found conf file` error. This PR changed the logic of config parsing, parse `confContent` first, then parse `confFilePath` if `confContent` empty. --- pulsar-function-go/conf/conf.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pulsar-function-go/conf/conf.go b/pulsar-function-go/conf/conf.go index 54a32d3651251..9254e57684ca6 100644 --- a/pulsar-function-go/conf/conf.go +++ b/pulsar-function-go/conf/conf.go @@ -87,15 +87,24 @@ func (c *Conf) GetConf() *Conf { flag.Usage() } + if confContent == "" && confFilePath == "" { + log.Errorf("no yaml file or conf content provided") + return nil + } + if confFilePath != "" { yamlFile, err := ioutil.ReadFile(confFilePath) - if err != nil { - log.Errorf("not found conf file, err:%s", err.Error()) + if err == nil { + err = yaml.Unmarshal(yamlFile, c) + if err != nil { + log.Errorf("unmarshal yaml file error:%s", err.Error()) + return nil + } + } else if err != nil && os.IsNotExist(err) && confContent == "" { + log.Errorf("conf file not found, no config content provided, err:%s", err.Error()) return nil - } - err = yaml.Unmarshal(yamlFile, c) - if err != nil { - log.Errorf("unmarshal yaml file error:%s", err.Error()) + } else if err != nil && !os.IsNotExist(err) { + log.Errorf("load conf file failed, err:%s", err.Error()) return nil } } @@ -107,6 +116,7 @@ func (c *Conf) GetConf() *Conf { return nil } } + return c }