Provider loop discovery almost finished
Provider lookup will lookup on startup, do the healthcheck until the stream starts and also restart on stream closure or provider stall
This commit is contained in:
+53
-7
@@ -25,8 +25,10 @@ type TelemetryService struct {
|
||||
// Output window
|
||||
Messages chan string
|
||||
// Cancel looking for providers
|
||||
CtxMonitor context.Context
|
||||
cancelMonitor context.CancelFunc
|
||||
CtxMonitor context.Context
|
||||
cancelMonitor context.CancelFunc
|
||||
CtxHealthcheck context.Context
|
||||
healthCheckCancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewTelemetryService(logger *slog.Logger, devServo *DeviceService) *TelemetryService {
|
||||
@@ -43,15 +45,51 @@ func NewTelemetryService(logger *slog.Logger, devServo *DeviceService) *Telemetr
|
||||
return newService
|
||||
}
|
||||
|
||||
func (t *TelemetryService) OnFindProvider(prov providers.Provider) {
|
||||
func (t *TelemetryService) ProviderMonitor(ctx context.Context) {
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
slog.Debug("checking if provider is still running")
|
||||
if !t.activeProvider.IsAlive(500 * time.Millisecond) {
|
||||
slog.Debug("provider healthcheck failed")
|
||||
t.dropActiveProvider()
|
||||
t.onProviderHealthCheckFailed()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TelemetryService) onProviderHealthCheckFailed() {
|
||||
// Just restart the whole lookup process
|
||||
go t.FindProvider(t.CtxMonitor)
|
||||
}
|
||||
|
||||
func (t *TelemetryService) onFindProvider(prov providers.Provider) {
|
||||
// Attach to the provider
|
||||
t.logger.Info("found provider for " + prov.Name)
|
||||
t.SwitchProvider(prov.NewProvider(t.logger))
|
||||
t.logger.Debug("Found provider for " + prov.Name)
|
||||
|
||||
// Create a routine to poll this provider while we wait to start the stream or pause it
|
||||
t.CtxHealthcheck, t.healthCheckCancel = context.WithCancel(context.Background())
|
||||
go t.ProviderMonitor(t.CtxHealthcheck)
|
||||
}
|
||||
|
||||
func (t *TelemetryService) onProviderStopsMidStream() {
|
||||
// clear the current provider
|
||||
t.logger.Info("cleaning dropped provider and restarting lookup service")
|
||||
t.dropActiveProvider()
|
||||
go t.FindProvider(t.CtxMonitor)
|
||||
}
|
||||
|
||||
// TODO: add some way of retriggering this. Currently it should:
|
||||
// start monitoring on startup -> find provider -> stop monitoring (when game closes for example)
|
||||
func (t *TelemetryService) FindProvider(ctx context.Context, callback func(providers.Provider),
|
||||
) {
|
||||
func (t *TelemetryService) FindProvider(ctx context.Context) {
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
@@ -64,7 +102,7 @@ func (t *TelemetryService) FindProvider(ctx context.Context, callback func(provi
|
||||
for _, prov := range providers.Providers {
|
||||
t.logger.Debug("checking provider: " + prov.Name)
|
||||
if prov.IsRunning() {
|
||||
callback(prov)
|
||||
t.onFindProvider(prov)
|
||||
return
|
||||
}
|
||||
t.logger.Debug(" wasn't read")
|
||||
@@ -95,6 +133,8 @@ func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan tele
|
||||
return
|
||||
case data, ok := <-dataCh:
|
||||
if !ok {
|
||||
t.logger.Debug("something happened on the provider - stream closed")
|
||||
t.onProviderStopsMidStream()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -118,6 +158,8 @@ func (t *TelemetryService) dropActiveProvider() {
|
||||
}
|
||||
|
||||
t.activeProvider.StopStream()
|
||||
t.activeProvider.Close()
|
||||
t.activeProvider = nil
|
||||
}
|
||||
|
||||
func (t *TelemetryService) SubscribeListener(id string, bufferSize int) <-chan telem.TelemetryData {
|
||||
@@ -160,6 +202,10 @@ func (t *TelemetryService) StartStream() {
|
||||
slog.Debug("there's no active provider. not starting the stream")
|
||||
return
|
||||
}
|
||||
|
||||
// Stop the provider healthcheck
|
||||
t.healthCheckCancel()
|
||||
|
||||
simInCh, _ := t.activeProvider.Stream()
|
||||
// TODO: the provider needs to be able to tell the data has stopped
|
||||
// so we can restart the provider lookup routine
|
||||
|
||||
Reference in New Issue
Block a user