github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/agent/manifold.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package agent 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/worker.v1" 9 "gopkg.in/juju/worker.v1/dependency" 10 "gopkg.in/tomb.v2" 11 12 "github.com/juju/juju/agent" 13 "github.com/juju/juju/version" 14 ) 15 16 // Manifold returns a manifold that starts a worker proxying the supplied Agent 17 // for use by other workers. 18 func Manifold(a agent.Agent) dependency.Manifold { 19 return dependency.Manifold{ 20 Start: startFunc(a), 21 Output: outputFunc, 22 } 23 } 24 25 // startFunc returns a StartFunc that starts a worker holding a reference to 26 // the supplied Agent. 27 func startFunc(a agent.Agent) dependency.StartFunc { 28 return func(_ dependency.Context) (worker.Worker, error) { 29 w := &agentWorker{agent: a} 30 w.tomb.Go(func() error { 31 <-w.tomb.Dying() 32 return nil 33 }) 34 return w, nil 35 } 36 } 37 38 // outputFunc extracts an Agent from its *agentWorker. 39 func outputFunc(in worker.Worker, out interface{}) error { 40 inWorker, _ := in.(*agentWorker) 41 outPointer, _ := out.(*agent.Agent) 42 if inWorker == nil || outPointer == nil { 43 return errors.Errorf("expected %T->%T; got %T->%T", inWorker, outPointer, in, out) 44 } 45 *outPointer = inWorker.agent 46 return nil 47 } 48 49 // agentWorker is a degenerate worker.Worker that exists only to make an Agent 50 // accessible via the manifold's Output. 51 type agentWorker struct { 52 tomb tomb.Tomb 53 agent agent.Agent 54 } 55 56 // Kill is part of the worker.Worker interface. 57 func (w *agentWorker) Kill() { 58 w.tomb.Kill(nil) 59 } 60 61 // Wait is part of the worker.Worker interface. 62 func (w *agentWorker) Wait() error { 63 return w.tomb.Wait() 64 } 65 66 // Report shows up in the dependency engine report. 67 func (w *agentWorker) Report() map[string]interface{} { 68 cfg := w.agent.CurrentConfig() 69 return map[string]interface{}{ 70 "agent": cfg.Tag().String(), 71 "model-uuid": cfg.Model().Id(), 72 "version": version.Current.String(), 73 } 74 }