diff options
Diffstat (limited to 'src/rhctl')
-rw-r--r-- | src/rhctl/switch_control.go | 69 |
1 files changed, 63 insertions, 6 deletions
diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go index 7ff2ad1..5c7bd7c 100644 --- a/src/rhctl/switch_control.go +++ b/src/rhctl/switch_control.go @@ -156,28 +156,81 @@ func (ctrl *SwitchControl) getSwitchServerAssignment() (swsrv, swch string, err return "", "", errors.New("not implemented") } +func (ctrl *SwitchControl) selectServer(server string) { + // TODO: send out commands to switch +} + func (ctrl *SwitchControl) reconcile(requestedServer string, snooze bool) (result bool) { rhdl.Printf("SwitchCTRL: reconciling state... (requested server: '%s', snooze: %t)", requestedServer, snooze) - // send out commands to switch and/or server to get missing infos - // TODO: this shouldn't be called every time, because this will lead to a lot of requests as updates come in... - ctrl.checkMissingOrStaleStates(2 * time.Hour) // TODO: hardcoded value - switch ctrl.state.Mood { case MoodAwakening: if !snooze { // this not a wakeup of the snooze timer // it's too early to take actions - let's learn a little bit more about current state return } + if len(ctrl.servers) < 1 { + ctrl.state.Mood = MoodHappy + rhdl.Printf("SwitchCTRL: there are no servers configured -> now in mood: %s", ctrl.state.Mood) + return + } + + if requestedServer != "" { + rhl.Printf("SwitchCTRL: ignoreing preferred server requests while waking-up") + return + } + swsrv, swch, err := ctrl.getSwitchServerAssignment() if err != nil { ctrl.state.Mood = MoodSad - rhl.Printf("SwitchCTRL: switch input assignments are ambigious or outdated -> now in mood: %s", ctrl.state.Mood) + rhl.Printf("SwitchCTRL: just woken up... switch input assignments are ambigious or outdated -> now in mood: %s", ctrl.state.Mood) + return + } + + rhl.Printf("SwitchCTRL: just woken up... switch is set to server '%s' channel '%s'", swsrv, swch) + + s, exists := ctrl.state.Server[swsrv] + if !exists || s.Health != ServerAlive { + rhl.Printf("SwitchCTRL: server '%s' is unknown or dead!", swsrv) + for name, state := range ctrl.state.Server { + if state.Health == ServerAlive { + ctrl.state.Mood = MoodNervous + rhl.Printf("SwitchCTRL: found another alive server -> now in mood: %s", ctrl.state.Mood) + ctrl.selectServer(name) + return + } + } + ctrl.state.Mood = MoodSad + rhl.Printf("SwitchCTRL: found no alive server -> now in mood: %s", ctrl.state.Mood) + return + } + + ctrl.state.ActiveServer = swsrv + + if s.Channel != swch { + rhl.Printf("SwitchCTRL: switch and server '%s' channel mismatch: switch = '%s', server = '%s'!", swsrv, swch, s.Channel) + ctrl.selectServer(ctrl.state.ActiveServer) return } - rhl.Printf("SwitchCTRL: we are awake now... switch is set to server '%s' channel '%s'", swsrv, swch) + + for _, s := range ctrl.state.Server { + if s.Health != ServerAlive { + ctrl.state.Mood = MoodNervous + rhl.Printf("SwitchCTRL: at least one configured server is dead -> now in mood: %s", ctrl.state.Mood) + return + } + } + ctrl.state.Mood = MoodHappy + rhl.Printf("SwitchCTRL: all servers are alive -> now in mood: %s", ctrl.state.Mood) + return default: + if len(ctrl.servers) < 1 { + ctrl.state.Mood = MoodHappy + rhdl.Printf("SwitchCTRL: there are no servers configured -> now in mood: %s", ctrl.state.Mood) + return + } + rhl.Printf("SwitchCTRL: error handling of mood %s is not implemented", ctrl.state.Mood) } @@ -199,6 +252,10 @@ func (ctrl *SwitchControl) Run() { go handleServer(srv.StateChanges, serverStateChanges) } + // send out commands to switch and/or server to get missing infos + // TODO: this should be called from reconcile but with some sort of rate-limiting... + ctrl.checkMissingOrStaleStates(2 * time.Hour) // TODO: hardcoded value + ctrl.reconcile("", false) for { select { |