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