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 { useEffect, useState } from 'react';
import { Modal, SmallModal } from './Modal'; import { Modal, SmallModal } from './Modal';
import { LoginChatBot, UpdateChatBotUrl } from '../../wailsjs/go/main/App'; import { LoginChatBot, UpdateChatBotUrl } from '../../wailsjs/go/main/App';
import './ChatBot.css'; import './ChatBot.css';

View file

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

View file

@ -21,14 +21,16 @@ import (
) )
type ChatBot struct { type ChatBot struct {
ctx context.Context ctx context.Context
client *rumblelivestreamlib.Client cancelChatStream context.CancelFunc
commands map[string]chan rumblelivestreamlib.ChatView cancelChatStreamMu sync.Mutex
commandsMu sync.Mutex client *rumblelivestreamlib.Client
Cfg config.ChatBot commands map[string]chan rumblelivestreamlib.ChatView
logError *log.Logger commandsMu sync.Mutex
messages map[string]*message Cfg config.ChatBot
messagesMu sync.Mutex logError *log.Logger
messages map[string]*message
messagesMu sync.Mutex
} }
type message struct { type message struct {
@ -389,22 +391,69 @@ func (cb *ChatBot) StartChatStream() error {
return fmt.Errorf("chatbot: error getting chat info: %v", err) return fmt.Errorf("chatbot: error getting chat info: %v", err)
} }
err = cb.client.StartChatStream(cb.handleChat, cb.handleError) ctx, cancel := context.WithCancel(context.Background())
if err != nil { cb.cancelChatStreamMu.Lock()
return fmt.Errorf("chatbot: error starting chat stream: %v", err) 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 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 { func (cb *ChatBot) StopChatStream() error {
if cb.client == nil { if cb.client == nil {
return fmt.Errorf("chatbot: client is nil") return fmt.Errorf("chatbot: client is nil")
} }
// TODO: should a panic be caught here? // 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() 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 return nil
} }