github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/storage/storager.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package storage 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 10 "github.com/juju/juju/worker/uniter/hook" 11 "github.com/juju/juju/worker/uniter/runner/jujuc" 12 ) 13 14 type storager struct { 15 st StorageAccessor 16 unitTag names.UnitTag 17 storageTag names.StorageTag 18 state *stateFile 19 source *storageSource 20 sender hook.Sender 21 } 22 23 // newStorager creates a new storager, watching for changes to the storage 24 // attachment with the specified tags, and generating hooks on the output 25 // channel. 26 func newStorager( 27 st StorageAccessor, 28 unitTag names.UnitTag, 29 storageTag names.StorageTag, 30 state *stateFile, 31 hooks chan<- hook.Info, 32 ) (*storager, error) { 33 source, err := newStorageSource(st, unitTag, storageTag, state.attached) 34 if err != nil { 35 return nil, errors.Annotate(err, "creating storage event source") 36 } 37 sender := hook.NewSender(hooks, source) 38 return &storager{ 39 st: st, 40 unitTag: unitTag, 41 storageTag: storageTag, 42 state: state, 43 source: source, 44 sender: sender, 45 }, nil 46 } 47 48 // Stop stops the storager from generating or sending any more hook events. 49 func (s *storager) Stop() error { 50 if err := s.sender.Stop(); err != nil { 51 return errors.Annotate(err, "stopping storage event sender") 52 } 53 return s.source.Stop() 54 } 55 56 // Context returns the ContextStorage for the storage that this storager 57 // corresponds to, and whether there is any context available yet. There 58 // will be context beginning from when the first hook is queued. 59 func (s *storager) Context() (jujuc.ContextStorageAttachment, bool) { 60 return s.source.Context() 61 } 62 63 // CommitHook persists the state change encoded in the supplied storage 64 // hook, or returns an error if the hook is invalid given current state. 65 func (s *storager) CommitHook(hi hook.Info) error { 66 return s.state.CommitHook(hi) 67 }