1

我正在运行一个 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_from, 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, ids=chunk_ids, metric_groups=metric_groups,
                                                 start_time=date_from,
                                                 end_time=date_to,
                                                 granularity=GRANULARITY.DAY,
                                                 placement=network,
                                                 segmentation_type=segmented_by,
                                                 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, indent=4, sort_keys=True))
    return async_data

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

4

1 回答 1

1

早在 8 月就有一个问题,Cloud Functions 没有显示任何日志。您可以通过重新部署 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

请查看此问题跟踪器以获取更多信息。

如果重新部署该功能后,您仍然看不到任何日志,我建议您在新的问题跟踪器中报告此问题,并按照评论中的建议,作为一种解决方法,您可以使用包装器。这是一个简单的例子:

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!'
于 2020-11-03T18:12:59.353 回答