github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/workers/interface.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package workers 5 6 import ( 7 "github.com/juju/juju/core/lease" 8 "github.com/juju/juju/state/presence" 9 "github.com/juju/juju/state/watcher" 10 "github.com/juju/juju/worker" 11 ) 12 13 // Workers doesn't really need to exist -- it could basically just exist 14 // in the state package -- but that'd entail duplication of the 15 // TxnLogWatcher, PresenceWatcher, and LeaseManager interfaces in both 16 // packages to avoid import cycles, and, yuck. 17 // 18 // See the DumbWorkers and RestartWorkers types for implementations. 19 type Workers interface { 20 worker.Worker 21 22 TxnLogWatcher() TxnLogWatcher 23 PresenceWatcher() PresenceWatcher 24 LeadershipManager() LeaseManager 25 SingularManager() LeaseManager 26 } 27 28 // Factory supplies implementations of various workers used in state, 29 // and is generally a critical dependency of a Workers implementation 30 // such as DumbWorkers or RestartWorkers. 31 // 32 // It'll generally just be a thin wrapper over a *State -- this package 33 // exists only to paper over worker-lifetime issues that are hard to 34 // address in the state package, not really to pave the way to alternate 35 // backends or anything. 36 type Factory interface { 37 NewTxnLogWorker() (TxnLogWorker, error) 38 NewPresenceWorker() (PresenceWorker, error) 39 NewLeadershipWorker() (LeaseWorker, error) 40 NewSingularWorker() (LeaseWorker, error) 41 } 42 43 // ExposedFailer encapsulates methods for shutdown detection and 44 // handling, used widely inside state watcher types. 45 // 46 // Would be lovely to remove this dependency -- by adding new watchers 47 // to something that tracked watcher and/or state validity, and stopped 48 // them automatically -- but that's likely to have impacts reverberating 49 // through state and apiserver and deserves its own careful analysis. 50 type ExposedFailer interface { 51 Dead() <-chan struct{} 52 Err() error 53 } 54 55 // TxnLogWatcher exposes the methods of watcher.Watcher that are needed 56 // by the state package. 57 type TxnLogWatcher interface { 58 ExposedFailer 59 60 // horrible hack for goosing it into activity (for tests). 61 StartSync() 62 63 // single-document watching 64 Watch(coll string, id interface{}, revno int64, ch chan<- watcher.Change) 65 Unwatch(coll string, id interface{}, ch chan<- watcher.Change) 66 67 // collection-watching 68 WatchCollection(coll string, ch chan<- watcher.Change) 69 WatchCollectionWithFilter(coll string, ch chan<- watcher.Change, filter func(interface{}) bool) 70 UnwatchCollection(coll string, ch chan<- watcher.Change) 71 } 72 73 // TxnLogWorker includes the watcher.Watcher's worker.Worker methods, 74 // so that a Workers implementation can manage its lifetime. 75 type TxnLogWorker interface { 76 worker.Worker 77 TxnLogWatcher 78 } 79 80 // PresenceWatcher exposes the methods of presence.Watcher that are 81 // needed by the state package. 82 type PresenceWatcher interface { 83 ExposedFailer 84 85 // Horrible hack for goosing it into activity. Not clear why 86 // this is used by state in place of StartSync, but it is. 87 Sync() 88 89 // Presence-reading and -watching. 90 Alive(key string) (bool, error) 91 Watch(key string, ch chan<- presence.Change) 92 Unwatch(key string, ch chan<- presence.Change) 93 } 94 95 // PresenceWorker includes the presence.Watcher's worker.Worker methods, 96 // so that a Workers implementation can manage its lifetime. 97 type PresenceWorker interface { 98 worker.Worker 99 PresenceWatcher 100 } 101 102 // LeaseManager exposes the methods of lease.Manager that are needed by 103 // the state package. 104 type LeaseManager interface { 105 lease.Claimer 106 lease.Checker 107 } 108 109 // LeaseWorker includes the lease.Manager's worker.Worker methods, 110 // so that a Workers implementation can manage its lifetime. 111 type LeaseWorker interface { 112 worker.Worker 113 LeaseManager 114 }