github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/simpleworker.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package worker 5 6 import ( 7 "github.com/juju/worker/v3" 8 "gopkg.in/tomb.v2" 9 ) 10 11 // simpleWorker implements the worker returned by NewSimpleWorker. 12 type simpleWorker struct { 13 tomb tomb.Tomb 14 } 15 16 // NewSimpleWorker returns a worker that runs the given function. The 17 // stopCh argument will be closed when the worker is killed. The error returned 18 // by the doWork function will be returned by the worker's Wait function. 19 func NewSimpleWorker(doWork func(stopCh <-chan struct{}) error) worker.Worker { 20 w := &simpleWorker{} 21 w.tomb.Go(func() error { 22 return doWork(w.tomb.Dying()) 23 }) 24 return w 25 } 26 27 // Kill implements Worker.Kill() and will close the channel given to the doWork 28 // function. 29 func (w *simpleWorker) Kill() { 30 w.tomb.Kill(nil) 31 } 32 33 // Wait implements Worker.Wait(), and will return the error returned by 34 // the doWork function. 35 func (w *simpleWorker) Wait() error { 36 return w.tomb.Wait() 37 }