add code to reconnect to database if connection is gettring broken or database getting a reboot
This commit is contained in:
@@ -1,16 +1,32 @@
|
||||
from server.GameBoard import GameBoard
|
||||
|
||||
from datetime import datetime
|
||||
import edgedb, json
|
||||
import edgedb, json, time
|
||||
|
||||
class EdgeDB:
|
||||
def __init__(self, **kwargs):
|
||||
def __init__(self, database:str=None, tls_security:str='insecure', **kwargs):
|
||||
self.database = database
|
||||
self.tls_security = tls_security
|
||||
self._connect()
|
||||
|
||||
def _connect(self):
|
||||
self.client = edgedb.create_client(
|
||||
tls_security="insecure"
|
||||
tls_security=self.tls_security,
|
||||
database=self.database
|
||||
)
|
||||
|
||||
def run_query_with_reconnection(self, function, *args, **kwargs):
|
||||
while True:
|
||||
try:
|
||||
return function(*args, **kwargs)
|
||||
except edgedb.errors.ClientConnectionFailedError:
|
||||
self._connect()
|
||||
time.sleep(0.5)
|
||||
|
||||
def insert_game_type(self, name:str, is_ladder:bool):
|
||||
return self.client.query_required_single("""
|
||||
return self.run_query_with_reconnection(
|
||||
self.client.query_required_single,
|
||||
"""
|
||||
insert GameType {
|
||||
name := <str>$name,
|
||||
is_ladder := <bool>$is_ladder
|
||||
@@ -32,7 +48,9 @@ class EdgeDB:
|
||||
def insert(self, game_board:GameBoard):
|
||||
game_type = game_board.get_type_of_game()
|
||||
|
||||
self.client.query("""
|
||||
self.run_query_with_reconnection(
|
||||
self.client.query,
|
||||
"""
|
||||
insert GameBoard {
|
||||
id := <uuid>$id,
|
||||
created_at := <datetime>$created_at,
|
||||
|
||||
@@ -4,7 +4,7 @@ from server.Files import save_file
|
||||
import json, os
|
||||
|
||||
class LocalStorage:
|
||||
def __init__(self, file_path:str):
|
||||
def __init__(self, file_path:str, **kwargs):
|
||||
self.save_folder_dict = {
|
||||
"standard": "01_Standard",
|
||||
"duel": "02_Duels",
|
||||
|
||||
Reference in New Issue
Block a user