change that GameplayDatabase can have different backends, sqlite and postgresql with a Template example backend

This commit is contained in:
2026-04-08 14:28:39 +02:00
parent a62501cf22
commit 341bb27278
10 changed files with 1660 additions and 708 deletions
+12 -3
View File
@@ -5,6 +5,7 @@ from snakes import SnakeBuilder
from server.database import (
GameplayDatabase,
GameplayBackendBuilder,
StorageLoader,
)
from server.metrics import (
@@ -29,7 +30,7 @@ from server.services import (
)
class Server:
def __init__(self, data_path:str, snake_type:str, storage_type:str, debug:bool=False, check_tls_security:bool=False, metrics_backend:str='memory', metrics_redis_url:str='redis://localhost:6379/0', metrics_ttl_sec:int|None=None, gameplay_db_enabled:bool=True, gameplay_db_path:str|None=None, gameplay_db_busy_timeout_ms:int=5000):
def __init__(self, data_path:str, snake_type:str, storage_type:str, debug:bool=False, check_tls_security:bool=False, metrics_backend:str='memory', metrics_redis_url:str='redis://localhost:6379/0', metrics_ttl_sec:int|None=None, gameplay_db_enabled:bool=True, gameplay_db_backend:str='sqlite', gameplay_db_path:str|None=None, gameplay_db_busy_timeout_ms:int=5000, gameplay_db_pg_dsn:str|None=None):
self.debug = debug
self.data_path = data_path
@@ -74,8 +75,12 @@ class Server:
if gameplay_db_enabled:
db_path = gameplay_db_path or os.path.join(data_path, 'data', 'database', 'gameplay.sqlite3')
self.gameplay_database = GameplayDatabase(
db_path=db_path,
busy_timeout_ms=gameplay_db_busy_timeout_ms,
backend=GameplayBackendBuilder.build(
backend=gameplay_db_backend,
db_path=db_path,
busy_timeout_ms=gameplay_db_busy_timeout_ms,
pg_dsn=gameplay_db_pg_dsn,
)
)
self.gameplay_tracking = GameplayTrackingService(
@@ -115,12 +120,16 @@ class Server:
if self._startup_worker_metrics_cleared:
return
self._startup_worker_metrics_cleared = True
if env_bool('METRICS_CLEAR_WORKERS_ON_STARTUP', True):
should_clear = await self.metrics_collector.should_clear_worker_metrics_on_startup(env_int('METRICS_STARTUP_CLEANUP_LOCK_TTL_SEC', 300))
if should_clear:
await self.metrics_collector.clear_worker_metrics()
await self.dashboard_events_service.start_listener()
if self.gameplay_database is not None:
await self.gameplay_database.initialize()
@self.app.after_serving
async def shutdown_state_storage():
await self.dashboard_events_service.stop_listener()