Google Cloud Functions崩溃而没有错误消息 [英] Google Cloud Functions crashes without an error message

查看:110
本文介绍了Google Cloud Functions崩溃而没有错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running an http triggered cloud function. The script was tested under Flask conditions, and ran perfectly. For an unknown reason, the cloud running suddenly stops and I get "Error: could not handle the request". When I checked the function logs, there was no error message or any indication for a crash. All I see is an output printed by the program, down to the point where it stops

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

I allocated the maximum memory size, 4GB, plus I reduced the size of data in my request (shorter time span, simple breakdown) . Yet, same problem.

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

This is a the part of the code where it stops (data request from 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.之后-什么都没有.没有错误或任何其他活动.我使用相同的代码段运行其他功能,并且运行正常.可能是什么原因?有什么想法吗?

The last print I see in the log is the queued_job_ids. after that - nothing. no error or any other activity. I run other functions with the very same piece of code and it runs fine. What could be the reason? any thoughts?

推荐答案

8月份出现了一个问题,即Cloud Functions不显示任何日志.您可以通过以下方式重新部署您的Cloud Functions来确认是否受到同一问题的影响:

There was an issue back in August where Cloud Functions did not show any logs. You could confirm if you are being affected by the same issue by redeploying your Cloud Functions as follows:

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

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

Please review this Issue Tracker for additional information.

如果重新部署该功能后仍然看不到任何日志,建议您在新的问题跟踪程序,并且如注释中所建议,作为解决方法,您可以使用包装器.这是一个简单的示例:

If after redeploying the function, you are still not able to see any logs, I would recommend you to report this in a new Issue Tracker and as suggested in the comments, as a workaround, you could use a wrapper. Here’s a quick example:

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!'

这篇关于Google Cloud Functions崩溃而没有错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆