github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/config/agent.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package config 18 19 import ( 20 "sync" 21 "time" 22 23 "github.com/sirupsen/logrus" 24 ) 25 26 // Agent watches a path and automatically loads the config stored 27 // therein. 28 type Agent struct { 29 sync.Mutex 30 c *Config 31 } 32 33 // Start will begin polling the config file at the path. If the first load 34 // fails, Start with return the error and abort. Future load failures will log 35 // the failure message but continue attempting to load. 36 func (ca *Agent) Start(path string) error { 37 c, err := Load(path) 38 if err != nil { 39 return err 40 } 41 ca.c = c 42 go func() { 43 for range time.Tick(1 * time.Minute) { 44 if c, err := Load(path); err != nil { 45 logrus.WithField("path", path).WithError(err).Error("Error loading config.") 46 } else { 47 ca.Lock() 48 ca.c = c 49 ca.Unlock() 50 } 51 } 52 }() 53 return nil 54 } 55 56 // Config returns the latest config. Do not modify the config. 57 func (ca *Agent) Config() *Config { 58 ca.Lock() 59 defer ca.Unlock() 60 return ca.c 61 } 62 63 // Set sets the config. Useful for testing. 64 func (ca *Agent) Set(c *Config) { 65 ca.Lock() 66 defer ca.Unlock() 67 ca.c = c 68 }