1
// Copyright 2012-2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/loggo"
10
var logger = loggo.GetLogger("juju.worker")
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
18
// Worker implementations must be goroutine-safe.
19
type Worker interface {
21
// Kill asks the worker to stop and returns immediately.
24
// Wait waits for the worker to complete and returns any
25
// error encountered when it was running or stopping.
29
// Stop kills the given Worker and waits for it to complete.
30
func Stop(worker Worker) error {
35
// Dead returns a channel that will be closed when the supplied
36
// Worker has completed.
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{})