Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(868)

Side by Side Diff: state/watcher.go

Issue 10180043: state: Unit.WatchConfigSettings
Patch Set: Created 10 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « state/unit_test.go ('k') | worker/uniter/filter.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012, 2013 Canonical Ltd. 1 // Copyright 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package state 4 package state
5 5
6 import ( 6 import (
7 "fmt" 7 "fmt"
8 "labix.org/v2/mgo" 8 "labix.org/v2/mgo"
9 "launchpad.net/juju-core/environs/config" 9 "launchpad.net/juju-core/environs/config"
10 "launchpad.net/juju-core/errors" 10 "launchpad.net/juju-core/errors"
(...skipping 992 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 return err 1003 return err
1004 } 1004 }
1005 out = w.out 1005 out = w.out
1006 case out <- settings: 1006 case out <- settings:
1007 out = nil 1007 out = nil
1008 } 1008 }
1009 } 1009 }
1010 return nil 1010 return nil
1011 } 1011 }
1012 1012
1013 type ConfigWatcher struct {
1014 *settingsWatcher
1015 }
1016
1017 // WatchServiceConfig returns a watcher for observing changes to
1018 // unit's service configuration.
1019 func (u *Unit) WatchServiceConfig() (*ConfigWatcher, error) {
1020 if u.doc.CharmURL == nil {
1021 return nil, fmt.Errorf("unit charm not set")
1022 }
1023 skey := serviceSettingsKey(u.doc.Service, u.doc.CharmURL)
1024 return &ConfigWatcher{newSettingsWatcher(u.st, skey)}, nil
1025 }
1026
1027 // EntityWatcher observes changes to a state entity. 1013 // EntityWatcher observes changes to a state entity.
1028 type EntityWatcher struct { 1014 type EntityWatcher struct {
1029 commonWatcher 1015 commonWatcher
1030 out chan struct{} 1016 out chan struct{}
1031 } 1017 }
1032 1018
1019 // Watch return a watcher for observing changes to a machine.
1020 func (m *Machine) Watch() *EntityWatcher {
1021 return newEntityWatcher(m.st, m.st.machines.Name, m.doc.Id, m.doc.TxnRev no)
1022 }
1023
1033 // Watch return a watcher for observing changes to a service. 1024 // Watch return a watcher for observing changes to a service.
1034 func (s *Service) Watch() *EntityWatcher { 1025 func (s *Service) Watch() *EntityWatcher {
1035 return newEntityWatcher(s.st, s.st.services.Name, s.doc.Name, s.doc.TxnR evno) 1026 return newEntityWatcher(s.st, s.st.services.Name, s.doc.Name, s.doc.TxnR evno)
1036 } 1027 }
1037 1028
1038 // Watch return a watcher for observing changes to a unit. 1029 // Watch return a watcher for observing changes to a unit.
1039 func (u *Unit) Watch() *EntityWatcher { 1030 func (u *Unit) Watch() *EntityWatcher {
1040 return newEntityWatcher(u.st, u.st.units.Name, u.doc.Name, u.doc.TxnRevn o) 1031 return newEntityWatcher(u.st, u.st.units.Name, u.doc.Name, u.doc.TxnRevn o)
1041 } 1032 }
1042 1033
1043 // Watch return a watcher for observing changes to a machine. 1034 // WatchConfigSettings returns a watcher for observing changes to the
1044 func (m *Machine) Watch() *EntityWatcher { 1035 // unit's service configuration settings. The unit must have a charm URL
1045 » return newEntityWatcher(m.st, m.st.machines.Name, m.doc.Id, m.doc.TxnRev no) 1036 // set before this method is called, and the returned watcher will be
1037 // valid only while the unit's charm URL is not changed.
1038 // TODO(fwereade): this could be much smarter; if it were, uniter.Filter
rog 2013/06/11 12:13:27 perhaps say a tiny bit about how this could be sma
fwereade 2013/06/12 00:30:20 The restrictions are described in the preceding pa
1039 // could be somewhat simpler.
1040 func (u *Unit) WatchConfigSettings() (*EntityWatcher, error) {
1041 » if u.doc.CharmURL == nil {
1042 » » return nil, fmt.Errorf("unit charm not set")
1043 » }
1044 » settingsKey := serviceSettingsKey(u.doc.Service, u.doc.CharmURL)
1045 » _, txnRevno, err := readSettingsDoc(u.st, settingsKey)
1046 » if err != nil {
1047 » » return nil, err
1048 » }
1049 » return newEntityWatcher(u.st, u.st.settings.Name, settingsKey, txnRevno) , nil
1046 } 1050 }
1047 1051
1048 func newEntityWatcher(st *State, coll string, key interface{}, revno int64) *Ent ityWatcher { 1052 func newEntityWatcher(st *State, coll string, key interface{}, revno int64) *Ent ityWatcher {
1049 w := &EntityWatcher{ 1053 w := &EntityWatcher{
1050 commonWatcher: commonWatcher{st: st}, 1054 commonWatcher: commonWatcher{st: st},
1051 out: make(chan struct{}), 1055 out: make(chan struct{}),
1052 } 1056 }
1053 go func() { 1057 go func() {
1054 defer w.tomb.Done() 1058 defer w.tomb.Done()
1055 defer close(w.out) 1059 defer close(w.out)
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
1274 select { 1278 select {
1275 case <-w.tomb.Dying(): 1279 case <-w.tomb.Dying():
1276 return tomb.ErrDying 1280 return tomb.ErrDying
1277 case <-in: 1281 case <-in:
1278 // Simply emit event for each change. 1282 // Simply emit event for each change.
1279 w.out <- struct{}{} 1283 w.out <- struct{}{}
1280 } 1284 }
1281 } 1285 }
1282 panic("unreachable") 1286 panic("unreachable")
1283 } 1287 }
OLDNEW
« no previous file with comments | « state/unit_test.go ('k') | worker/uniter/filter.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b