~ubuntu-branches/ubuntu/saucy/juju-core/saucy

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/worker/stringsworker.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 16:02:16 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20130820160216-5yu1llasa2e2youn
Tags: 1.13.1-0ubuntu1
* New upstream release.
  - Build and install juju metadata plugin.
  - d/NEWS: Add some guidance on upgrading environments from 1.11.x
    to 1.13.x.
* d/NEWS: Add details about lack of upgrade path from juju < 1.11
  and how to interact with older juju environments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package worker
 
5
 
 
6
import (
 
7
        "launchpad.net/tomb"
 
8
 
 
9
        "launchpad.net/juju-core/state/api"
 
10
        "launchpad.net/juju-core/state/watcher"
 
11
)
 
12
 
 
13
// stringsWorker is the internal implementation of the Worker
 
14
// interface, using a StringsWatcher for handling changes.
 
15
type stringsWorker struct {
 
16
        tomb tomb.Tomb
 
17
 
 
18
        // handler is what will be called when events are triggered.
 
19
        handler StringsWatchHandler
 
20
 
 
21
        // mustErr is set to watcher.MustErr, but that panic()s, so
 
22
        // we let the test suite override it.
 
23
        mustErr func(watcher.Errer) error
 
24
}
 
25
 
 
26
// StringsWatchHandler implements the business logic triggered as part
 
27
// of watching a StringsWatcher.
 
28
type StringsWatchHandler interface {
 
29
        // SetUp starts the handler, this should create the watcher we
 
30
        // will be waiting on for more events. SetUp can return a Watcher
 
31
        // even if there is an error, and strings Worker will make sure to
 
32
        // stop the watcher.
 
33
        SetUp() (api.StringsWatcher, error)
 
34
 
 
35
        // TearDown should cleanup any resources that are left around
 
36
        TearDown() error
 
37
 
 
38
        // Handle is called when the Watcher has indicated there are
 
39
        // changes, do whatever work is necessary to process it
 
40
        Handle(changes []string) error
 
41
}
 
42
 
 
43
// NewStringsWorker starts a new worker running the business logic
 
44
// from the handler. The worker loop is started in another goroutine
 
45
// as a side effect of calling this.
 
46
func NewStringsWorker(handler StringsWatchHandler) Worker {
 
47
        sw := &stringsWorker{
 
48
                handler: handler,
 
49
                mustErr: watcher.MustErr,
 
50
        }
 
51
        go func() {
 
52
                defer sw.tomb.Done()
 
53
                sw.tomb.Kill(sw.loop())
 
54
        }()
 
55
        return sw
 
56
}
 
57
 
 
58
// Kill the loop with no-error
 
59
func (sw *stringsWorker) Kill() {
 
60
        sw.tomb.Kill(nil)
 
61
}
 
62
 
 
63
// Wait for the looping to finish
 
64
func (sw *stringsWorker) Wait() error {
 
65
        return sw.tomb.Wait()
 
66
}
 
67
 
 
68
func (sw *stringsWorker) loop() error {
 
69
        w, err := sw.handler.SetUp()
 
70
        if err != nil {
 
71
                if w != nil {
 
72
                        // We don't bother to propagate an error, because we
 
73
                        // already have an error
 
74
                        w.Stop()
 
75
                }
 
76
                return err
 
77
        }
 
78
        defer propagateTearDown(sw.handler, &sw.tomb)
 
79
        defer watcher.Stop(w, &sw.tomb)
 
80
        for {
 
81
                select {
 
82
                case <-sw.tomb.Dying():
 
83
                        return tomb.ErrDying
 
84
                case changes, ok := <-w.Changes():
 
85
                        if !ok {
 
86
                                return sw.mustErr(w)
 
87
                        }
 
88
                        if err := sw.handler.Handle(changes); err != nil {
 
89
                                return err
 
90
                        }
 
91
                }
 
92
        }
 
93
}