yGuy / chatgpt-mattermost-bot

A very simple implementation of a service for a mattermost bot that uses ChatGPT in the backend.
MIT License
145 stars 49 forks source link

Bot working on Mattermost, but logs show errors and container restarts regularly #9

Closed zeppelinlg closed 1 year ago

zeppelinlg commented 1 year ago

The bot seems to be functioning on my Mattermost installation, but when I check the logs, I encounter the following messages:

chatgpt   | websocket connecting to wss://mattermost.zeppelinlg.com/api/v4/websocket
chatgpt   | {"level":"ERROR","time":"2023-03-30T16:15:00.682Z","name":"bot","msg":"undefined","pid":1,"hostname":"52ed9da46fb8","diff":0}
chatgpt   | {"level":"INFO","time":"2023-03-30T16:15:00.667Z","name":"bot-ws","msg":"websocket closed","pid":1,"hostname":"52ed9da46fb8","diff":0}

Additionally, the container seems to be restarting regularly. Any assistance with resolving these issues would be appreciated.

yGuy commented 1 year ago

Thanks for the feedback. I wasn't aware of this. Seems like for some reason the websocket to mattermost gets closed. The image is configured to automatically restart upon error and right now I treat a disconnected websocket as an error. Don't know whether maybe the mattermost instance disconnects the socket after a while or whether you had a flaky connection to the server, maybe? If it's a problem with the socket, that is slightly more difficult to debug. Maybe the MM server logs tell you something? You can turn up verbosity and see what happens (DEBUG LEVEL=TRACE). And actually running the bot directly from the command line were you could add some console logging is super, easy, too. Just clone, npm install and npm run (or debug with an IDE).

zeppelinlg commented 1 year ago

I don't think it's due to a connection stability issue, as the Docker container is on the same hypervisor as the Mattermost instance. I will run some tests locally this weekend using Visual Studio Code and try to determine if it's the error causing the disconnection or if the error is the disconnection itself. Initially, I thought it might be an unrecognized message format that could be causing the crash and disconnection. I will keep you updated on my findings.

yGuy commented 1 year ago

Any updates? Have you been able to narrow down the issue?

zeppelinlg commented 1 year ago

~I have found the cause of the issue. I have an HAProxy in front of my Mattermost, and increasing the server connection timeout in case of inactivity to 30 minutes (compared to 30 seconds) has resolved the problem. I believe that some people implement regular pinging to avoid this type of issue.~ Sorry, I spoke too soon. The issue is still ongoing. I haven't had the time to delve into debugging yet. I will keep you updated as soon as I have had the time to look into it :slightly_smiling_face:

yGuy commented 1 year ago

Ideally you would run both containers on the same, shared docker network without any proxy in between.

ronin-gw commented 1 year ago

I had the same issue and fixed it by directory accessing to a mattermost container instead of via nginx proxy.

yGuy commented 1 year ago

So I guess this is the proxy closing the connection when it's not in use for a while. We could add a ping/heartbeat, but I think it's better to connect to the server via docker networking and without the proxy in between. You can configure the proxy to not close WS connections, too, of course.