Skip to content

Commit

Permalink
file config update
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiaozhi committed Dec 21, 2023
1 parent bfe7b2b commit 0967b4f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
8 changes: 5 additions & 3 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func BuildFromString(config string, logger core.Logger) (*Config, error) {
}

// Call this only once at app init
func (c *Config) InitializeApp(dd bool) error {
func (c *Config) InitializeApp(configFile string, dd bool) error {
c.sh = core.NewScheduler(c.logger)
c.buildSchedulerMiddlewares(c.sh)

var err error
c.configHandler, err = NewFileConfigHandler(c, c.logger)
c.configHandler, err = NewFileConfigHandler(configFile, c, c.logger)
if err != nil {
return err
}
Expand Down Expand Up @@ -121,10 +121,12 @@ func (c *Config) buildSchedulerMiddlewares(sh *core.Scheduler) {
}

func (c *Config) updateExecJobs(newConfig *Config) {
c.Global = newConfig.Global
c.buildSchedulerMiddlewares(c.sh)
// Calculate the delta
for name, j := range c.ExecJobs {
// this prevents deletion of jobs that were added by reading a configuration file
if !j.FromDockerLabel {
if j.FromDockerLabel {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *DaemonCommand) boot() (err error) {
c.Logger.Debugf("Cannot read config file: %q", err)
}

err = config.InitializeApp(c.DisableDocker)
err = config.InitializeApp(c.ConfigFile, c.DisableDocker)
if err != nil {
c.Logger.Criticalf("Can't start the app: %v", err)
}
Expand Down
12 changes: 4 additions & 8 deletions cli/file_config_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ type fileConfigUpdate interface {
fileConfigUpdate(name *Config)
}

func NewFileConfigHandler(notifier fileConfigUpdate, logger core.Logger) (*FileConfigHandler, error) {
logger.Debugf("FileConfigHandler NewFileConfigHandler")

func NewFileConfigHandler(configFile string, notifier fileConfigUpdate, logger core.Logger) (*FileConfigHandler, error) {
c := &FileConfigHandler{}
c.ConfigFile = configFile
c.notifier = notifier
c.logger = logger
go c.watch()
Expand All @@ -31,14 +30,11 @@ func NewFileConfigHandler(notifier fileConfigUpdate, logger core.Logger) (*FileC

func (c *FileConfigHandler) watch() {
cfgHash := c.getCfgHash(c.ConfigFile)
c.logger.Debugf("FileConfigHandler watch")

tick := time.Tick(10000 * time.Millisecond)
for {
select {
case <-tick:
newCfgHash := c.getCfgHash(c.ConfigFile)
c.logger.Debugf("config file hash,old hash:%s,new hash:%s", cfgHash, newCfgHash)
if cfgHash != newCfgHash {
c.logger.Debugf("config file has changed,old hash:%s,new hash:%s", cfgHash, newCfgHash)
config, err := BuildFromFile(c.ConfigFile, c.logger)
Expand All @@ -56,12 +52,12 @@ func (c *FileConfigHandler) getCfgHash(filename string) string {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
c.logger.Errorf(err.Error())
c.logger.Errorf("filename:%s,err:%s", filename, err.Error())
}

hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
c.logger.Errorf(err.Error())
c.logger.Errorf("filename:%s,err:%s", filename, err.Error())
}
sum := fmt.Sprintf("%x", hash.Sum(nil))

Expand Down
6 changes: 3 additions & 3 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ func (c *middlewareContainer) Use(ms ...Middleware) {
}

t := reflect.TypeOf(m).String()
if _, ok := c.m[t]; ok {
continue
}
//if _, ok := c.m[t]; ok {
// continue
//}

c.order = append(c.order, t)
c.m[t] = m
Expand Down

0 comments on commit 0967b4f

Please sign in to comment.