1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
11
"launchpad.net/juju-core/environs"
12
"launchpad.net/juju-core/state"
13
"launchpad.net/juju-core/state/watcher"
14
"launchpad.net/juju-core/worker"
17
// TODO(rog) 2013-10-02
18
// Put this somewhere generally available and
19
// refactor other workers to use it.
21
// environObserver watches the current environment configuration
22
// and makes it available. It discards invalid environment
24
type environObserver struct {
26
environWatcher state.NotifyWatcher
29
environ environs.Environ
32
// newEnvironObserver waits for the state to have a valid environment
33
// configuration and returns a new environment observer. While waiting
34
// for the first environment configuration, it will return with
35
// tomb.ErrDying if it receives a value on dying.
36
func newEnvironObserver(st *state.State, dying <-chan struct{}) (*environObserver, error) {
37
environWatcher := st.WatchForEnvironConfigChanges()
38
environ, err := worker.WaitForEnviron(environWatcher, st, dying)
42
obs := &environObserver{
45
environWatcher: environWatcher,
49
defer watcher.Stop(environWatcher, &obs.tomb)
50
obs.tomb.Kill(obs.loop())
55
func (obs *environObserver) loop() error {
58
case <-obs.tomb.Dying():
60
case _, ok := <-obs.environWatcher.Changes():
62
return watcher.MustErr(obs.environWatcher)
65
config, err := obs.st.EnvironConfig()
67
logger.Warningf("error reading environment config: %v", err)
70
environ, err := environs.New(config)
72
logger.Warningf("error creating Environ: %v", err)
81
// Environ returns the most recent valid Environ.
82
func (obs *environObserver) Environ() environs.Environ {
88
func (obs *environObserver) Kill() {
92
func (obs *environObserver) Wait() error {
93
return obs.tomb.Wait()