-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes for connection retry in case of db server restart fo prompt s…
…ervice
- Loading branch information
1 parent
fc77d70
commit 12b0a4a
Showing
5 changed files
with
56 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from collections.abc import Generator | ||
from contextlib import contextmanager | ||
from os import environ as env | ||
from typing import Any | ||
|
||
from playhouse.postgres_ext import PostgresqlExtDatabase | ||
|
||
|
||
def get_env_or_die(env_key: str) -> str: | ||
env_value = env.get(env_key) | ||
if not env_value: | ||
raise ValueError(f"Env variable {env_key} is required") | ||
return env_value | ||
|
||
|
||
# Load required environment variables | ||
db_host = get_env_or_die("PG_BE_HOST") | ||
db_port = get_env_or_die("PG_BE_PORT") | ||
db_user = get_env_or_die("PG_BE_USERNAME") | ||
db_pass = get_env_or_die("PG_BE_PASSWORD") | ||
db_name = get_env_or_die("PG_BE_DATABASE") | ||
|
||
be_db = PostgresqlExtDatabase( | ||
db_name, | ||
user=db_user, | ||
password=db_pass, | ||
host=db_host, | ||
port=db_port, | ||
) | ||
|
||
|
||
@contextmanager | ||
def db_context() -> Generator[PostgresqlExtDatabase, Any, None]: | ||
try: | ||
be_db.connect() | ||
yield be_db | ||
finally: | ||
if not be_db.is_closed(): | ||
be_db.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters