github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/environ/wait.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package environ 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 10 "github.com/juju/juju/environs" 11 "github.com/juju/juju/watcher" 12 ) 13 14 // TODO(fwereade) remove WaitForEnviron, use a manifold-managed Tracker to share 15 // a single environs.Environ among firewaller, instancepoller, provisioner. 16 17 var logger = loggo.GetLogger("juju.worker.environ") 18 19 // ErrWaitAborted is returned from WaitForEnviron when the wait is terminated by 20 // closing the abort chan. 21 var ErrWaitAborted = errors.New("environ wait aborted") 22 23 // WaitForEnviron waits for an valid environment to arrive from the given 24 // watcher. It terminates with ErrWaitAborted if it receives a value on abort. 25 // 26 // In practice, it shouldn't wait at all: juju *should* never deliver invalid 27 // environ configs. Regardless, it should be considered deprecated; clients 28 // should prefer to access an Environ via a shared Tracker. 29 // 30 // It never takes responsibility for the supplied watcher; the client remains 31 // responsible for detecting and handling any watcher errors that may occur, 32 // whether this func succeeds or fails. 33 func WaitForEnviron( 34 w watcher.NotifyWatcher, 35 getter ConfigGetter, 36 newEnviron NewEnvironFunc, 37 abort <-chan struct{}, 38 ) (environs.Environ, error) { 39 for { 40 select { 41 case <-abort: 42 return nil, ErrWaitAborted 43 case _, ok := <-w.Changes(): 44 if !ok { 45 return nil, errors.New("environ config watch closed") 46 } 47 config, err := getter.ModelConfig() 48 if err != nil { 49 return nil, errors.Annotate(err, "cannot read environ config") 50 } 51 environ, err := newEnviron(config) 52 if err == nil { 53 return environ, nil 54 } 55 logger.Errorf("loaded invalid environment configuration: %v", err) 56 } 57 } 58 }