У меня есть облачная функция, запущенная в Google Cloud с HTTP-триггером.Триггер работает нормально, а также функция, но я получаю ошибки для его выполнения через Google Scheduler.Функция подключается к Cloud SQL и вставляет и получает информацию из базы данных: Вот функция:
from os import getenv
import requests
import pymysql
import json
from pymysql.err import OperationalError
# TODO(developer): specify SQL connection details
CONNECTION_NAME = getenv('INSTANCE_CONNECTION_NAME', 'name')
DB_USER = getenv('MYSQL_USER', 'root')
DB_PASSWORD = getenv('MYSQL_PASSWORD', 'password')
DB_NAME = getenv('MYSQL_DATABASE', 'dbName')
mysql_config = {
'user': DB_USER,
'password': DB_PASSWORD,
'db': DB_NAME,
'charset': 'utf8mb4',
'cursorclass': pymysql.cursors.DictCursor,
'autocommit': True
}
# Create SQL connection globally to enable reuse
# PyMySQL does not include support for connection pooling
mysql_conn = None
def __get_cursor():
"""
Helper function to get a cursor
PyMySQL does NOT automatically reconnect,
so we must reconnect explicitly using ping()
"""
try:
return mysql_conn.cursor()
except OperationalError:
mysql_conn.ping(reconnect=True)
return mysql_conn.cursor()
def authMonzo(request):
global mysql_conn
global endpoint
endpoint = 'https://****/oauth2/token'
# Initialize connections lazily, in case SQL access isn't needed for this
# GCF instance. Doing so minimizes the number of active SQL connections,
# which helps keep your GCF instances under SQL connection limits.
if not mysql_conn:
try:
mysql_conn = pymysql.connect(**mysql_config)
except OperationalError:
# If production settings fail, use local development ones
mysql_config['unix_socket'] = f'/cloudsql/{CONNECTION_NAME}'
mysql_conn = pymysql.connect(**mysql_config)
# Remember to close SQL resources declared while running this function.
# Keep any declared in global scope (e.g. mysql_conn) for later reuse.
with __get_cursor() as cursor:
cursor.execute("SELECT * FROM TOKENS ORDER BY ID DESC LIMIT 1")
result = cursor.fetchall()
clientSecret = result[len(result) - 1]['CLIENT_SECRET']
clientId = result[len(result) - 1]['CLIENT_ID']
refreshToken = result[len(result) - 1]['REFRESH_TOKEN']
# Change the last refresh token for a new access token
tokenRequest = requests.post(endpoint, data={'grant_type': 'refresh_token', 'client_id': clientId, 'client_secret': clientSecret, 'refresh_token': refreshToken})
tokenJson = json.loads(tokenRequest.content)
print(tokenJson)
# Assing the values to variables
accessToken = tokenJson['access_token']
refreshToken = tokenJson['refresh_token']
scope = tokenJson['scope']
clientId = tokenJson['client_id']
expiresIn = tokenJson['expires_in']
userId = tokenJson['user_id']
tokenType = tokenJson['token_type']
# Insert the new token on the table tokens
SQLToken = ("INSERT INTO TOKENS (CLIENT_SECRET, ACCESS_TOKEN, REFRESH_TOKEN, TOKEN_TYPE, USER_ID, CLIENT_ID, EXPIRES_IN, SCOPE) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
Values = (clientSecret, accessToken, refreshToken, tokenType, userId, clientId, expiresIn, scope)
# Insert new account information
cursor.execute(SQLToken, Values)
return str(accessToken)
После попытки более 3 или 4 раз я получила статус Success
для выполнения.
Следуйте Json, извлеченному из журнала:
{
insertId: "000000-dca60f91-5c0f-439f-b21c-bda99114ddce"
labels: {
execution_id: ""
}
logName: "projects/quidsave-237317/logs/cloudfunctions.googleapis.com%2Fcloud-functions"
receiveTimestamp: "2019-05-02T12:15:55.145395087Z"
resource: {
labels: {
function_name: "authMonzo"
project_id: "quidsave-237317"
region: "europe-west2"
}
type: "cloud_function"
}
severity: "ERROR"
textPayload: "Error: function crashed. Function invocation was interrupted.
"
timestamp: "2019-05-02T12:15:48.766445135Z"
}
Это ошибка:
severity: "ERROR"
textPayload: "Traceback (most recent call last):
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 346, in run_http_function
result = _function_handler.invoke_user_function(flask.request)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
return call_user_function(request_or_event)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 210, in call_user_function
return self._user_function(request_or_event)
File "/user_code/main.py", line 60, in authMonzo
cursor.execute("SELECT * FROM TOKENS ORDER BY ID DESC LIMIT 1")
File "/env/local/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/env/local/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 516, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/env/local/lib/python3.7/site-packages/pymysql/connections.py", line 750, in _execute_command
raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')