github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/workers.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "github.com/juju/loggo" 11 "github.com/juju/pubsub/v2" 12 "github.com/juju/worker/v3" 13 14 "github.com/juju/juju/state/watcher" 15 jworker "github.com/juju/juju/worker" 16 ) 17 18 const ( 19 txnLogWorker = "txnlog" 20 ) 21 22 // workers runs the workers that a State instance requires. 23 // It wraps a Runner instance which restarts any of the 24 // workers when they fail. 25 type workers struct { 26 state *State 27 *worker.Runner 28 29 hub *pubsub.SimpleHub 30 } 31 32 func newWorkers(st *State, hub *pubsub.SimpleHub) (*workers, error) { 33 if hub == nil { 34 return nil, errors.NotValidf("missing hub") 35 } 36 ws := &workers{ 37 state: st, 38 hub: hub, 39 Runner: worker.NewRunner(worker.RunnerParams{ 40 Logger: loggo.GetLogger("juju.state.watcher"), 41 IsFatal: func(err error) bool { return err == jworker.ErrRestartAgent }, 42 RestartDelay: time.Second, 43 Clock: st.clock(), 44 }), 45 } 46 _ = ws.StartWorker(txnLogWorker, func() (worker.Worker, error) { 47 return watcher.NewHubWatcher(watcher.HubWatcherConfig{ 48 Hub: hub, 49 Clock: st.clock(), 50 ModelUUID: st.ModelUUID(), 51 Logger: loggo.GetLogger("juju.state.watcher"), 52 }) 53 }) 54 return ws, nil 55 } 56 57 func (ws *workers) txnLogWatcher() watcher.BaseWatcher { 58 w, err := ws.Worker(txnLogWorker, nil) 59 if err != nil { 60 return watcher.NewDead(errors.Trace(err)) 61 } 62 return w.(watcher.BaseWatcher) 63 }