1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"github.com/juju/errors"
10
"github.com/juju/utils/clock"
11
"gopkg.in/juju/names.v2"
13
"github.com/juju/juju/api/instancepoller"
14
"github.com/juju/juju/instance"
15
"github.com/juju/juju/worker"
16
"github.com/juju/juju/worker/catacomb"
22
Facade *instancepoller.API
23
Environ InstanceGetter
26
func (config Config) Validate() error {
27
if config.Clock == nil {
28
return errors.NotValidf("nil clock.Clock")
30
if config.Delay == 0 {
31
return errors.NotValidf("zero Delay")
33
if config.Facade == nil {
34
return errors.NotValidf("nil Facade")
36
if config.Environ == nil {
37
return errors.NotValidf("nil Environ")
42
type updaterWorker struct {
44
aggregator *aggregator
45
catacomb catacomb.Catacomb
48
// NewWorker returns a worker that keeps track of
49
// the machines in the state and polls their instance
50
// addresses and status periodically to keep them up to date.
51
func NewWorker(config Config) (worker.Worker, error) {
52
if err := config.Validate(); err != nil {
53
return nil, errors.Trace(err)
58
err := catacomb.Invoke(catacomb.Plan{
63
return nil, errors.Trace(err)
68
// Kill is part of the worker.Worker interface.
69
func (u *updaterWorker) Kill() {
73
// Wait is part of the worker.Worker interface.
74
func (u *updaterWorker) Wait() error {
75
return u.catacomb.Wait()
78
func (u *updaterWorker) loop() (err error) {
79
u.aggregator, err = newAggregator(
81
Clock: u.config.Clock,
82
Delay: u.config.Delay,
83
Environ: u.config.Environ,
87
return errors.Trace(err)
89
if err := u.catacomb.Add(u.aggregator); err != nil {
90
return errors.Trace(err)
92
watcher, err := u.config.Facade.WatchModelMachines()
94
return errors.Trace(err)
96
if err := u.catacomb.Add(watcher); err != nil {
97
return errors.Trace(err)
99
return watchMachinesLoop(u, watcher)
102
// newMachineContext is part of the updaterContext interface.
103
func (u *updaterWorker) newMachineContext() machineContext {
107
// getMachine is part of the machineContext interface.
108
func (u *updaterWorker) getMachine(tag names.MachineTag) (machine, error) {
109
return u.config.Facade.Machine(tag)
112
// instanceInfo is part of the machineContext interface.
113
func (u *updaterWorker) instanceInfo(id instance.Id) (instanceInfo, error) {
114
return u.aggregator.instanceInfo(id)
117
// kill is part of the lifetimeContext interface.
118
func (u *updaterWorker) kill(err error) {
122
// dying is part of the lifetimeContext interface.
123
func (u *updaterWorker) dying() <-chan struct{} {
124
return u.catacomb.Dying()
127
// errDying is part of the lifetimeContext interface.
128
func (u *updaterWorker) errDying() error {
129
return u.catacomb.ErrDying()