~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/worker.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012-2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package worker
 
5
 
 
6
import (
 
7
        "github.com/juju/loggo"
 
8
)
 
9
 
 
10
var logger = loggo.GetLogger("juju.worker")
 
11
 
 
12
// Worker describes any type whose validity and/or activity is bounded
 
13
// in time. Most frequently, they will represent the duration of some
 
14
// task or tasks running on internal goroutines, but it's possible and
 
15
// rational to use them to represent any resource that might become
 
16
// invalid.
 
17
//
 
18
// Worker implementations must be goroutine-safe.
 
19
type Worker interface {
 
20
 
 
21
        // Kill asks the worker to stop and returns immediately.
 
22
        Kill()
 
23
 
 
24
        // Wait waits for the worker to complete and returns any
 
25
        // error encountered when it was running or stopping.
 
26
        Wait() error
 
27
}
 
28
 
 
29
// Stop kills the given Worker and waits for it to complete.
 
30
func Stop(worker Worker) error {
 
31
        worker.Kill()
 
32
        return worker.Wait()
 
33
}
 
34
 
 
35
// Dead returns a channel that will be closed when the supplied
 
36
// Worker has completed.
 
37
//
 
38
// Don't be too casual about calling Dead -- for example, in a
 
39
// standard select loop, `case <-worker.Dead(w):` will create
 
40
// one new goroutine per iteration, which is... untidy.
 
41
func Dead(worker Worker) <-chan struct{} {
 
42
        dead := make(chan struct{})
 
43
        go func() {
 
44
                defer close(dead)
 
45
                worker.Wait()
 
46
        }()
 
47
        return dead
 
48
}