~themue/juju-core/go-state-lifecycle-watcher

« back to all changes in this revision

Viewing changes to state/watcher.go

  • Committer: Frank Mueller
  • Date: 2012-08-21 09:33:00 UTC
  • mfrom: (357.1.2 juju-core)
  • Revision ID: themue@gmail.com-20120821093300-c8uo15pg17fsju8u
state: merged trunk to be up-to-date

Show diffs side-by-side

added added

removed removed

Lines of Context:
525
525
        close(w.changeChan)
526
526
}
527
527
 
 
528
// AgentToolsWatcher observes changes to an agent's tools.
 
529
type AgentToolsWatcher struct {
 
530
        contentWatcher
 
531
        changeChan chan *Tools
 
532
        prefix     string
 
533
        current    *Tools
 
534
}
 
535
 
 
536
// newAgentToolsWatcher creates a new watcher watching
 
537
// the tools at the given path with the given attribute prefix
 
538
// ("current" or "proposed").
 
539
func newAgentToolsWatcher(st *State, path, prefix string) *AgentToolsWatcher {
 
540
        w := &AgentToolsWatcher{
 
541
                contentWatcher: newContentWatcher(st, path),
 
542
                changeChan:     make(chan *Tools),
 
543
                prefix:         prefix,
 
544
        }
 
545
        go w.loop(w)
 
546
        return w
 
547
}
 
548
 
 
549
func (w *AgentToolsWatcher) Changes() <-chan *Tools {
 
550
        return w.changeChan
 
551
}
 
552
 
 
553
func (w *AgentToolsWatcher) update(change watcher.ContentChange) error {
 
554
        // A non-existent node is treated as an empty node.
 
555
        configNode, err := parseConfigNode(w.st.zk, w.path, change.Content)
 
556
        if err != nil {
 
557
                return err
 
558
        }
 
559
        tools, err := getAgentTools(configNode, w.prefix)
 
560
        if err != nil {
 
561
                return err
 
562
        }
 
563
        if w.current != nil && *tools == *w.current {
 
564
                return nil
 
565
        }
 
566
        w.current = tools
 
567
        select {
 
568
        case <-w.tomb.Dying():
 
569
                return tomb.ErrDying
 
570
        case w.changeChan <- w.current:
 
571
        }
 
572
        return nil
 
573
}
 
574
 
 
575
func (w *AgentToolsWatcher) done() {
 
576
        close(w.changeChan)
 
577
}
 
578
 
528
579
// ServicesWatcher observes the addition and removal of services.
529
580
type ServicesWatcher struct {
530
581
        contentWatcher