Skip to content

Commit

Permalink
Reset chat bot stream ever 90 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
tylertravisty committed Feb 14, 2024
1 parent cdbaf8f commit 7d0e825
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
2 changes: 0 additions & 2 deletions frontend/src/components/ChatBot.jsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/screens/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ function Dashboard() {
// console.log('Query response error:', error);
setActive(false);
});

EventsOn('ChatBotChatStreamError', (error) => {
setError(error);
});
}, []);

const home = () => {
Expand Down
73 changes: 61 additions & 12 deletions internal/chatbot/chatbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import (
)

type ChatBot struct {
ctx context.Context
client *rumblelivestreamlib.Client
commands map[string]chan rumblelivestreamlib.ChatView
commandsMu sync.Mutex
Cfg config.ChatBot
logError *log.Logger
messages map[string]*message
messagesMu sync.Mutex
ctx context.Context
cancelChatStream context.CancelFunc
cancelChatStreamMu sync.Mutex
client *rumblelivestreamlib.Client
commands map[string]chan rumblelivestreamlib.ChatView
commandsMu sync.Mutex
Cfg config.ChatBot
logError *log.Logger
messages map[string]*message
messagesMu sync.Mutex
}

type message struct {
Expand Down Expand Up @@ -389,22 +391,69 @@ 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
}

Expand Down

0 comments on commit 7d0e825

Please sign in to comment.