From fdaa579ec01d192b9ac534460b787973c8aeb2e5 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Sun, 28 Nov 2021 18:29:48 +0300 Subject: [PATCH] Read reqs in postgres should be in transactions In order to prevent break whole program instance by one failed request --- model/postgres_model.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/model/postgres_model.py b/model/postgres_model.py index faa30ef..f324e5b 100644 --- a/model/postgres_model.py +++ b/model/postgres_model.py @@ -48,16 +48,17 @@ class PostgresModel(AbstractModel): logger.debug(f'Not cached result for {cache_key}') - with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: - cursor.execute(postgres_sql_requests.select_activity_pretty_names, { - 'LB_type': utils.LeaderboardTypes(leaderboard_type.lower()).value, - 'platform': utils.Platform(platform.upper()).value, - 'limit': limit, - 'high_timestamp': high_timestamp, - 'low_timestamp': low_timestamp - }) + with self.db: + with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: + cursor.execute(postgres_sql_requests.select_activity_pretty_names, { + 'LB_type': utils.LeaderboardTypes(leaderboard_type.lower()).value, + 'platform': utils.Platform(platform.upper()).value, + 'limit': limit, + 'high_timestamp': high_timestamp, + 'low_timestamp': low_timestamp + }) - result: list = cursor.fetchall() + result: list = cursor.fetchall() if not cache.disabled: cache.set(cache_key, json.dumps(result)) @@ -118,9 +119,10 @@ class PostgresModel(AbstractModel): logger.debug(f'Not cached result for {cache_key}') - with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: - cursor.execute(postgres_sql_requests.select_diff_by_action_id, {'action_id': action_id}) - result: list = cursor.fetchall() + with self.db: + with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: + cursor.execute(postgres_sql_requests.select_diff_by_action_id, {'action_id': action_id}) + result: list = cursor.fetchall() if not cache.disabled: cache.set(cache_key, json.dumps(result))