~rogpeppe/juju-core/438-local-instance-Addresses

« back to all changes in this revision

Viewing changes to worker/addressupdater/worker.go

[r=rogpeppe] worker/addressupdater: wire up

It could do with some more comprehensive
testing, but it might be OK to land anyway.

https://codereview.appspot.com/14306043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package addressupdater
 
5
 
 
6
import (
 
7
        "launchpad.net/tomb"
 
8
 
 
9
        "launchpad.net/juju-core/instance"
 
10
        "launchpad.net/juju-core/state"
 
11
        "launchpad.net/juju-core/worker"
 
12
)
 
13
 
 
14
type updaterWorker struct {
 
15
        st   *state.State
 
16
        tomb tomb.Tomb
 
17
 
 
18
        observer *environObserver
 
19
}
 
20
 
 
21
// NewWorker returns a worker that keeps track of
 
22
// the machines in the state and polls their instance
 
23
// addresses periodically to keep them up to date.
 
24
func NewWorker(st *state.State) worker.Worker {
 
25
        u := &updaterWorker{
 
26
                st: st,
 
27
        }
 
28
        // wait for environment
 
29
        go func() {
 
30
                defer u.tomb.Done()
 
31
                u.tomb.Kill(u.loop())
 
32
        }()
 
33
        return u
 
34
}
 
35
 
 
36
func (u *updaterWorker) Kill() {
 
37
        u.tomb.Kill(nil)
 
38
}
 
39
 
 
40
func (u *updaterWorker) Wait() error {
 
41
        return u.tomb.Wait()
 
42
}
 
43
 
 
44
func (u *updaterWorker) loop() (err error) {
 
45
        u.observer, err = newEnvironObserver(u.st, u.tomb.Dying())
 
46
        if err != nil {
 
47
                return err
 
48
        }
 
49
        defer func() {
 
50
                obsErr := worker.Stop(u.observer)
 
51
                if err == nil {
 
52
                        err = obsErr
 
53
                }
 
54
        }()
 
55
        return watchMachinesLoop(u, u.st.WatchEnvironMachines())
 
56
}
 
57
 
 
58
func (u *updaterWorker) newMachineContext() machineContext {
 
59
        return u
 
60
}
 
61
 
 
62
func (u *updaterWorker) getMachine(id string) (machine, error) {
 
63
        return u.st.Machine(id)
 
64
}
 
65
 
 
66
func (u *updaterWorker) dying() <-chan struct{} {
 
67
        return u.tomb.Dying()
 
68
}
 
69
 
 
70
func (u *updaterWorker) killAll(err error) {
 
71
        u.tomb.Kill(err)
 
72
}
 
73
 
 
74
func (u *updaterWorker) addresses(id instance.Id) ([]instance.Address, error) {
 
75
        env := u.observer.Environ()
 
76
        insts, err := env.Instances([]instance.Id{id})
 
77
        if err != nil {
 
78
                return nil, err
 
79
        }
 
80
        return insts[0].Addresses()
 
81
}