Skip to content

Commit

Permalink
Recover from db error
Browse files Browse the repository at this point in the history
  • Loading branch information
amadejkastelic committed Jul 15, 2024
1 parent 097aac5 commit f399a28
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions bot/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import typing

import magic
from django.db import connections
from django import db as django_db

emoji = ['😼', '😺', '😸', '😹', '😻', '🙀', '😿', '😾', '😩', '🙈', '🙉', '🙊', '😳']

Expand Down Expand Up @@ -56,7 +56,7 @@ def random_emoji() -> str:
return random.choice(emoji)


def recover_from_db_error():
for conn in connections.all():
conn.close()
conn.connect()
def recover_from_db_error(exc: Exception):
if isinstance(exc, django_db.OperationalError) and exc.args[0] in (2006, 2013):
django_db.reset_queries()
django_db.close_old_connections()
2 changes: 1 addition & 1 deletion bot/management/commands/discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ def handle(self, *args: typing.Any, **options: typing.Any) -> typing.NoReturn:
asyncio.run(bot.DiscordBot().run())
except db.OperationalError as e:
logging.warning(f'DB Connection expired, reconnecting... {str(e)}')
utils.recover_from_db_error()
utils.recover_from_db_error(e)
2 changes: 1 addition & 1 deletion bot/management/commands/purge_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def handle(self, *args: typing.Any, **options: typing.Any) -> typing.NoReturn:
).delete()
except db.OperationalError as e:
logging.warning(f'DB Connection expired, reconnecting... {str(e)}')
utils.recover_from_db_error()
utils.recover_from_db_error(e)
time.sleep(1)
continue

Expand Down

0 comments on commit f399a28

Please sign in to comment.