github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/fortress/manifold.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package fortress 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/worker" 10 "github.com/juju/juju/worker/dependency" 11 ) 12 13 // Manifold returns a dependency.Manifold that runs a fortress. 14 // 15 // Clients should access the fortress resource via Guard and/or Guest pointers. 16 // Guest.Visit calls will block until a Guard.Unlock call is made; Guard.Lockdown 17 // calls will block new Guest.Visits and wait until all active Visits complete. 18 // 19 // If multiple clients act as guards, the fortress' state at any time will be 20 // determined by whichever guard last ran an operation; that is to say, it will 21 // be impossible to reliably tell from outside. So please don't do that. 22 func Manifold() dependency.Manifold { 23 return dependency.Manifold{ 24 Start: func(_ dependency.Context) (worker.Worker, error) { 25 return newFortress(), nil 26 }, 27 Output: func(in worker.Worker, out interface{}) error { 28 inFortress, _ := in.(*fortress) 29 if inFortress == nil { 30 return errors.Errorf("in should be %T; is %T", inFortress, in) 31 } 32 switch outPointer := out.(type) { 33 case *Guard: 34 *outPointer = inFortress 35 case *Guest: 36 *outPointer = inFortress 37 default: 38 return errors.Errorf("out should be *fortress.Guest or *fortress.Guard; is %T", out) 39 } 40 return nil 41 }, 42 } 43 }