Ejemplo n.º 1
0
func newRelationUnitsWatcher(
	relationId int,
	in apiwatcher.RelationUnitsWatcher,
	out chan<- relationUnitsChange,
) *relationUnitsWatcher {
	ruw := &relationUnitsWatcher{relationId: relationId, in: in, out: out}
	go func() {
		defer ruw.tomb.Done()
		// TODO(axw) add Kill() and Wait() to watchers?
		//
		// At the moment we have to rely on the watcher's
		// channel being closed inside loop() to react
		// to it being killed/stopped.
		ruw.tomb.Kill(ruw.loop())
		ruw.tomb.Kill(in.Stop())
	}()
	return ruw
}
Ejemplo n.º 2
0
// watchRelationUnits starts watching the relation units for the given
// relation, waits for its first event, and records the information in
// the current snapshot.
func (w *RemoteStateWatcher) watchRelationUnits(
	rel Relation, relationTag names.RelationTag, in apiwatcher.RelationUnitsWatcher,
) error {
	relationSnapshot := RelationSnapshot{
		Life:    rel.Life(),
		Members: make(map[string]int64),
	}
	select {
	case <-w.tomb.Dying():
		return tomb.ErrDying
	case change, ok := <-in.Changes():
		if !ok {
			return watcher.EnsureErr(in)
		}
		for unit, settings := range change.Changed {
			relationSnapshot.Members[unit] = settings.Version
		}
	}
	w.current.Relations[rel.Id()] = relationSnapshot
	w.relations[relationTag] = newRelationUnitsWatcher(
		rel.Id(), in, w.relationUnitsChanges,
	)
	return nil
}