Reset chat bot stream ever 90 minutes

This commit is contained in:
tyler 2024-02-14 15:01:06 -05:00
parent cdbaf8ff14
commit 7d0e825077
3 changed files with 65 additions and 14 deletions

View file

@ -1,7 +1,5 @@
import { useEffect, useState } from 'react';
import { Modal, SmallModal } from './Modal';
import { LoginChatBot, UpdateChatBotUrl } from '../../wailsjs/go/main/App';
import './ChatBot.css';

View file

@ -122,6 +122,10 @@ function Dashboard() {
// console.log('Query response error:', error);
setActive(false);
});
EventsOn('ChatBotChatStreamError', (error) => {
setError(error);
});
}, []);
const home = () => {

View file

@ -22,6 +22,8 @@ import (
type ChatBot struct {
ctx context.Context
cancelChatStream context.CancelFunc
cancelChatStreamMu sync.Mutex
client *rumblelivestreamlib.Client
commands map[string]chan rumblelivestreamlib.ChatView
commandsMu sync.Mutex
@ -389,21 +391,68 @@ func (cb *ChatBot) StartChatStream() error {
return fmt.Errorf("chatbot: error getting chat info: %v", err)
}
err = cb.client.StartChatStream(cb.handleChat, cb.handleError)
if err != nil {
return fmt.Errorf("chatbot: error starting chat stream: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cb.cancelChatStreamMu.Lock()
cb.cancelChatStream = cancel
cb.cancelChatStreamMu.Unlock()
go cb.startChatStream(ctx)
// err = cb.client.StartChatStream(cb.handleChat, cb.handleError)
// if err != nil {
// return fmt.Errorf("chatbot: error starting chat stream: %v", err)
// }
return nil
}
func (cb *ChatBot) startChatStream(ctx context.Context) {
for {
err := cb.client.StartChatStream(cb.handleChat, cb.handleError)
if err != nil {
cb.logError.Println("error starting chat stream:", err)
runtime.EventsEmit(cb.ctx, "ChatBotChatStreamError", "Error starting chat stream.")
return
}
select {
case <-time.After(90 * time.Minute):
cb.client.StopChatStream()
break
case <-ctx.Done():
cb.client.StopChatStream()
return
}
}
}
func (cb *ChatBot) StopChatStream() error {
if cb.client == nil {
return fmt.Errorf("chatbot: client is nil")
}
// TODO: should a panic be caught here?
cb.cancelChatStreamMu.Lock()
if cb.cancelChatStream != nil {
cb.cancelChatStream()
} else {
cb.client.StopChatStream()
}
cb.cancelChatStreamMu.Unlock()
return nil
}
func (cb *ChatBot) RestartChatStream() error {
if cb.client == nil {
return fmt.Errorf("chatbot: client is nil")
}
cb.client.StopChatStream()
err := cb.client.StartChatStream(cb.handleChat, cb.handleError)
if err != nil {
return fmt.Errorf("chatbot: error starting chat stream: %v", err)
}
return nil
}