github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/pubsub/reporter.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package pubsub 5 6 import ( 7 "sync" 8 9 "gopkg.in/juju/worker.v1" 10 ) 11 12 // Reporter gives visibility for the introspection worker into the 13 // internals of the pubsub forwarding worker. Also defines the 14 // Report method used by the engine report. 15 type Reporter interface { 16 Report() map[string]interface{} 17 IntrospectionReport() string 18 } 19 20 // NewReporter returns a reporter for the pubsub forwarding worker. 21 func NewReporter() Reporter { 22 return &reporter{} 23 } 24 25 type reporter struct { 26 mu sync.Mutex 27 worker Reporter 28 } 29 30 // IntrospectionReport is the method called by the introspection 31 // worker to get what to show to the user. 32 func (r *reporter) IntrospectionReport() string { 33 r.mu.Lock() 34 defer r.mu.Unlock() 35 if r.worker == nil { 36 return "pubsub worker not started" 37 } 38 return r.worker.IntrospectionReport() 39 } 40 41 // Report hooks in to the worker's report mechanism. 42 func (r *reporter) Report() map[string]interface{} { 43 r.mu.Lock() 44 defer r.mu.Unlock() 45 if r.worker == nil { 46 return nil 47 } 48 return r.worker.Report() 49 } 50 51 func (r *reporter) setWorker(w worker.Worker) { 52 if rep, ok := w.(Reporter); ok { 53 r.mu.Lock() 54 defer r.mu.Unlock() 55 r.worker = rep 56 } 57 }