Google Cloud Functions崩溃而没有错误消息

问题描述

我正在运行http触发的云功能。该脚本在Flask条件下进行了测试,并完美运行。出于未知原因,云运行突然停止,并且出现“错误:无法处理请求”。 当我检查功能日志时,没有错误消息或任何崩溃提示。我所看到的只是程序打印的输出,直到停止为止

我分配了最大内存大小4GB,另外我还减少了请求中的数据大小(时间跨度更短,击穿时间更短)。但是,同样的问题。

这是代码停止的一部分(来自Twitter API的数据请求):

def getAsyncData(account,entity,date_from,date_to,metric_groups,network,segmented_by=False,country_lst = None):
    entity_dict = entity.active_entities(account,date_to)
    if entity_dict == []:
        return None
    print(json.dumps(entity_dict,indent=4,sort_keys=True))
    df = pd.DataFrame.from_dict(entity_dict)
    entity_arr = df['entity_id'].tolist()
    print(entity_arr)

    queued_job_ids = []

    for chunk_ids in getChunks(entity_arr,n=20):
        if (segmented_by == 'LOCATIONS') or (segmented_by is None) or (segmented_by =='PLATFORMS'):
            queued_job_ids.append(
                entity.queue_async_stats_job(account=account,ids=chunk_ids,metric_groups=metric_groups,start_time=date_from,end_time=date_to,granularity=GRANULARITY.DAY,segmentation_type=segmented_by,placement=network).id)

        elif segmented_by == 'REGIONS':
            for country in country_lst:
                queued_job_ids.append(
                    entity.queue_async_stats_job(account=account,placement=network,country=country,).id)

    print(queued_job_ids)
    if queued_job_ids == []:
        return None
    # let the job complete
    seconds = 10
    time.sleep(seconds)
    while True:
        async_stats_job_results = entity.async_stats_job_result(account,job_ids=queued_job_ids)
        if all(result.status == 'SUCCESS' for result in async_stats_job_results):
            break
    async_data = []

    for result in async_stats_job_results:
         async_data.append(entity.async_stats_job_data(account,url=result.url))
    print(json.dumps(async_data,sort_keys=True))
    return async_data

我在日志中看到的最后一个打印内容是queued_job_ids。之后-什么都没有。没有错误或任何其他活动。 我使用相同的代码段运行其他功能,并且运行良好。 可能是什么原因?有什么想法吗?

解决方法

早在8月,Cloud Functions就没有显示任何日志。您可以通过以下方式重新部署云功能来确认是否受到同一问题的影响:

gcloud functions deploy func --set-env-vars USE_WORKER_V2=true,PYTHON37_DRAIN_LOGS_ON_CRASH_WAIT_SEC=5 --runtime=python37 --runtime=python37

请查看此Issue Tracker,以获取更多信息。

如果重新部署该功能后仍然看不到任何日志,建议您使用新的Issue Tracker报告此问题,并按照注释中的建议进行解决,可以使用包装器。这是一个简单的示例:

import logging
import traceback
def try_catch_log(wrapped_func):
 def wrapper(*args,**kwargs):
   try:
     response = wrapped_func(*args,**kwargs)
   except Exception:
     error_message = traceback.format_exc().replace('\n','  ')
     logging.error(error_message)
     return 'Error';
   return response;
 return wrapper;
 
#Example hello world function
@try_catch_log
def hello_world(request):
 request_args = request.args
 print( 0 / 0 )
 return 'Hello World!'

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...