github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/syslogger/manifold.go (about) 1 // Copyright 2022 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package syslogger 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/worker/v3" 9 "github.com/juju/worker/v3/dependency" 10 11 corelogger "github.com/juju/juju/core/logger" 12 ) 13 14 // ManifoldConfig defines the names of the manifolds on which a Manifold will 15 // depend. 16 type ManifoldConfig struct { 17 NewWorker func(WorkerConfig) (worker.Worker, error) 18 NewLogger NewLogger 19 } 20 21 // Validate validates the manifold configuration. 22 func (config ManifoldConfig) Validate() error { 23 if config.NewWorker == nil { 24 return errors.NotValidf("nil NewWorker") 25 } 26 if config.NewLogger == nil { 27 return errors.NotValidf("nil NewLogger") 28 } 29 return nil 30 } 31 32 // Manifold returns a dependency manifold that runs the dbaccessor 33 // worker, using the resource names defined in the supplied config. 34 func Manifold(config ManifoldConfig) dependency.Manifold { 35 return dependency.Manifold{ 36 Inputs: []string{}, 37 Output: output, 38 Start: func(context dependency.Context) (worker.Worker, error) { 39 if err := config.Validate(); err != nil { 40 return nil, errors.Trace(err) 41 } 42 43 w, err := config.NewWorker(WorkerConfig{ 44 NewLogger: config.NewLogger, 45 }) 46 if err != nil { 47 return nil, errors.Trace(err) 48 } 49 return w, nil 50 }, 51 } 52 } 53 54 type withOutputs interface { 55 Log([]corelogger.LogRecord) error 56 } 57 58 func output(in worker.Worker, out interface{}) error { 59 w, ok := in.(withOutputs) 60 if !ok { 61 return errors.Errorf("expected input of type syslogWorker, got %T", in) 62 } 63 64 switch out := out.(type) { 65 case *SysLogger: 66 var target SysLogger = w 67 *out = target 68 default: 69 return errors.Errorf("expected output of *syslogger.SysLogger, got %T", out) 70 } 71 return nil 72 }