谷歌Pubsub推送到AppEngine 502和504错误

时间:2018-06-05 19:49:58

标签: python google-app-engine google-cloud-platform publish-subscribe

我将此任务基于Google提供的文档: https://cloud.google.com/appengine/docs/flexible/python/writing-and-responding-to-pub-sub-messages

我最终为我的main提供了这个代码,它取出了全局存储的消息变量,而是将每个变量写入一个平面文件。

# [START push]
@app.route('/pubsub/push', methods=['POST'])
def pubsub_push():
    if (request.args.get('token', '') != current_app.config['PUBSUB_VERIFICATION_TOKEN']):
        return 'Invalid request', 400
    # Decode the data
    envelope = json.loads(request.data.decode('utf-8'))
    # Current time in UTC
    current_date = datetime.utcnow()

    payload = json.loads(base64.b64decode(envelope['message']['data']))
    # Normalize and flatten the data
    if "events" in payload and payload['events']:
        payload['events'] = payload['events'][0]['data']
    payload = parse_dict(init=payload, sep='_')
    # Now jsonify all remaining lists and dicts
    for key in payload.keys():
        value = payload[key]
        if isinstance(value, list) or isinstance(value, dict):
            if value:
                value = json.dumps(value)
            else:
                value = None
            payload[key] = value

    # Custom id with the message id and date string
    id = "{}.{}".format(
        payload['timestamp_unixtime_ms'],
        payload['message_id']
    )

    filename_hourly = 'landing_path/{date}/{hour}/{id}.json'.format(
        date=current_date.strftime("%Y%m%d"),
        hour=current_date.strftime("%H"),
        id=id
    )

    blob = bucket.get_blob(filename_hourly)
    if blob:  # We already have this file, skip this message
        print('Already have {} stored.'.format(filename_hourly))
        return 'OK', 200

    blob_hourly = Blob(bucket=bucket, name=filename_hourly)
    blob_hourly.upload_from_string(json.dumps(payload, indent=2, sort_keys=True))

    # Returning any 2xx status indicates successful receipt of the message.
    return 'OK', 200


# [END push]

这很好用,但我收到了大量的502和504错误。它显示在我创建的stackdriver仪表板中。

Backlog is filling and the errors are high

我的猜测是上传文件花了太长时间,但我不确定该怎么做。有什么建议? appengine框上的资源非常低,我的API限制甚至都不是很接近。

建议任何人?

1 个答案:

答案 0 :(得分:0)

此问题似乎已在Google Cloud Platform问题跟踪器link上得到解决。正如在该链接上所解释的那样,通常,当应用程序太忙而无法响应时,通常会导致5xx响应来自nginx。 nginx webserver位于Google App Engine实例上的App Engine应用程序之前。

但是,进一步审核建议避免使用Google Cloud Client Library的gevent异步工作人员,因为它们会导致请求挂起,而是使用更多gunicorn workers