github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/singular/manifold.go (about) 1 // Copyright 2015-2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package singular 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "github.com/juju/juju/agent" 11 "github.com/juju/juju/api/base" 12 "github.com/juju/juju/cmd/jujud/agent/engine" 13 "github.com/juju/juju/worker" 14 "github.com/juju/juju/worker/dependency" 15 "github.com/juju/utils/clock" 16 "gopkg.in/juju/names.v2" 17 ) 18 19 // ManifoldConfig holds the information necessary to run a FlagWorker in 20 // a dependency.Engine. 21 type ManifoldConfig struct { 22 ClockName string 23 APICallerName string 24 AgentName string 25 Duration time.Duration 26 27 NewFacade func(base.APICaller, names.MachineTag) (Facade, error) 28 NewWorker func(FlagConfig) (worker.Worker, error) 29 } 30 31 // start is a method on ManifoldConfig because it's more readable than a closure. 32 func (config ManifoldConfig) start(context dependency.Context) (worker.Worker, error) { 33 var clock clock.Clock 34 if err := context.Get(config.ClockName, &clock); err != nil { 35 return nil, errors.Trace(err) 36 } 37 var apiCaller base.APICaller 38 if err := context.Get(config.APICallerName, &apiCaller); err != nil { 39 return nil, errors.Trace(err) 40 } 41 var agent agent.Agent 42 if err := context.Get(config.AgentName, &agent); err != nil { 43 return nil, errors.Trace(err) 44 } 45 agentTag := agent.CurrentConfig().Tag() 46 machineTag, ok := agentTag.(names.MachineTag) 47 if !ok { 48 return nil, errors.New("singular flag expected a machine agent") 49 } 50 51 // TODO(fwereade): model id is implicit in apiCaller, would 52 // be better if explicit. 53 facade, err := config.NewFacade(apiCaller, machineTag) 54 if err != nil { 55 return nil, errors.Trace(err) 56 } 57 flag, err := config.NewWorker(FlagConfig{ 58 Clock: clock, 59 Facade: facade, 60 Duration: config.Duration, 61 }) 62 if err != nil { 63 return nil, errors.Trace(err) 64 } 65 return flag, nil 66 } 67 68 // Manifold returns a dependency.Manifold that will run a FlagWorker and 69 // expose it to clients as a engine.Flag resource. 70 func Manifold(config ManifoldConfig) dependency.Manifold { 71 return dependency.Manifold{ 72 Inputs: []string{ 73 config.ClockName, 74 config.APICallerName, 75 config.AgentName, 76 }, 77 Start: config.start, 78 Output: engine.FlagOutput, 79 } 80 }