add code to reconnect to database if connection is gettring broken or database getting a reboot
This commit is contained in:
@@ -138,6 +138,8 @@ class Server:
|
|||||||
game_board.save(
|
game_board.save(
|
||||||
StorageLoader.build(self.storage_type),
|
StorageLoader.build(self.storage_type),
|
||||||
file_path=os.path.join(self.data_path, 'data'),
|
file_path=os.path.join(self.data_path, 'data'),
|
||||||
|
database=os.getenv("EDGEDB_DATABASE", None),
|
||||||
|
tls_security=None
|
||||||
)
|
)
|
||||||
|
|
||||||
print("GAME ENDED: Winner is", [ x["name"] for x in game_state["board"]['snakes']])
|
print("GAME ENDED: Winner is", [ x["name"] for x in game_state["board"]['snakes']])
|
||||||
|
|||||||
@@ -1,16 +1,32 @@
|
|||||||
from server.GameBoard import GameBoard
|
from server.GameBoard import GameBoard
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import edgedb, json
|
import edgedb, json, time
|
||||||
|
|
||||||
class EdgeDB:
|
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(
|
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):
|
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 {
|
insert GameType {
|
||||||
name := <str>$name,
|
name := <str>$name,
|
||||||
is_ladder := <bool>$is_ladder
|
is_ladder := <bool>$is_ladder
|
||||||
@@ -32,7 +48,9 @@ class EdgeDB:
|
|||||||
def insert(self, game_board:GameBoard):
|
def insert(self, game_board:GameBoard):
|
||||||
game_type = game_board.get_type_of_game()
|
game_type = game_board.get_type_of_game()
|
||||||
|
|
||||||
self.client.query("""
|
self.run_query_with_reconnection(
|
||||||
|
self.client.query,
|
||||||
|
"""
|
||||||
insert GameBoard {
|
insert GameBoard {
|
||||||
id := <uuid>$id,
|
id := <uuid>$id,
|
||||||
created_at := <datetime>$created_at,
|
created_at := <datetime>$created_at,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from server.Files import save_file
|
|||||||
import json, os
|
import json, os
|
||||||
|
|
||||||
class LocalStorage:
|
class LocalStorage:
|
||||||
def __init__(self, file_path:str):
|
def __init__(self, file_path:str, **kwargs):
|
||||||
self.save_folder_dict = {
|
self.save_folder_dict = {
|
||||||
"standard": "01_Standard",
|
"standard": "01_Standard",
|
||||||
"duel": "02_Duels",
|
"duel": "02_Duels",
|
||||||
|
|||||||
Reference in New Issue
Block a user