Skip to content

Commit dfe82f1

Browse files
committed
LCORE-741: refactored code used to connect to databases with quota
1 parent ddc9c6c commit dfe82f1

3 files changed

Lines changed: 56 additions & 39 deletions

File tree

src/quota/connect_pg.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""PostgreSQL connection handler."""
2+
3+
from typing import Any
4+
import psycopg2
5+
6+
from log import get_logger
7+
from models.config import PostgreSQLDatabaseConfiguration
8+
9+
logger = get_logger(__name__)
10+
11+
12+
def connect_pg(config: PostgreSQLDatabaseConfiguration) -> Any:
13+
"""Initialize connection to PostgreSQL database."""
14+
logger.info("Connecting to PostgreSQL storage")
15+
connection = psycopg2.connect(
16+
host=config.host,
17+
port=config.port,
18+
user=config.user,
19+
password=config.password.get_secret_value(),
20+
dbname=config.db,
21+
sslmode=config.ssl_mode,
22+
# sslrootcert=config.ca_cert_path,
23+
gssencmode=config.gss_encmode,
24+
)
25+
if connection is not None:
26+
connection.autocommit = True
27+
return connection

src/quota/connect_sqlite.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""SQLite connection handler."""
2+
3+
import sqlite3
4+
from typing import Any
5+
6+
from log import get_logger
7+
from models.config import SQLiteDatabaseConfiguration
8+
9+
logger = get_logger(__name__)
10+
11+
12+
def connect_sqlite(config: SQLiteDatabaseConfiguration) -> Any:
13+
"""Initialize connection to database."""
14+
logger.info("Connecting to SQLite storage")
15+
# make sure the connection will have known state
16+
# even if SQLite is not alive
17+
connection = None
18+
try:
19+
connection = sqlite3.connect(database=config.db_path)
20+
except sqlite3.Error as e:
21+
if connection is not None:
22+
connection.close()
23+
logger.exception("Error initializing SQLite cache:\n%s", e)
24+
raise
25+
connection.autocommit = True
26+
return connection

src/runners/quota_scheduler.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
from threading import Thread
55
from time import sleep
66

7-
import sqlite3
8-
import psycopg2
97

108
import constants
119
from log import get_logger
1210
from models.config import (
1311
Configuration,
1412
QuotaHandlersConfiguration,
1513
QuotaLimiterConfiguration,
16-
PostgreSQLDatabaseConfiguration,
17-
SQLiteDatabaseConfiguration,
1814
)
1915

16+
from quota.connect_pg import connect_pg
17+
from quota.connect_sqlite import connect_sqlite
18+
2019
from quota.sql import (
2120
CREATE_QUOTA_TABLE,
2221
INCREASE_QUOTA_STATEMENT_PG,
@@ -211,41 +210,6 @@ def connect(config: QuotaHandlersConfiguration) -> Any:
211210
return None
212211

213212

214-
def connect_pg(config: PostgreSQLDatabaseConfiguration) -> Any:
215-
"""Initialize connection to PostgreSQL database."""
216-
logger.info("Connecting to PostgreSQL storage")
217-
connection = psycopg2.connect(
218-
host=config.host,
219-
port=config.port,
220-
user=config.user,
221-
password=config.password.get_secret_value(),
222-
dbname=config.db,
223-
sslmode=config.ssl_mode,
224-
# sslrootcert=config.ca_cert_path,
225-
gssencmode=config.gss_encmode,
226-
)
227-
if connection is not None:
228-
connection.autocommit = True
229-
return connection
230-
231-
232-
def connect_sqlite(config: SQLiteDatabaseConfiguration) -> Any:
233-
"""Initialize connection to database."""
234-
logger.info("Connecting to SQLite storage")
235-
# make sure the connection will have known state
236-
# even if SQLite is not alive
237-
connection = None
238-
try:
239-
connection = sqlite3.connect(database=config.db_path)
240-
except sqlite3.Error as e:
241-
if connection is not None:
242-
connection.close()
243-
logger.exception("Error initializing SQLite cache:\n%s", e)
244-
raise
245-
connection.autocommit = True
246-
return connection
247-
248-
249213
def init_tables(connection: Any) -> None:
250214
"""Initialize tables used by quota limiter."""
251215
logger.info("Initializing tables for quota limiter")

0 commit comments

Comments
 (0)