~jameinel/juju-core/api-registry-tracks-type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package worker

import (
	"launchpad.net/tomb"

	apiWatcher "launchpad.net/juju-core/state/api/watcher"
	"launchpad.net/juju-core/state/watcher"
)

// stringsWorker is the internal implementation of the Worker
// interface, using a StringsWatcher for handling changes.
type stringsWorker struct {
	tomb tomb.Tomb

	// handler is what will be called when events are triggered.
	handler StringsWatchHandler
}

// StringsWatchHandler implements the business logic triggered as part
// of watching a StringsWatcher.
type StringsWatchHandler interface {
	// SetUp starts the handler, this should create the watcher we
	// will be waiting on for more events. SetUp can return a Watcher
	// even if there is an error, and strings Worker will make sure to
	// stop the watcher.
	SetUp() (apiWatcher.StringsWatcher, error)

	// TearDown should cleanup any resources that are left around
	TearDown() error

	// Handle is called when the Watcher has indicated there are
	// changes, do whatever work is necessary to process it
	Handle(changes []string) error
}

// NewStringsWorker starts a new worker running the business logic
// from the handler. The worker loop is started in another goroutine
// as a side effect of calling this.
func NewStringsWorker(handler StringsWatchHandler) Worker {
	sw := &stringsWorker{
		handler: handler,
	}
	go func() {
		defer sw.tomb.Done()
		sw.tomb.Kill(sw.loop())
	}()
	return sw
}

// Kill the loop with no-error
func (sw *stringsWorker) Kill() {
	sw.tomb.Kill(nil)
}

// Wait for the looping to finish
func (sw *stringsWorker) Wait() error {
	return sw.tomb.Wait()
}

func (sw *stringsWorker) loop() error {
	w, err := sw.handler.SetUp()
	if err != nil {
		if w != nil {
			// We don't bother to propagate an error, because we
			// already have an error
			w.Stop()
		}
		return err
	}
	defer propagateTearDown(sw.handler, &sw.tomb)
	defer watcher.Stop(w, &sw.tomb)
	for {
		select {
		case <-sw.tomb.Dying():
			return tomb.ErrDying
		case changes, ok := <-w.Changes():
			if !ok {
				return mustErr(w)
			}
			if err := sw.handler.Handle(changes); err != nil {
				return err
			}
		}
	}
}