github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/minunitsworker/minunitsworker.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package minunitsworker 5 6 import ( 7 "github.com/juju/loggo" 8 "gopkg.in/juju/worker.v1" 9 10 "github.com/juju/juju/state" 11 "github.com/juju/juju/watcher/legacy" 12 ) 13 14 var logger = loggo.GetLogger("juju.worker.minunitsworker") 15 16 // MinUnitsWorker ensures the minimum number of units for applications is respected. 17 type MinUnitsWorker struct { 18 st *state.State 19 } 20 21 // NewMinUnitsWorker returns a Worker that runs application.EnsureMinUnits() 22 // if the number of alive units belonging to a application decreases, or if the 23 // minimum required number of units for a application is increased. 24 func NewMinUnitsWorker(st *state.State) worker.Worker { 25 mu := &MinUnitsWorker{st: st} 26 return legacy.NewStringsWorker(mu) 27 } 28 29 func (mu *MinUnitsWorker) SetUp() (state.StringsWatcher, error) { 30 return mu.st.WatchMinUnits(), nil 31 } 32 33 func (mu *MinUnitsWorker) handleOneApplication(applicationName string) error { 34 application, err := mu.st.Application(applicationName) 35 if err != nil { 36 return err 37 } 38 return application.EnsureMinUnits() 39 } 40 41 func (mu *MinUnitsWorker) Handle(applicationNames []string) error { 42 for _, name := range applicationNames { 43 logger.Infof("processing application %q", name) 44 if err := mu.handleOneApplication(name); err != nil { 45 logger.Errorf("failed to process application %q: %v", name, err) 46 return err 47 } 48 } 49 return nil 50 } 51 52 func (mu *MinUnitsWorker) TearDown() error { 53 // Nothing to do here. 54 return nil 55 }