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 50 forks source link

Bug - bot will sleep after a few hours and require restart of container to continue operating #32

Closed JackPala closed 1 year ago

JackPala commented 1 year ago

Hello - fantastic plugin super valuable for having ChatGPT built into mattermost.

I am experiencing a small bug in the software in which when the bot is marked as "away" after end of day when the workers are no longer using it, the bot will fail to wakeup again the morning when users start messaging it - requiring a restart of the container in order to continue using the plugin.

JackPala commented 1 year ago

Here is the status change that occurs upon restarting the container. it likely occurs as a result of the bot going from "online" to "away" - but I am unsure how or if that happens.

2023-05-20T19:14:08.382Z DEBUG name=bot msg={"event":"status_change","data":{"status":"online","user_id":"USERIDSANITIZED"},"broadcast":{"omit_users":null,"user_id":"USERIDSANITIZED","channel_id":"","team_id":"","connection_id":""},"seq":1} pid=1 hostname=9ef18cde95cb diff=3

yGuy commented 1 year ago

I have never seen this happen and we have the bot running for months, now. How is the bot connecting to Mattermost? Via a proxy or is there a direct connection? Could it be that your proxy kills the connection after 24h or restarts at night?

JackPala commented 1 year ago

It is connecting via a reverse proxy (Caddy2). I was able to get the final line in the debug log by letting it sit

2023-05-20T19:20:08.373Z DEBUG name=bot msg={"event":"status_change","data":{"status":"away","user_id":"USERIDSANITIZED"},"broadcast":{"omit_users":null,"user_id":USERIDSANITIZED","channel_id":"","team_id":"","connection_id":""},"seq":2} pid=1 hostname=SANITZIED diff=359991

I suspect that "status: away" is related to it.

Restarting the container and it springs back to life

yGuy commented 1 year ago

'seems the proxy is cutting the connection on inactivity. Reconfigure the proxy or restart the container automatically. Alternatively feel free to come up with a patch that implements a heart beat. Ideally though directly connect the bot with the mattermost instance via container networking.

PatrickHuetter commented 1 year ago

Same problem here in kubernetes with default nginx ingress controller.

yGuy commented 1 year ago

This is actually not a bug in the bot, but a misconfiguration with the other services. It could be a "feature" where we try to workaround the limitation of the other services by implementing a heart beat and graceful automatic reconnect. Right now, "restart-unless-stopped" and "simply fail and exit" works well enough for those cases where you detect an exception. But if the bot does not get triggered anymore, this simple approach fails, of course.

JackPala commented 1 year ago

Just to follow up, you were right on the fix, I have the bot connected directly to the mattermost instance instead of the reverse proxy thru an IP instead of the internet facing domain and it works flawlessly now.