-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleworker.go
37 lines (31 loc) · 988 Bytes
/
simpleworker.go
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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package worker
import (
"gopkg.in/juju/worker.v1"
"gopkg.in/tomb.v2"
)
// simpleWorker implements the worker returned by NewSimpleWorker.
type simpleWorker struct {
tomb tomb.Tomb
}
// NewSimpleWorker returns a worker that runs the given function. The
// stopCh argument will be closed when the worker is killed. The error returned
// by the doWork function will be returned by the worker's Wait function.
func NewSimpleWorker(doWork func(stopCh <-chan struct{}) error) worker.Worker {
w := &simpleWorker{}
w.tomb.Go(func() error {
return doWork(w.tomb.Dying())
})
return w
}
// Kill implements Worker.Kill() and will close the channel given to the doWork
// function.
func (w *simpleWorker) Kill() {
w.tomb.Kill(nil)
}
// Wait implements Worker.Wait(), and will return the error returned by
// the doWork function.
func (w *simpleWorker) Wait() error {
return w.tomb.Wait()
}