8000 GCF SQL: Use proper environment variables (#1703) · cevaris/python-docs-samples@c359be8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c359be8

Browse files
author
Ace Nassri
authored
GCF SQL: Use proper environment variables (GoogleCloudPlatform#1703)
* Update postgres_sample.py * Update mysql_sample.py * Switch to try-except instead of env vars Change-Id: Ibe4d6ff8d6dc928bce8c62178d52e3c5485899be * Missed a spot Change-Id: Id5d50643012e2bb7f3f34cd1dc69c3385d62dc7f
1 parent 06cc18c commit c359be8

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

functions/sql/mysql_sample.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
from os import getenv
1717

1818
import pymysql
19-
20-
is_production = getenv('SUPERVISOR_HOSTNAME') is not None
19+
from pymysql.err import OperationalError
2120

2221
# TODO(developer): specify SQL connection details
2322
CONNECTION_NAME = getenv(
@@ -36,13 +35,9 @@
3635
'autocommit': True
3736
}
3837

39-
if is_production:
40-
mysql_config['unix_socket'] = \
41-
'/cloudsql/' + CONNECTION_NAME
42-
4338
# Create SQL connection globally to enable reuse
4439
# PyMySQL does not include support for connection pooling
45-
mysql_conn = pymysql.connect(**mysql_config)
40+
mysql_conn = None
4641

4742

4843
def __get_cursor():
@@ -53,12 +48,25 @@ def __get_cursor():
5348
"""
5449
try:
5550
return mysql_conn.cursor()
56-
except Exception:
51+
except OperationalError:
5752
mysql_conn.ping(reconnect=True)
5853
return mysql_conn.cursor()
5954

6055

6156
def mysql_demo(request):
57+
global mysql_conn
58+
59+
# Initialize connections lazily, in case SQL access isn't needed for this
60+
# GCF instance. Doing so minimizes the number of active SQL connections,
61+
# which helps keep your GCF instances under SQL connection limits.
62+
if not mysql_conn:
63+
try:
64+
mysql_conn = pymysql.connect(**mysql_config)
65+
except OperationalError:
66+
# If production settings fail, use local development ones
67+
mysql_config['unix_socket'] = f'/cloudsql/{CONNECTION_NAME}'
68+
mysql_conn = pymysql.connect(**mysql_config)
69+
6270
# Remember to close SQL resources declared while running this function.
6371
# Keep any declared in global scope (e.g. mysql_conn) for later reuse.
6472
with __get_cursor() as cursor:

functions/sql/postgres_sample.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
# [START functions_sql_postgres]
1616
from os import getenv
1717

18+
from psycopg2 import OperationalError
1819
from psycopg2.pool import SimpleConnectionPool
1920

20-
is_production = getenv('SUPERVISOR_HOSTNAME') is not None
21-
2221
# TODO(developer): specify SQL connection details
2322
CONNECTION_NAME = getenv(
2423
'INSTANCE_CONNECTION_NAME',
@@ -30,27 +29,35 @@
3029
pg_config = {
3130
'user': DB_USER,
3231
'password': DB_PASSWORD,
33-
'dbname': DB_NAME,
32+
'dbname': DB_NAME
3433
}
3534

36-
if is_production:
37-
pg_config['host'] = '/cloudsql/' + CONNECTION_NAME
38-
else:
39-
pg_config['host'] = 'localhost'
40-
4135
# Connection pools reuse connections between invocations,
4236
# and handle dropped or expired connections automatically.
4337
pg_pool = None
4438

4539

40+
def __connect(host):
41+
"""
42+
Helper function to connect to Postgres
43+
"""
44+
global pg_pool
45+
pg_config['host'] = host
46+
pg_pool = SimpleConnectionPool(1, 1, **pg_config)
47+
48+
4649
def postgres_demo(request):
4750
global pg_pool
4851

4952
# Initialize the pool lazily, in case SQL access isn't needed for this
5053
# GCF instance. Doing so minimizes the number of active SQL connections,
5154
# which helps keep your GCF instances under SQL connection limits.
5255
if not pg_pool:
53-
pg_pool = SimpleConnectionPool(1, 1, **pg_config)
56+
try:
57+
__connect(f'/cloudsql/{CONNECTION_NAME}')
58+
except OperationalError:
59+
# If production settings fail, use local development ones
60+
__connect('localhost')
5461

5562
# Remember to close SQL resources declared while running this function.
5663
# Keep any declared in global scope (e.g. pg_pool) for later reuse.

0 commit comments

Comments
 (0)
0