From e9ebe0a8ed3416938e4a703750628bc57951dc74 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 24 Sep 2016 13:58:34 +0200 Subject: cleanup some hardcoded values (still not done), added ticker to update states if the go stale diff --git a/src/rhctl/switch_control.go b/src/rhctl/switch_control.go index 66a41bf..cd66161 100644 --- a/src/rhctl/switch_control.go +++ b/src/rhctl/switch_control.go @@ -87,12 +87,17 @@ type Command struct { } type SwitchControl struct { - sw *AudioSwitch - servers []*PlayoutServer - state State - settling *time.Timer - Updates *pubsub.PubSub - Commands chan *Command + sw *AudioSwitch + servers []*PlayoutServer + state State + settling *time.Timer + Updates *pubsub.PubSub + Commands chan *Command + AwakeningSettlingTime time.Duration // 30 * time.Second + SelectServerSettlingTime time.Duration // 10 * time.Second + UpdateStatesSettlingTime time.Duration // 3 * time.Second + StaleStateCheckInterval time.Duration // 1 * time.Minute + StatesMaxAge time.Duration // 2 * time.Hour } func (ctrl *SwitchControl) handleCommand(cmd *Command) { @@ -142,29 +147,36 @@ func handleServer(sin <-chan ServerState, sout chan<- ServerState, cin <-chan Co } } -func (ctrl *SwitchControl) checkMissingOrStaleStates(maxAge time.Duration) { +func (ctrl *SwitchControl) checkMissingOrStaleStates(maxAge time.Duration) (isStale bool) { if time.Since(ctrl.state.Switch.AudioInputsChanged) > maxAge { ctrl.sw.Commands <- &SwitchCommand{SwitchCmdStateAudio, nil, nil} + isStale = true } if time.Since(ctrl.state.Switch.AudioSilenceChanged) > maxAge { ctrl.sw.Commands <- &SwitchCommand{SwitchCmdStateSilence, nil, nil} + isStale = true } if time.Since(ctrl.state.Switch.GPIChanged) > maxAge { ctrl.sw.Commands <- &SwitchCommand{SwitchCmdStateGPIAll, nil, nil} + isStale = true } if time.Since(ctrl.state.Switch.RelayChanged) > maxAge { ctrl.sw.Commands <- &SwitchCommand{SwitchCmdStateRelay, nil, nil} + isStale = true } if time.Since(ctrl.state.Switch.OCChanged) > maxAge { ctrl.sw.Commands <- &SwitchCommand{SwitchCmdStateOC, nil, nil} + isStale = true } for _, server := range ctrl.servers { s, exists := ctrl.state.Server[server.name] if !exists || time.Since(s.Changed) > maxAge { server.UpdateRequest <- true + isStale = true } } + return } func (ctrl *SwitchControl) getSwitchServerAssignment() (swsrv, swch string, err error) { @@ -196,7 +208,7 @@ func (ctrl *SwitchControl) selectServer(server string) bool { } } ctrl.sw.Commands <- &SwitchCommand{SwitchCmdAudioFadeUpInput, []interface{}{newIn}, nil} - ctrl.settling.Reset(10 * time.Second) // TODO: hardcoded value + ctrl.settling.Reset(ctrl.SelectServerSettlingTime) ctrl.state.Settled = false return true } @@ -309,7 +321,7 @@ func (ctrl *SwitchControl) reconcile(requestedServer string) (result bool) { func (ctrl *SwitchControl) Run() { rhl.Printf("SwitchCTRL: starting up -> now in mood: %s", ctrl.state.Mood) - ctrl.settling = time.NewTimer(30 * time.Second) // TODO: hardcode value + ctrl.settling = time.NewTimer(ctrl.AwakeningSettlingTime) ctrl.state.Settled = false serverStateChanges := make(chan ServerState, 8) @@ -318,11 +330,19 @@ func (ctrl *SwitchControl) Run() { } // send out commands to switch and/or server to get missing infos - ctrl.checkMissingOrStaleStates(2 * time.Hour) // TODO: hardcoded value + ctrl.checkMissingOrStaleStates(0) + staleTicker := time.NewTicker(ctrl.StaleStateCheckInterval) ctrl.reconcile("") for { select { + case <-staleTicker.C: + if ctrl.state.Settled { // better don't interfere with changes in progress + if ctrl.checkMissingOrStaleStates(ctrl.StatesMaxAge) { + ctrl.settling.Reset(ctrl.UpdateStatesSettlingTime) + ctrl.state.Settled = false + } + } case <-ctrl.settling.C: ctrl.state.Settled = true ctrl.reconcile("") @@ -359,5 +379,12 @@ func SwitchControlInit(conf *Config, sw *AudioSwitch, servers []*PlayoutServer) ctrl.state.Server = make(map[string]ServerState) ctrl.Updates = pubsub.NewNonBlocking(32) ctrl.Commands = make(chan *Command, 8) + + // TODO: get this from conf + ctrl.AwakeningSettlingTime = 30 * time.Second + ctrl.SelectServerSettlingTime = 10 * time.Second + ctrl.UpdateStatesSettlingTime = 3 * time.Second + ctrl.StaleStateCheckInterval = 1 * time.Minute + ctrl.StatesMaxAge = 2 * time.Hour return } -- cgit v0.10.2