~thumper/juju-core/move-environs-tools

« back to all changes in this revision

Viewing changes to worker/cleaner/cleaner.go

  • Committer: Tarmac
  • Author(s): John Arbash Meinel
  • Date: 2013-07-08 12:51:34 UTC
  • mfrom: (1390.3.4 use-notify-worker)
  • Revision ID: tarmac-20130708125134-nr74t8u11iw61vu0
[r=jameinel] worker/cleaner: use new NotifyWorker structure

Just reimplements the Cleaner worker as a WatchHandler passed to the
NotifyWorker. Cleaner was pretty simple, but I'm still able to remove about
20 lines of boilerplate. (And replace it with heavily tested logic.)

https://codereview.appspot.com/10876048/

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import (
7
7
        "fmt"
 
8
 
8
9
        "launchpad.net/juju-core/log"
9
10
        "launchpad.net/juju-core/state"
10
 
        "launchpad.net/juju-core/state/watcher"
11
 
        "launchpad.net/tomb"
 
11
        "launchpad.net/juju-core/state/api/params"
 
12
        "launchpad.net/juju-core/worker"
12
13
)
13
14
 
14
15
// Cleaner is responsible for cleaning up the state.
15
16
type Cleaner struct {
16
 
        tomb tomb.Tomb
17
 
        st   *state.State
 
17
        st *state.State
18
18
}
19
19
 
20
 
// NewCleaner returns a Cleaner that runs state.Cleanup()
 
20
// NewCleaner returns a worker.NotifyWorker that runs state.Cleanup()
21
21
// if the CleanupWatcher signals documents marked for deletion.
22
 
func NewCleaner(st *state.State) *Cleaner {
23
 
        c := &Cleaner{st: st}
24
 
        go func() {
25
 
                defer c.tomb.Done()
26
 
                c.tomb.Kill(c.loop())
27
 
        }()
28
 
        return c
 
22
func NewCleaner(st *state.State) worker.NotifyWorker {
 
23
        return worker.NewNotifyWorker(&Cleaner{st: st})
29
24
}
30
25
 
31
26
func (c *Cleaner) String() string {
32
27
        return fmt.Sprintf("cleaner")
33
28
}
34
29
 
35
 
func (c *Cleaner) Kill() {
36
 
        c.tomb.Kill(nil)
37
 
}
38
 
 
39
 
func (c *Cleaner) Stop() error {
40
 
        c.tomb.Kill(nil)
41
 
        return c.tomb.Wait()
42
 
}
43
 
 
44
 
func (c *Cleaner) Wait() error {
45
 
        return c.tomb.Wait()
46
 
}
47
 
 
48
 
func (c *Cleaner) loop() error {
49
 
        w := c.st.WatchCleanups()
50
 
        defer watcher.Stop(w, &c.tomb)
51
 
 
52
 
        for {
53
 
                select {
54
 
                case <-c.tomb.Dying():
55
 
                        return tomb.ErrDying
56
 
                case _, ok := <-w.Changes():
57
 
                        if !ok {
58
 
                                return watcher.MustErr(w)
59
 
                        }
60
 
                        if err := c.st.Cleanup(); err != nil {
61
 
                                log.Errorf("worker/cleaner: cannot cleanup state: %v", err)
62
 
                        }
63
 
                }
 
30
func (c *Cleaner) SetUp() (params.NotifyWatcher, error) {
 
31
        return c.st.WatchCleanups(), nil
 
32
}
 
33
 
 
34
func (c *Cleaner) Handle() error {
 
35
        if err := c.st.Cleanup(); err != nil {
 
36
                log.Errorf("worker/cleaner: cannot cleanup state: %v", err)
64
37
        }
65
 
        panic("unreachable")
 
38
        // We do not return the err from Cleanup, because we don't want to stop
 
39
        // the loop as a failure
 
40
        return nil
 
41
}
 
42
 
 
43
func (c *Cleaner) TearDown() error {
 
44
        // Nothing to cleanup, only state is the watcher
 
45
        return nil
66
46
}